gpt4 book ai didi

postgresql - 如何在 golang 中使用 mattes/migrate?

转载 作者:IT王子 更新时间:2023-10-29 02:22:22 26 4
gpt4 key购买 nike

我一直在尝试使用 mattes/migrate包,但我似乎无法让它真正做任何事情。数据库在 postgres 上运行,我通过 sqlx 与它交互.

我已经阅读了 github 上的自述文件,并应用了以下代码:

// use synchronous versions of migration functions ...
allErrors, ok := migrate.UpSync("postgres://<my_url_string>", "./app/database/migrations")
if !ok {
fmt.Println("Oh no ...")
// do sth with allErrors slice
}

我的架构是这样启动的:

//sqlx's initiated DB is imported in the database package

func SyncSchemas() {
database.DB.MustExec(schema)
}

var schema =
`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE IF NOT EXISTS examples (
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
deleted_at text DEFAULT NULL,
id UUID PRIMARY KEY UNIQUE NOT NULL DEFAULT uuid_generate_v4()
);`

type Example struct {
ID string `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
DeletedAt *time.Time `json:"deleted_at" db:"deleted_at"`
}

它运行没有错误,但目前它似乎只做这些。它不应该跟踪我的模式或其他什么吗?我以前用过 gorm作为我的 ORM,我只需要在模式上运行 autoMigrate(),它会在我更改模式时自动创建和应用迁移。我想要与 mattes/migrate 相同的功能,但我似乎找不到说明如何使用它的示例或教程。

我应该怎么做?

最佳答案

每当您使用 Golang 编写代码时,一个建议是检查错误以这种方式进行

if allErrors, ok := migrate.UpSync("postgres://<my_url_string>", "./migrations"); !ok {
fmt.Println("Oh no ...", ok)
// do sth with allErrors slice
}

因为我不知道你的错误是什么至少通过这种方式你可以检查你的错误是什么。

关于postgresql - 如何在 golang 中使用 mattes/migrate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42088562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com