From fb843003796fbdefdd3404eabb8416613d516553 Mon Sep 17 00:00:00 2001 From: xormplus Date: Tue, 30 Jan 2018 13:24:28 +0800 Subject: [PATCH] Fix migrate migrationDidRun bug on postgres fix missing error handle --- migrate/migrate.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/migrate/migrate.go b/migrate/migrate.go index 7848b85..ea0560e 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -122,7 +122,10 @@ func (m *Migrate) RollbackLast() error { func (m *Migrate) getLastRunnedMigration() (*Migration, error) { for i := len(m.migrations) - 1; i >= 0; i-- { migration := m.migrations[i] - if m.migrationDidRun(migration) { + run, err := m.migrationDidRun(migration) + if err != nil { + return nil, err + } else if run { return migration, nil } } @@ -165,7 +168,12 @@ func (m *Migrate) runMigration(migration *Migration) error { return ErrMissingID } - if !m.migrationDidRun(migration) { + run, err :=m.migrationDidRun(migration) + if err != nil { + return err + } + + if !run { if err := migration.Migrate(m.db); err != nil { return err } @@ -193,11 +201,9 @@ func (m *Migrate) createMigrationTableIfNotExists() error { return nil } -func (m *Migrate) migrationDidRun(mig *Migration) bool { - row := m.db.DB().QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE %s = ?", m.options.TableName, m.options.IDColumnName), mig.ID) - var count int - row.Scan(&count) - return count > 0 +func (m *Migrate) migrationDidRun(mig *Migration) (bool, error) { + count, err := m.db.SQL(fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE %s = ?", m.options.TableName, m.options.IDColumnName), mig.ID).Count() + return count > 0, err } func (m *Migrate) isFirstRun() bool {