如何在pgx中使用goose迁移?

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

How to use goose migrations with pgx?

问题

我有一个小的Go Web应用程序,它使用PostgreSQL。

我的数据库驱动程序是pgx。现在我想在应用程序启动时自动运行迁移。我找到了goose来做这件事。

但是,我在使用goosepgx驱动程序时遇到了困难。

当我运行我的代码时,我会得到以下错误消息:

打开数据库
2023/08/09 09:39:28 sql: 未知的驱动程序 "pgx"(是否忘记导入?)

以下是缩短的main.go代码:

package main

import (
    "context"
    "flag"
    "fmt"
    "log"
    "os"

    _ "github.com/jackc/pgx/v5"
    "github.com/pressly/goose/v3"
)

func main() {
    // DSN是PostgreSQL数据源名称,通常与连接字符串相同。
    dsn := flag.String("dsn", "postgres://postgres:password@localhost:5555/plazet", "PostgreSQL数据源名称")
    flag.Parse()

    db, err := openDbConnectionPool(*dsn)
    if err != nil {
        fmt.Printf("无法连接到数据库:%v\n", err)
        os.Exit(1)
    }

    // Ping数据库以检查连接是否正常工作
    connection, err := db.Acquire(context.Background())
    if err != nil {
        fmt.Printf("无法获取连接:%v\n", err)
        os.Exit(1)
    }

    println("打开数据库")

    sql, err := goose.OpenDBWithDriver("pgx", *dsn)
    if err != nil {
        log.Fatalf(err.Error())
    }

    println("迁移")

    err = goose.Up(sql, "./migrations")
    if err != nil {
        log.Fatalf(err.Error())
    }

    defer connection.Release()

    defer db.Close()

    // 路由和其他代码...
}

我检查了OpenDBWithDriverpgx,它们都是预期的值。在这种情况下,我漏掉了什么?

英文:

I have a small go web application that is using PostgreSQL.

My database driver is pgx. Now I am at a point where I want to run migrations automatically at applications start. I found goose for that.

However, I am struggling to use goose with the pgx driver.

When I run my code I will get:

Opening DB
2023/08/09 09:39:28 sql: unknown driver "pgx" (forgotten import?)

Here is the shortened main.go:

package main

import (
"context"
"flag"
"fmt"
"log"
"os"

    _ "github.com/jackc/pgx/v5"
    "github.com/pressly/goose/v3"

)

func main() {

    // DSN is the PostgreSQL Data Source Name, Database Source Name.
    // Often the same as connection string.
    dsn := flag.String("dsn", "postgres://postgres:password@localhost:5555/plazet", "PostgreSQL data source name")
    flag.Parse()
    
    db, err := openDbConnectionPool(*dsn)
    if err != nil {
    	fmt.Printf("Unable to connect to database: %v\n", err)
    	os.Exit(1)
    }
    
    // Ping the database to check if the connection is working
    connection, err := db.Acquire(context.Background())
    if err != nil {
    	fmt.Printf("Unable to acquire connection: %v\n", err)
    	os.Exit(1)
    }
    
    println("Opening DB")
    
    sql, err := goose.OpenDBWithDriver("pgx", *dsn)
    if err != nil {
    	log.Fatalf(err.Error())
    }
    
    println("Migrating")
    
    err = goose.Up(sql, "./migrations")
    if err != nil {
    	log.Fatalf(err.Error())
    }
    
    defer connection.Release()
    
    defer db.Close()
    
    // Code for router and stuff...

}

I checked the OpenDBWithDriver and pgx is an expected value. What am I missing in this case?

答案1

得分: 1

驱动程序 pgx 是由 github.com/jackc/pgx/v5/stdlib 包注册的(源代码):

package stdlib

// ...

func init() {
	pgxDriver = &Driver{
		configs: make(map[string]*pgx.ConnConfig),
	}

	// 如果不同的 pgx 主要版本已经注册了 pgx 驱动程序,则我们跳过使用默认名称进行注册。
	if !contains(sql.Drivers(), "pgx") {
		sql.Register("pgx", pgxDriver)
	}
	sql.Register("pgx/v5", pgxDriver)
	// ...
}

尝试导入该包:

import (
    _ "github.com/jackc/pgx/v5/stdlib"
    "github.com/pressly/goose/v3"
)
英文:

The driver pgx is registered by the github.com/jackc/pgx/v5/stdlib package (source code):

package stdlib

// ...

func init() {
	pgxDriver = &Driver{
		configs: make(map[string]*pgx.ConnConfig),
	}

	// if pgx driver was already registered by different pgx major version then we
	// skip registration under the default name.
	if !contains(sql.Drivers(), "pgx") {
		sql.Register("pgx", pgxDriver)
	}
	sql.Register("pgx/v5", pgxDriver)
	// ...

Try importing that package instead:

import (
    _ "github.com/jackc/pgx/v5/stdlib"
    "github.com/pressly/goose/v3"
)

huangapple
  • 本文由 发表于 2023年8月9日 15:53:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865674-2.html
匿名

发表评论

匿名网友

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

确定