golang-migrate 无法找到 postgres 驱动程序
golang-migrate unable to find postgres driver
在我的internal/platform/database/database.go
import (
"github.com/golang-migrate/migrate"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
func RunMigrations() error {
m, err := migrate.New(
"file://schema",
"postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable")
if err != nil {
return errors.Wrap(err, "error creating migrations object")
}
这个函数是从我的 cmd/my-api/main.go
中调用的,如下所示:
import (
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/myrepo/myproject/internal/platform/database"
)
// =========================================================================
// Executing migrations
if err := database.RunMigrations(); err != nil {
log.Fatal(err)
}
虽然我在 两个 文件中导入 postgres
驱动程序,_ "github.com/lib/pq"
运行程序失败如下:
error creating migrations object: source driver: unknown driver file (forgotten import?)
exit status 1
这是为什么?
看来golang-migrate
需要自己的对应驱动版本(?)
下面的导入帮我解决了这个问题
_ "github.com/golang-migrate/migrate/v4/database/postgres"
当您导入以下内容时,将触发 postgres 驱动程序初始化函数,并且该函数会注册 postgres 驱动程序。
_ "github.com/golang-migrate/migrate/v4/database/postgres"
你可以检查这个。
https://www.calhoun.io/why-we-import-sql-drivers-with-the-blank-identifier/
在我的internal/platform/database/database.go
import (
"github.com/golang-migrate/migrate"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
func RunMigrations() error {
m, err := migrate.New(
"file://schema",
"postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable")
if err != nil {
return errors.Wrap(err, "error creating migrations object")
}
这个函数是从我的 cmd/my-api/main.go
中调用的,如下所示:
import (
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/myrepo/myproject/internal/platform/database"
)
// =========================================================================
// Executing migrations
if err := database.RunMigrations(); err != nil {
log.Fatal(err)
}
虽然我在 两个 文件中导入 postgres
驱动程序,_ "github.com/lib/pq"
运行程序失败如下:
error creating migrations object: source driver: unknown driver file (forgotten import?)
exit status 1
这是为什么?
看来golang-migrate
需要自己的对应驱动版本(?)
下面的导入帮我解决了这个问题
_ "github.com/golang-migrate/migrate/v4/database/postgres"
当您导入以下内容时,将触发 postgres 驱动程序初始化函数,并且该函数会注册 postgres 驱动程序。
_ "github.com/golang-migrate/migrate/v4/database/postgres"
你可以检查这个。 https://www.calhoun.io/why-we-import-sql-drivers-with-the-blank-identifier/