英文:
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 
Txlike 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 
committedto 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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论