How to do Migration using golang-migrate

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

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如下所示:

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 {
        // **我在这里遇到错误!!**
		return fmt.Errorf("迁移时发生错误")
	}
	if err := m.Up(); err != nil && err != migrate.ErrNoChange {
		return fmt.Errorf("迁移上时发生错误:%v", err)
	}

	log.Println("迁移完成!")
	return err
}

这是我的config.goDATABASE_URL与PostgreSQL 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"),
		},
	}
}
英文:

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

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

Then, my main.go is like below.

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")
}

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

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
}

Here is my config.go. DATABASE_URL is the same as 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"),
		},
	}
}

答案1

得分: 4

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

import (
  ....
  _ "github.com/golang-migrate/migrate/v4/source/file"
)

你可以在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:

import (
  ....
  _ "github.com/golang-migrate/migrate/v4/source/file"
)

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:

确定