Can';t将txdb与GORMATE一起使用

Can';t将txdb与GORMATE一起使用,go,go-gorm,Go,Go Gorm,我试图在一个独立的事务中迁移数据库,用于测试与数据库交互的代码,也用于测试迁移本身。但每次我运行代码时,都会返回一个错误,Gorm会发出一些警告: $ DATABASE_URL="postgres://postgres@localhost:5432/postgres?sslmode=disable" ./txdb_ejemplo `txdb_postgres` is not officially supported, running under compatibility mode. (/ho

我试图在一个独立的事务中迁移数据库,用于测试与数据库交互的代码,也用于测试迁移本身。但每次我运行代码时,都会返回一个错误,Gorm会发出一些警告:

$ DATABASE_URL="postgres://postgres@localhost:5432/postgres?sslmode=disable" ./txdb_ejemplo
`txdb_postgres` is not officially supported, running under compatibility mode.

(/home/jorge/go/pkg/mod/gopkg.in/gormigrate.v1@v1.6.0/gormigrate.go:381)
[2019-08-12 19:46:28]  pq: current transaction is aborted, commands ignored until end of transaction block
panic: migration failed: pq: current transaction is aborted, commands ignored until end of transaction block

goroutine 1 [running]:
main.main()
        /home/jorge/proyectos/kue/fero/txdb_ejemplo/main.go:76 +0x3a1
这是提供数据库的docker服务的输出:

db_1  | 2019-08-13 01:46:08.788 UTC [1] LOG:  database system is ready to accept connections
db_1  | 2019-08-13 01:46:28.564 UTC [48] ERROR:  function database() does not exist at character 8
db_1  | 2019-08-13 01:46:28.564 UTC [48] HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
db_1  | 2019-08-13 01:46:28.564 UTC [48] STATEMENT:  SELECT DATABASE()
db_1  | 2019-08-13 01:46:28.564 UTC [48] ERROR:  syntax error at or near "AND" at character 71
db_1  | 2019-08-13 01:46:28.564 UTC [48] STATEMENT:  SELECT count(*) FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = ? AND table_name = ?
db_1  | 2019-08-13 01:46:28.564 UTC [48] ERROR:  current transaction is aborted, commands ignored until end of transaction block
db_1  | 2019-08-13 01:46:28.564 UTC [48] STATEMENT:  CREATE TABLE migrations (id VARCHAR(255) PRIMARY KEY)
我不明白我做错了什么,但我提供了一个最小的函数示例,它再现了我遇到的问题,并且有一个Go代码的入门:

package main

import (
    "fmt"
    "os"
    "time"

    "github.com/DATA-DOG/go-txdb"
    "github.com/jinzhu/gorm"
    _ "github.com/lib/pq"
    gormigrate "gopkg.in/gormigrate.v1"
)

var (
    migration_1 = func(tx *gorm.DB) error {
        type Person struct {
            gorm.Model
            Name string
        }

        return tx.CreateTable(&Person{}).Error
    }
    migration_2 = func(tx *gorm.DB) error {
        type Person struct {
            Age int
        }
        return tx.AutoMigrate(&Person{}).Error
    }
    return_nil = func(tx *gorm.DB) error {
        return nil
    }
)

type Person struct {
    gorm.Model
    Name string
    Age  int
}

func MigrateAll(gdb *gorm.DB) error {
    m := gormigrate.New(gdb, gormigrate.DefaultOptions, []*gormigrate.Migration{
        {
            ID:       "first",
            Migrate:  migration_1,
            Rollback: return_nil,
        },
        {
            ID:       "second",
            Migrate:  migration_2,
            Rollback: return_nil,
        },
    })
    return m.Migrate()
}

func main() {
    url := os.Getenv("DATABASE_URL")
    txdb.Register("txdb_postgres", "postgres", url)
    var db *gorm.DB
    var err error
    for i := 0; i < 3; i++ {
        db, err = gorm.Open("txdb_postgres", "tx_1")
        if err == nil {
            break
        }
        fmt.Printf("connection failed, retrying in 10 seconds. Reason: %s\n", err)
        time.Sleep(10 * time.Second)
    }
    if err != nil {
        panic(fmt.Sprintf("connection failed: %s", err))
    }

    defer db.Close()
    err = MigrateAll(db)
    if err != nil {
        panic(fmt.Sprintf("migration failed: %s", err))
    }
}
主程序包
进口(
“fmt”
“操作系统”
“时间”
“github.com/DATA-DOG/go-txdb”
“github.com/jinju/gorm”
_“github.com/lib/pq”
gormigate“gopkg.in/gormigate.v1”
)
变量(
迁移_1=func(tx*gorm.DB)错误{
类型Person结构{
戈姆模型
名称字符串
}
返回tx.CreateTable(&Person{})。错误
}
迁移_2=func(tx*gorm.DB)错误{
类型Person结构{
年龄智力
}
返回tx.AutoMigrate(&Person{})。错误
}
return_nil=func(tx*gorm.DB)错误{
归零
}
)
类型Person结构{
戈姆模型
名称字符串
年龄智力
}
func MigrateAll(gdb*gorm.DB)错误{
m:=gormigate.New(gdb,gormigate.DefaultOptions,[]*gormigate.Migration{
{
ID:“第一”,
迁移:迁移1,
回滚:返回\u nil,
},
{
ID:“第二”,
迁移:迁移2,
回滚:返回\u nil,
},
})
返回m.Migrate()
}
func main(){
url:=os.Getenv(“数据库url”)
注册(“txdb_postgres”,“postgres”,url)
var db*gorm.db
变量错误
对于i:=0;i<3;i++{
db,err=gorm.Open(“txdb\u postgres”,“tx\u 1”)
如果err==nil{
打破
}
fmt.Printf(“连接失败,10秒钟后重试。原因:%s\n”,错误)
时间。睡眠(10*时间。秒)
}
如果错误!=零{
死机(fmt.Sprintf(“连接失败:%s”,错误))
}
延迟db.Close()
err=MigrateAll(db)
如果错误!=零{
死机(fmt.Sprintf(“迁移失败:%s”,错误))
}
}

问题在于
gorm
将第一个
gorm.Open
参数用作方言名称(它有一组预定义的方言),而不是
数据库/sql
驱动程序名称

因此,您需要使用
gorm
已知方言名称作为第一个参数,在本例中,您需要
的“postgres”
,因为您将使用postgresql作为基础数据库,因此您需要使用相应的sql方言
gorm

然后告诉
gorm
使用
“txdb”
驱动程序和
“tx_1”
作为连接url

这似乎有效:

package main

import (
    "fmt"
    "os"
    "time"

    "github.com/DATA-DOG/go-txdb"
    "github.com/jinzhu/gorm"
    _ "github.com/lib/pq"
    gormigrate "gopkg.in/gormigrate.v1"
)

var (
    migration_1 = func(tx *gorm.DB) error {
        type Person struct {
            gorm.Model
            Name string
        }

        return tx.CreateTable(&Person{}).Error
    }
    migration_2 = func(tx *gorm.DB) error {
        type Person struct {
            Age int
        }
        return tx.AutoMigrate(&Person{}).Error
    }
    return_nil = func(tx *gorm.DB) error {
        return nil
    }
)

type Person struct {
    gorm.Model
    Name string
    Age  int
}

func MigrateAll(gdb *gorm.DB) error {
    m := gormigrate.New(gdb, gormigrate.DefaultOptions, []*gormigrate.Migration{
        {
            ID:       "first",
            Migrate:  migration_1,
            Rollback: return_nil,
        },
        {
            ID:       "second",
            Migrate:  migration_2,
            Rollback: return_nil,
        },
    })
    return m.Migrate()
}

func main() {
    url := os.Getenv("DATABASE_URL")
    txdb.Register("txdb", "postgres", url)
    var db *gorm.DB
    var err error
    for i := 0; i < 3; i++ {
        db, err = gorm.Open("postgres", "txdb", "tx_1")
        if err == nil {
            break
        }
        fmt.Printf("connection failed, retrying in 10 seconds. Reason: %s\n", err)
        time.Sleep(10 * time.Second)
    }
    if err != nil {
        panic(fmt.Sprintf("connection failed: %s", err))
    }

    defer db.Close()
    err = MigrateAll(db)
    if err != nil {
        panic(fmt.Sprintf("migration failed: %s", err))
    }
}

那也行。

你救了我的命,谢谢!我已经更新了示例代码,以反映您的答案在两个不同分支中的实现
        s, err := sql.Open("txdb", "tx_1")
        // handle err
        db, err := gorm.Open("postgres", s)
        // handle err