如何在golang中使用mattes / migrate?

时间:2017-02-07 11:21:35

标签: postgresql go migration sqlx

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

我该怎么做?

1 个答案:

答案 0 :(得分: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
}

由于我至少不知道你的错误是什么,所以你可以查看你的错误。

相关问题