我想使用golang-migrate
将一个sql文件迁移到我的postgresql数据库中。我可能做错了什么,但当我运行迁移命令时,它说找不到模式:
$ go run ./cmd/ migrate
2022/04/05 16:20:29 no scheme
exit status 1
这是代码:
// package dbschema contains the database schema, migrations, and seeding data.
package dbschema
import (
"context"
_ "embed" // Calls init function.
"fmt"
"log"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/jmoiron/sqlx"
"github.com/jonleopard/bootstrap/pkg/sys/database"
_ "github.com/lib/pq"
)
var (
//go:embed sql/000001_schema.up.sql
schemaDoc string
//go:embed sql/seed.sql
seedDoc string
)
// Migrate attempts to bring the schema for db up to date with the migrations
// defined in this package.
func Migrate(ctx context.Context, db *sqlx.DB) error {
if err := database.StatusCheck(ctx, db); err != nil {
return fmt.Errorf("status check database: %w", err)
}
driver, err := postgres.WithInstance(db.DB, &postgres.Config{})
if err != nil {
return fmt.Errorf("Construct Migrate driver: %w", err)
}
m, err := migrate.NewWithDatabaseInstance(schemaDoc, "postgres", driver)
if err != nil {
log.Fatal(err)
}
return m.Up()
}
m, err := migrate.NewWithInstance("iofs", d, "postgres", driver)
。 - Jeremie Weldin