Skip to content

Commit

Permalink
Add source path to Go migrations with file on disk
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Nov 11, 2023
1 parent 91e20b2 commit c5e0d3c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 7 additions & 2 deletions provider_collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,13 @@ func merge(sources *fileSources, registerd map[int64]*Migration) ([]*Migration,
// This is almost always a user error.
var unregistered []string
for _, s := range sources.goSources {
if _, ok := registerd[s.Version]; !ok {
m, ok := registerd[s.Version]
if !ok {
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 {
Expand All @@ -151,7 +156,7 @@ func merge(sources *fileSources, registerd map[int64]*Migration) ([]*Migration,
if existing, ok := migrationLookup[version]; ok {
fullpath := r.Source
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",
version,
Expand Down
6 changes: 3 additions & 3 deletions provider_collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ func TestMerge(t *testing.T) {
check.NoError(t, err)
check.Number(t, len(migrations), 3)
assertMigration(t, migrations[0], newSource(TypeSQL, "00001_foo.sql", 1))
assertMigration(t, migrations[1], newSource(TypeGo, "", 2))
assertMigration(t, migrations[2], newSource(TypeGo, "", 3))
assertMigration(t, migrations[1], newSource(TypeGo, "00002_bar.go", 2))
assertMigration(t, migrations[2], newSource(TypeGo, "00003_baz.go", 3))
})
t.Run("unregistered_all", func(t *testing.T) {
_, err := merge(sources, nil)
Expand Down Expand Up @@ -280,7 +280,7 @@ func TestMerge(t *testing.T) {
check.NoError(t, err)
check.Number(t, len(migrations), 4)
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[3], newSource(TypeGo, "", 6))
})
Expand Down

0 comments on commit c5e0d3c

Please sign in to comment.