用 gorm 写一个 goose go migration
Writing a goose go migration with gorm
默认的 goose go 迁移准备了一个提供 *sql.Tx
:
的函数
A transaction is provided, rather than the DB instance directly, since goose also needs to record the schema version within the same transaction. Each migration should run as a single transaction to ensure DB integrity, so it's good practice anyway.
我想使用 gorm migrations 编写我的迁移,但我不确定如何使用给定的事务来实现该目的。这是一个例子:
func Up_20151230135812(txn *sql.Tx) {
txn.CreateTable(&User{})
}
该版本按预期给了我 txn.CreateTable undefined (type *sql.Tx has no field or method CreateTable)
。我如何获取交易以用于 gorm?
goose 对 gorm 及其功能(CreateTable 等)一无所知
看goose / lib / goose / migration_go.go
结尾
鹅刚刚创建交易
db, err := goose.OpenDBFromDBConf(&conf)
if err != nil {
log.Fatal("failed to open DB:", err)
}
defer db.Close()
txn, err := db.Begin()
if err != nil {
log.Fatal("db.Begin:", err)
}
{{ .Func }}(txn)
err = goose.FinalizeMigration(&conf, txn, {{ .Direction }}, {{ .Version }})
if err != nil {
log.Fatal("Commit() failed:", err)
}
并使用 "database/sql" 包中的 sql.Tx
但您可以使用 gorm 实现自定义包装器 ;-)
默认的 goose go 迁移准备了一个提供 *sql.Tx
:
A transaction is provided, rather than the DB instance directly, since goose also needs to record the schema version within the same transaction. Each migration should run as a single transaction to ensure DB integrity, so it's good practice anyway.
我想使用 gorm migrations 编写我的迁移,但我不确定如何使用给定的事务来实现该目的。这是一个例子:
func Up_20151230135812(txn *sql.Tx) {
txn.CreateTable(&User{})
}
该版本按预期给了我 txn.CreateTable undefined (type *sql.Tx has no field or method CreateTable)
。我如何获取交易以用于 gorm?
goose 对 gorm 及其功能(CreateTable 等)一无所知
看goose / lib / goose / migration_go.go
结尾鹅刚刚创建交易
db, err := goose.OpenDBFromDBConf(&conf)
if err != nil {
log.Fatal("failed to open DB:", err)
}
defer db.Close()
txn, err := db.Begin()
if err != nil {
log.Fatal("db.Begin:", err)
}
{{ .Func }}(txn)
err = goose.FinalizeMigration(&conf, txn, {{ .Direction }}, {{ .Version }})
if err != nil {
log.Fatal("Commit() failed:", err)
}
并使用 "database/sql" 包中的 sql.Tx
但您可以使用 gorm 实现自定义包装器 ;-)