如何在pgx中使用goose迁移?

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

How to use goose migrations with pgx?

问题

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

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

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

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

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

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

  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "os"
  8. _ "github.com/jackc/pgx/v5"
  9. "github.com/pressly/goose/v3"
  10. )
  11. func main() {
  12. // DSN是PostgreSQL数据源名称,通常与连接字符串相同。
  13. dsn := flag.String("dsn", "postgres://postgres:password@localhost:5555/plazet", "PostgreSQL数据源名称")
  14. flag.Parse()
  15. db, err := openDbConnectionPool(*dsn)
  16. if err != nil {
  17. fmt.Printf("无法连接到数据库:%v\n", err)
  18. os.Exit(1)
  19. }
  20. // Ping数据库以检查连接是否正常工作
  21. connection, err := db.Acquire(context.Background())
  22. if err != nil {
  23. fmt.Printf("无法获取连接:%v\n", err)
  24. os.Exit(1)
  25. }
  26. println("打开数据库")
  27. sql, err := goose.OpenDBWithDriver("pgx", *dsn)
  28. if err != nil {
  29. log.Fatalf(err.Error())
  30. }
  31. println("迁移")
  32. err = goose.Up(sql, "./migrations")
  33. if err != nil {
  34. log.Fatalf(err.Error())
  35. }
  36. defer connection.Release()
  37. defer db.Close()
  38. // 路由和其他代码...
  39. }

我检查了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:

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

Here is the shortened main.go:

  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "os"
  8. _ "github.com/jackc/pgx/v5"
  9. "github.com/pressly/goose/v3"
  10. )
  11. func main() {
  12. // DSN is the PostgreSQL Data Source Name, Database Source Name.
  13. // Often the same as connection string.
  14. dsn := flag.String("dsn", "postgres://postgres:password@localhost:5555/plazet", "PostgreSQL data source name")
  15. flag.Parse()
  16. db, err := openDbConnectionPool(*dsn)
  17. if err != nil {
  18. fmt.Printf("Unable to connect to database: %v\n", err)
  19. os.Exit(1)
  20. }
  21. // Ping the database to check if the connection is working
  22. connection, err := db.Acquire(context.Background())
  23. if err != nil {
  24. fmt.Printf("Unable to acquire connection: %v\n", err)
  25. os.Exit(1)
  26. }
  27. println("Opening DB")
  28. sql, err := goose.OpenDBWithDriver("pgx", *dsn)
  29. if err != nil {
  30. log.Fatalf(err.Error())
  31. }
  32. println("Migrating")
  33. err = goose.Up(sql, "./migrations")
  34. if err != nil {
  35. log.Fatalf(err.Error())
  36. }
  37. defer connection.Release()
  38. defer db.Close()
  39. // Code for router and stuff...
  40. }

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 包注册的(源代码):

  1. package stdlib
  2. // ...
  3. func init() {
  4. pgxDriver = &Driver{
  5. configs: make(map[string]*pgx.ConnConfig),
  6. }
  7. // 如果不同的 pgx 主要版本已经注册了 pgx 驱动程序,则我们跳过使用默认名称进行注册。
  8. if !contains(sql.Drivers(), "pgx") {
  9. sql.Register("pgx", pgxDriver)
  10. }
  11. sql.Register("pgx/v5", pgxDriver)
  12. // ...
  13. }

尝试导入该包:

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

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

  1. package stdlib
  2. // ...
  3. func init() {
  4. pgxDriver = &Driver{
  5. configs: make(map[string]*pgx.ConnConfig),
  6. }
  7. // if pgx driver was already registered by different pgx major version then we
  8. // skip registration under the default name.
  9. if !contains(sql.Drivers(), "pgx") {
  10. sql.Register("pgx", pgxDriver)
  11. }
  12. sql.Register("pgx/v5", pgxDriver)
  13. // ...

Try importing that package instead:

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

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:

确定