我有一个简单的应用程序,使用golang-migrate/migrate和postgresql数据库。然而,当我调用
但我不确定我的sourceURL或databaseURL为什么会引起错误-我将sourceURL存储为
我的
然后,我的
这是我的
Migration
函数时,我认为我的migrate.New()
中的sourceURL或databaseURL是无效的内存地址或空指针,导致错误。但我不确定我的sourceURL或databaseURL为什么会引起错误-我将sourceURL存储为
file:///database/migration
,这是存储我的sql文件的目录,将databaseURL定义在我的Makefile中,如下所示:postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable
。我的
Makefile
如下:migrate:
migrate -source file://database/migration \
-database postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable up
rollback:
migrate -source file://database/migration \
-database postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable down
drop:
migrate -source file://database/migration \
-database postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable drop
migration:
migrate create -ext sql -dir database/migration go_graphql
run:
go run main.go
然后,我的
main.go
文件如下。func main() {
ctx := context.Background()
config := config.New()
db := pg.New(ctx, config)
println("HEL")
if err := db.Migration(); err != nil {
log.Fatal(err)
}
fmt.Println("Server started")
}
当我在main.go
中调用db.Migration
时,我遇到了错误。
func (db *DB) Migration() error {
m, err := migrate.New("file:///database/migration/", "postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable"))
println(m)
if err != nil {
// **I get error here!!**
return fmt.Errorf("error happened when migration")
}
if err := m.Up(); err != nil && err != migrate.ErrNoChange {
return fmt.Errorf("error when migration up: %v", err)
}
log.Println("migration completed!")
return err
}
这是我的
config.go
文件。其中DATABASE_URL
与postgres URL相同。type database struct {
URL string
}
type Config struct {
Database database
}
func New() *Config {
godotenv.Load()
return &Config{
Database: database{
URL: os.Getenv("DATABASE_URL"),
},
}
}
err
出现了错误,错误发生在迁移源驱动程序时:未知的驱动程序'file'(是否忘记导入?) - husky