Add source path to Go migrations with file on disk

pull/643/head
Mike Fridman 2023-11-11 10:24:25 -05:00
parent 91e20b290a
commit c5e0d3cffc
No known key found for this signature in database
2 changed files with 10 additions and 5 deletions

View File

@ -133,8 +133,13 @@ func merge(sources *fileSources, registerd map[int64]*Migration) ([]*Migration,
// This is almost always a user error. // This is almost always a user error.
var unregistered []string var unregistered []string
for _, s := range sources.goSources { for _, s := range sources.goSources {
if _, ok := registerd[s.Version]; !ok { m, ok := registerd[s.Version]
if !ok {
unregistered = append(unregistered, s.Path) unregistered = append(unregistered, s.Path)
} else {
// Populate the source path for registered Go migrations that have a corresponding file
// on disk.
m.Source = s.Path
} }
} }
if len(unregistered) > 0 { if len(unregistered) > 0 {
@ -151,7 +156,7 @@ func merge(sources *fileSources, registerd map[int64]*Migration) ([]*Migration,
if existing, ok := migrationLookup[version]; ok { if existing, ok := migrationLookup[version]; ok {
fullpath := r.Source fullpath := r.Source
if fullpath == "" { if fullpath == "" {
fullpath = "manually registered (no source)" fullpath = "no source path"
} }
return nil, fmt.Errorf("found duplicate migration version %d:\n\texisting:%v\n\tcurrent:%v", return nil, fmt.Errorf("found duplicate migration version %d:\n\texisting:%v\n\tcurrent:%v",
version, version,

View File

@ -208,8 +208,8 @@ func TestMerge(t *testing.T) {
check.NoError(t, err) check.NoError(t, err)
check.Number(t, len(migrations), 3) check.Number(t, len(migrations), 3)
assertMigration(t, migrations[0], newSource(TypeSQL, "00001_foo.sql", 1)) assertMigration(t, migrations[0], newSource(TypeSQL, "00001_foo.sql", 1))
assertMigration(t, migrations[1], newSource(TypeGo, "", 2)) assertMigration(t, migrations[1], newSource(TypeGo, "00002_bar.go", 2))
assertMigration(t, migrations[2], newSource(TypeGo, "", 3)) assertMigration(t, migrations[2], newSource(TypeGo, "00003_baz.go", 3))
}) })
t.Run("unregistered_all", func(t *testing.T) { t.Run("unregistered_all", func(t *testing.T) {
_, err := merge(sources, nil) _, err := merge(sources, nil)
@ -280,7 +280,7 @@ func TestMerge(t *testing.T) {
check.NoError(t, err) check.NoError(t, err)
check.Number(t, len(migrations), 4) check.Number(t, len(migrations), 4)
assertMigration(t, migrations[0], newSource(TypeSQL, "00001_foo.sql", 1)) assertMigration(t, migrations[0], newSource(TypeSQL, "00001_foo.sql", 1))
assertMigration(t, migrations[1], newSource(TypeGo, "", 2)) assertMigration(t, migrations[1], newSource(TypeGo, "00002_bar.go", 2))
assertMigration(t, migrations[2], newSource(TypeGo, "", 3)) assertMigration(t, migrations[2], newSource(TypeGo, "", 3))
assertMigration(t, migrations[3], newSource(TypeGo, "", 6)) assertMigration(t, migrations[3], newSource(TypeGo, "", 6))
}) })