Skip to content

Commit

Permalink
update filename regex to match .generated.sql files (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryQ authored Aug 2, 2024
1 parent ef18dd2 commit cf9383c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/spanner/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var (
// 001.sql
// 001_name.sql
// 001_name.up.sql
migrationFileRegex = regexp.MustCompile(`^([0-9]+)(?:_([a-zA-Z0-9_\-]+))?(\.up)?\.sql$`)
// 001_name.generated.sql
migrationFileRegex = regexp.MustCompile(`^([0-9]+)(?:_([a-zA-Z0-9_\-]+))?(?:[.]up|[.]generated)?\.sql$`)

MigrationNameRegex = regexp.MustCompile(`[a-zA-Z0-9_\-]+`)

Expand Down Expand Up @@ -114,7 +115,7 @@ func LoadMigrations(dir string, toSkipSlice []uint, detectPartitionedDML bool) (
}

matches := migrationFileRegex.FindStringSubmatch(f.Name())
if len(matches) != 4 {
if matches == nil {
continue
}

Expand Down
37 changes: 37 additions & 0 deletions pkg/spanner/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package spanner
import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

const (
Expand Down Expand Up @@ -316,3 +318,38 @@ func Test_isPartitionedDMLOnly(t *testing.T) {
})
}
}

func Test_migrationFileRegex(t *testing.T) {
tests := map[string]struct {
input string
expected []string
}{
"NoName": {
input: "001.sql",
expected: []string{"001.sql", "001", ""},
},
"WithName": {
input: "001_name.sql",
expected: []string{"001_name.sql", "001", "name"},
},
"MatchAndIgnoreUp": {
input: "001_name.up.sql",
expected: []string{"001_name.up.sql", "001", "name"},
},
"MatchAndIgnoreGenerated": {
input: "001_name.generated.sql",
expected: []string{"001_name.generated.sql", "001", "name"},
},
"NotMatchDownMigration": {
input: "001_name.down.sql",
expected: nil,
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
matches := migrationFileRegex.FindStringSubmatch(tc.input)
assert.Equal(t, tc.expected, matches)
})
}
}

0 comments on commit cf9383c

Please sign in to comment.