From 286883adf54fb4d6e749d95a4596bebfe44d191d Mon Sep 17 00:00:00 2001 From: 1vn Date: Tue, 30 Oct 2018 16:51:07 -0400 Subject: [PATCH] add db fix as well --- dialect.go | 1 + fix.go | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/dialect.go b/dialect.go index edc2c35..1f21aa6 100644 --- a/dialect.go +++ b/dialect.go @@ -10,6 +10,7 @@ import ( type SQLDialect interface { createVersionTableSQL() string // sql string to create the db version table insertVersionSQL() string // sql string to insert the initial version table row + updateVersionSQL() string // sql string to update version dbVersionQuery(db *sql.DB) (*sql.Rows, error) } diff --git a/fix.go b/fix.go index abb9d19..caa9ff8 100644 --- a/fix.go +++ b/fix.go @@ -29,6 +29,12 @@ func Fix(db *sql.DB, dir string) error { version = last.Version + 1 } + // fix db table as well + tx, err := db.Begin() + if err != nil { + log.Fatal("db.Begin: ", err) + } + // fix filenames by replacing timestamps with sequential versions for _, tsm := range tsMigrations { oldPath := tsm.Source @@ -38,8 +44,13 @@ func Fix(db *sql.DB, dir string) error { return err } + if _, err := tx.Exec(GetDialect().updateVersionSQL(), version, tsm.Version); err != nil { + tx.Rollback() + return err + } + version++ } - return nil + return tx.Commit() }