Skip to content

Commit

Permalink
Fix FindMigrations panic with missing migrations
Browse files Browse the repository at this point in the history
If a migration was missing and the directory contained only valid
migration files, FindMigrations would panic with an index out of range
error instead of returning an error indicating a missing migration.

This was hidden in the tests because the test data included a
non-migration file that added an extra element to the array.
  • Loading branch information
bluekeyes committed Apr 18, 2023
1 parent 6264dab commit 0bc69b7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func FindMigrations(fsys fs.FS) ([]string, error) {
}

paths := make([]string, 0, len(fileInfos))
foundMigrations := make([]bool, len(fileInfos))

for _, fi := range fileInfos {
if fi.IsDir() {
Expand All @@ -123,17 +122,16 @@ func FindMigrations(fsys fs.FS) ([]string, error) {
return nil, err
}

if foundMigrations[n-1] {
if n-1 < int64(len(paths)) && paths[n-1] != "" {
return nil, fmt.Errorf("Duplicate migration %d", n)
}

// Set at specific index, so that paths are properly sorted
paths = setAt(paths, fi.Name(), n-1)
foundMigrations[n-1] = true
}

for i := range paths {
if !foundMigrations[i] {
for i, path := range paths {
if path == "" {
return nil, fmt.Errorf("Missing migration %d", i+1)
}
}
Expand Down
Empty file.

0 comments on commit 0bc69b7

Please sign in to comment.