go - 使用golang-migrate时数据库数据库版本错误

标签 go migration golang-migrate

我是golang-migrate的新用户。

我进行了一些成功执行的迁移。

我处于开发模式,因此我想重新运行迁移,因此在psql shell中连接到数据库后,我执行了drop database schema_migrations
现在的问题是,当我运行执行迁移的代码时(如下所示)


func RunMigrations() {
    m, err := migrate.New(
        "file://db/migrations",
        "postgres://postgres:postgres@localhost:5432/mydatabase?sslmode=disable")
    if err != nil {
        log.Fatal(err)
    }
    if err := m.Up(); err != nil {
        if err.Error() == "no change" {
            log.Println("no change made by migration scripts")
        } else {
            log.Fatal(err)
        }
    }
}

我得到这个错误

Dirty database version 2. Fix and force version.



这是什么错误,我该如何解决?

最佳答案

数据库版本2较脏意味着您尝试运行迁移v2,但失败。

如果迁移失败,数据库可能会不一致或损坏。

在中断状态之上重新运行其他迁移是不可预测的,因此迁移将被阻止,直到您清理数据库为止。

https://github.com/golang-migrate/migrate/blob/master/FAQ.md#what-does-dirty-database-mean

What does "dirty" database mean?

Before a migration runs, each database sets a dirty flag. Execution stops if a migration fails and the dirty state persists, which prevents attempts to run more migrations on top of a failed migration. You need to manually fix the error and then "force" the expected version.



清理数据库之后,您还可以打开schema_migrations表并将Dirty标志和回滚版本号更改为成功应用的上一次迁移。

关于go - 使用golang-migrate时数据库数据库版本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59616263/

相关文章:

php - 拉维尔 5.3 : Migrate step by step

dictionary - 在结构中声明和使用映射

sql - 如何在 Rails 的迁移中编写 SQL

go - 如何将数组中的配置项绑定(bind)到环境变量

go - 使用 Go 的 socket.io 客户端

Go:如何只解析日期到时间。时间?

c# - 如何在 Entity Framework Core 中创建基类和派生类组合的复合键

string - slice 显示在带有行和列的列表中

json - Golang 结构解码 xss

pointers - reflect如何获取返回的结构体指针