1

我一直在尝试使用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 具有相同的功能,但我似乎找不到显示如何使用它的示例或教程。

我该怎么做?

4

1 回答 1

0

每当您在 Golang 中编码时,一个建议就是以check the errors这种方式进行

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

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

于 2017-02-07T13:07:13.890 回答