How to do Migration using golang-migrate

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

How to do Migration using golang-migrate

问题

我有一个使用golang-migrate/migrate和PostgreSQL数据库的简单应用程序。然而,当我调用Migration函数时,我认为我的migrate.New()中的sourceURL或databaseURL是无效的内存地址或空指针,因此出现了错误。

但我不确定为什么我的sourceURL或databaseURL会引发错误-我将sourceURL存储为file:///database/migration,这是存储我的SQL文件的目录,将databaseURL存储为postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable,这在我的Makefile中定义。

我的Makefile如下所示:

  1. migrate:
  2. migrate -source file://database/migration \
  3. -database postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable up
  4. rollback:
  5. migrate -source file://database/migration \
  6. -database postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable down
  7. drop:
  8. migrate -source file://database/migration \
  9. -database postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable drop
  10. migration:
  11. migrate create -ext sql -dir database/migration go_graphql
  12. run:
  13. go run main.go

然后,我的main.go如下所示。

  1. func main() {
  2. ctx := context.Background()
  3. config := config.New()
  4. db := pg.New(ctx, config)
  5. println("HEL")
  6. if err := db.Migration(); err != nil {
  7. log.Fatal(err)
  8. }
  9. fmt.Println("Server started")
  10. }

当我在main.go中调用db.Migration时,我遇到了错误。

  1. func (db *DB) Migration() error {
  2. m, err := migrate.New("file:///database/migration/", "postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable"))
  3. println(m)
  4. if err != nil {
  5. // **我在这里遇到错误!!**
  6. return fmt.Errorf("迁移时发生错误")
  7. }
  8. if err := m.Up(); err != nil && err != migrate.ErrNoChange {
  9. return fmt.Errorf("迁移上时发生错误:%v", err)
  10. }
  11. log.Println("迁移完成!")
  12. return err
  13. }

这是我的config.goDATABASE_URL与PostgreSQL URL相同。

  1. type database struct {
  2. URL string
  3. }
  4. type Config struct {
  5. Database database
  6. }
  7. func New() *Config {
  8. godotenv.Load()
  9. return &Config{
  10. Database: database{
  11. URL: os.Getenv("DATABASE_URL"),
  12. },
  13. }
  14. }
英文:

I have a simple application using golang-migrate/migrate with postgresql database. However, I think I receive an error when I call Migration function since my either sourceURL or databaseURL in migrate.New() is invalid memory address or nil pointer.

But I am not sure why my sourceURL or databaseURL is causing error - I store sourceURL as file:///database/migration which is the directory where my sql file is stored, databaseURL as postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable which is defined in my Makefile.

My Makefile is like this

  1. migrate:
  2. migrate -source file://database/migration \
  3. -database postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable up
  4. rollback:
  5. migrate -source file://database/migration \
  6. -database postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable down
  7. drop:
  8. migrate -source file://database/migration \
  9. -database postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable drop
  10. migration:
  11. migrate create -ext sql -dir database/migration go_graphql
  12. run:
  13. go run main.go

Then, my main.go is like below.

  1. func main() {
  2. ctx := context.Background()
  3. config := config.New()
  4. db := pg.New(ctx, config)
  5. println("HEL")
  6. if err := db.Migration(); err != nil {
  7. log.Fatal(err)
  8. }
  9. fmt.Println("Server started")
  10. }

I got error when I call db.Migration in main.go

  1. func (db *DB) Migration() error {
  2. m, err := migrate.New("file:///database/migration/", "postgres://postgres:postgres@127.0.0.1:5432/go_graphql?sslmode=disable"))
  3. println(m)
  4. if err != nil {
  5. // **I get error here!!**
  6. return fmt.Errorf("error happened when migration")
  7. }
  8. if err := m.Up(); err != nil && err != migrate.ErrNoChange {
  9. return fmt.Errorf("error when migration up: %v", err)
  10. }
  11. log.Println("migration completed!")
  12. return err
  13. }

Here is my config.go. DATABASE_URL is the same as postgres URL

  1. type database struct {
  2. URL string
  3. }
  4. type Config struct {
  5. Database database
  6. }
  7. func New() *Config {
  8. godotenv.Load()
  9. return &Config{
  10. Database: database{
  11. URL: os.Getenv("DATABASE_URL"),
  12. },
  13. }
  14. }

答案1

得分: 4

根据你在评论中提供的错误信息“error happened when migration source driver: unknown driver 'file' (forgotten import?)”,我可以告诉你,正如错误信息所述,你忘记导入file

  1. import (
  2. ....
  3. _ "github.com/golang-migrate/migrate/v4/source/file"
  4. )

你可以在https://github.com/golang-migrate/migrate#use-in-your-go-project的“Want to use an existing database client?”部分中找到一个示例。

英文:

With the error you posted in the comments error happened when migration source driver: unknown driver 'file' (forgotten import?) I can tell you that, as stated, you forgot to import file:

  1. import (
  2. ....
  3. _ "github.com/golang-migrate/migrate/v4/source/file"
  4. )

You can see an example in https://github.com/golang-migrate/migrate#use-in-your-go-project in the section Want to use an existing database client?

huangapple
  • 本文由 发表于 2021年9月4日 17:31:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/69054061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定