diff --git a/up.go b/up.go index 6d92f581e..ab5a30630 100644 --- a/up.go +++ b/up.go @@ -70,7 +70,7 @@ func UpTo(db *sql.DB, dir string, version int64, opts ...OptionsFunc) error { lookupAppliedInDB[m.Version] = true } - missingMigrations := findMissingMigrations(dbMigrations, foundMigrations) + missingMigrations := findMissingMigrations(dbMigrations, foundMigrations, dbMaxVersion) // feature(mf): It is very possible someone may want to apply ONLY new migrations // and skip missing migrations altogether. At the moment this is not supported, @@ -181,15 +181,14 @@ func listAllDBVersions(ctx context.Context, db *sql.DB) (Migrations, error) { // findMissingMigrations migrations returns all missing migrations. // A migrations is considered missing if it has a version less than the // current known max version. -func findMissingMigrations(knownMigrations, newMigrations Migrations) Migrations { - max := knownMigrations[len(knownMigrations)-1].Version +func findMissingMigrations(knownMigrations, newMigrations Migrations, dbMaxVersion int64) Migrations { existing := make(map[int64]bool) for _, known := range knownMigrations { existing[known.Version] = true } var missing Migrations for _, new := range newMigrations { - if !existing[new.Version] && new.Version < max { + if !existing[new.Version] && new.Version < dbMaxVersion { missing = append(missing, new) } } diff --git a/up_test.go b/up_test.go index cb491ca72..63c8f2eeb 100644 --- a/up_test.go +++ b/up_test.go @@ -10,7 +10,7 @@ func TestFindMissingMigrations(t *testing.T) { {Version: 3}, {Version: 4}, {Version: 5}, - {Version: 7}, + {Version: 7}, // <-- database max version_id } new := Migrations{ {Version: 1}, @@ -22,7 +22,7 @@ func TestFindMissingMigrations(t *testing.T) { {Version: 7}, // <-- database max version_id {Version: 8}, // new migration } - got := findMissingMigrations(known, new) + got := findMissingMigrations(known, new, 7) if len(got) != 2 { t.Fatalf("invalid migration count: got:%d want:%d", len(got), 2) }