英文:
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
驱动程序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"
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论