Skip to content

Commit

Permalink
use same var as pass it down
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed May 20, 2023
1 parent 9ac2ab8 commit 184cdde
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
7 changes: 3 additions & 4 deletions up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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)
}
Expand Down

0 comments on commit 184cdde

Please sign in to comment.