我是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/