鹅的交易何时被提交?

huangapple go评论84阅读模式
英文:

When are go goose transactions committed?

问题

我有一些需要在类似于这里Tx中运行的代码。
我按照官方文档中关于go goose的说明生成了一个go sql迁移,就像这里所提到的那样。

我的问题是:

  • 这些事务实际上是何时提交到数据库的?
  • 我使用tx.Commit()强制提交了迁移。这是我的go迁移文件:
func Up00002(tx *sql.Tx) error {
	_, err := tx.Exec("UPDATE users SET username='admin' WHERE username='root';")
	if err != nil {
		return err
	}

	if err = tx.Commit(); err != nil {
		return fmt.Errorf("提交事务时出错:%w", err)
	}

	return nil
}

在运行down部分之后,我无法运行上述迁移,因为当我运行它们时,我会得到ErrTxDone错误(来源)。我仍然希望多次运行go迁移Up00002(用于测试)。我在这里做错了什么?如何解决这个问题?

英文:
  • I have some code that needs to be run in a Tx like here
  • I generated a go sql migration as mentioned here in the official docs for go goose

my questions are:

  • when are these transactions actually committed to the database?
  • I forced the migrations to be committed with tx.Commit(). Here's my migration file in go:
func Up00002(tx *sql.Tx) error {
	_, err := tx.Exec("UPDATE users SET username='admin' WHERE username='root';")
	if err != nil {
		return err
	}

	if err = tx.Commit(); err != nil {
		return fmt.Errorf("error during committing the transaction: %w", err)
	}


	return nil
}

I am unable to run the above migration after the down part as I get the ErrTxDone (source) when I run them. I still want to run the go migration Up00002 multiple times (for testing). How can I do this? What am I doing wrong here?

答案1

得分: 1

所以,仅仅浏览他们的文档这里显示,提交已经在AddMigrations调用中发生了 - 这当然是通过事务处理的,并且由go goose在内部处理 - 非常好 鹅的交易何时被提交?

对于 SQL 也是如此,我们在这里看到了。

该包还提供了--no-transaction选项,我们可以使用它来完全控制db对象 - 太棒了!

英文:

So, just going through their documentations here shows that the commit already happens in an AddMigrations call - this is with transactions ofc and is handled internally by go goose - pretty nice 鹅的交易何时被提交?

The same is true for sql as well, as we see it here

The pkg also provides --no-transaction option which we can use to have full control of the db object - awesome!

huangapple
  • 本文由 发表于 2023年3月27日 02:17:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75849689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定