From 0bc69b74eaec70bb1800fce3b0eaa5f2206d69ee Mon Sep 17 00:00:00 2001 From: Billy Keyes Date: Tue, 18 Apr 2023 11:00:30 -0700 Subject: [PATCH] Fix FindMigrations panic with missing migrations 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. --- migrate/migrate.go | 8 +++----- migrate/testdata/gap/001_create_people.sql.example | 0 2 files changed, 3 insertions(+), 5 deletions(-) delete mode 100644 migrate/testdata/gap/001_create_people.sql.example diff --git a/migrate/migrate.go b/migrate/migrate.go index 619a022..d9fa6ab 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -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() { @@ -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) } } diff --git a/migrate/testdata/gap/001_create_people.sql.example b/migrate/testdata/gap/001_create_people.sql.example deleted file mode 100644 index e69de29..0000000