如何在pgx中使用goose迁移?

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

How to use goose migrations with pgx?

问题

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

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

然而,我在使用pgx驱动程序时遇到了困难。

当我运行我的代码时,会出现以下错误:

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

这是简化后的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 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数据库以检查连接是否正常工作
	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()

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

}

我检查了OpenDBWithDriver和pgx是一个预期的值。在这种情况下,我漏掉了什么?

英文:

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

驱动程序pgxgithub.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.html
匿名

发表评论

匿名网友

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

确定