Skip to content

Commit 71e6385

Browse files
hperlory-bot
authored andcommitted
chore: improve readability of popx.MigrationBox
GitOrigin-RevId: e7e413764d7e4084f2d9ee4313e5f1405c807a10
1 parent c760183 commit 71e6385

File tree

3 files changed

+56
-26
lines changed

3 files changed

+56
-26
lines changed

oryx/popx/match.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ var MigrationFileRegexp = regexp.MustCompile(
1515
`^(\d+)_([^.]+)(\.[a-z0-9]+)?(\.autocommit)?\.(up|down)\.(sql)$`,
1616
)
1717

18+
const (
19+
// Human-readable constants for the regex capture groups
20+
versionIdx = iota + 1
21+
nameIdx
22+
dbTypeIdx
23+
autocommitIdx
24+
directionIdx
25+
typeIdx
26+
)
27+
1828
// match holds the information parsed from a migration filename.
1929
type match struct {
2030
Version string
@@ -33,37 +43,40 @@ func parseMigrationFilename(filename string) (*match, error) {
3343
}
3444
m := matches[0]
3545

36-
var autocommit bool
37-
var dbType string
38-
if m[3] == ".autocommit" {
46+
var (
47+
autocommit bool
48+
dbType string
49+
)
50+
51+
if m[dbTypeIdx] == ".autocommit" {
3952
// A special case where autocommit group moves forward to the 3rd index.
4053
autocommit = true
4154
dbType = "all"
42-
} else if m[3] == "" {
55+
} else if m[dbTypeIdx] == "" {
4356
dbType = "all"
4457
} else {
45-
dbType = pop.CanonicalDialect(m[3][1:])
58+
dbType = pop.CanonicalDialect(m[dbTypeIdx][1:])
4659
if !pop.DialectSupported(dbType) {
4760
return nil, errors.Errorf("unsupported dialect %s", dbType)
4861
}
4962
}
5063

51-
if m[6] == "fizz" && dbType != "all" {
64+
if m[typeIdx] == "fizz" && dbType != "all" {
5265
return nil, errors.Errorf("invalid database type %q, expected \"all\" because fizz is database type independent", dbType)
5366
}
5467

55-
if m[4] == ".autocommit" {
68+
if m[autocommitIdx] == ".autocommit" {
5669
autocommit = true
57-
} else if m[4] != "" {
58-
return nil, errors.Errorf("invalid autocommit flag %q", m[4])
70+
} else if m[autocommitIdx] != "" {
71+
return nil, errors.Errorf("invalid autocommit flag %q", m[autocommitIdx])
5972
}
6073

6174
return &match{
62-
Version: m[1],
63-
Name: m[2],
75+
Version: m[versionIdx],
76+
Name: m[nameIdx],
6477
DBType: dbType,
6578
Autocommit: autocommit,
66-
Direction: m[5],
67-
Type: m[6],
79+
Direction: m[directionIdx],
80+
Type: m[typeIdx],
6881
}, nil
6982
}

oryx/popx/migration_box.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func WithTestdata(t *testing.T, testdata fs.FS) MigrationBoxOption {
108108
flavor = pop.CanonicalDialect(strings.TrimPrefix(match[2], "."))
109109
}
110110

111-
//t.Logf("Found test migration \"%s\" (%s, %+v): %s", flavor, match, err, info.Name())
111+
// t.Logf("Found test migration \"%s\" (%s, %+v): %s", flavor, match, err, info.Name())
112112

113113
m.migrationsUp = append(m.migrationsUp, Migration{
114114
Version: version + "9", // run testdata after version

oryx/popx/migration_info.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,40 @@ func compareMigration(a, b Migration) int {
6363
return strings.Compare(a.DBType, b.DBType)
6464
}
6565

66+
// specificity returns an integer representing how specific the migration is.
67+
// Higher numbers indicate a more specific migration.
68+
func specificity(m *Migration) int {
69+
specificity := 0
70+
if m.DBType != "all" {
71+
specificity += 1
72+
}
73+
74+
return specificity
75+
}
76+
77+
// isApplicable checks whether the migration is applicable for the given dialect.
78+
func isApplicable(m Migration, dialect string) bool {
79+
return m.DBType == dialect || m.DBType == "all"
80+
}
81+
6682
func (mfs Migrations) sortAndFilter(dialect string) Migrations {
67-
usable := make(map[string]Migration, len(mfs))
68-
for _, v := range mfs {
69-
if v.DBType == dialect {
70-
usable[v.Version] = v
71-
} else if v.DBType == "all" {
72-
// Add "all" only if we do not have a more specific migration for the dialect.
73-
// If a more specific migration is found later, it will override this one.
74-
if _, ok := usable[v.Version]; !ok {
75-
usable[v.Version] = v
83+
byVersion := make(map[string]Migration, len(mfs))
84+
for _, migration := range mfs {
85+
if !isApplicable(migration, dialect) {
86+
continue
87+
}
88+
if previousMigration, ok := byVersion[migration.Version]; ok {
89+
if specificity(&migration) < specificity(&previousMigration) {
90+
// Previous migration is more specific, skip this one.
91+
continue
7692
}
7793
}
94+
byVersion[migration.Version] = migration
7895
}
7996

80-
filtered := make(Migrations, 0, len(usable))
81-
for k := range usable {
82-
filtered = append(filtered, usable[k])
97+
filtered := make(Migrations, 0, len(byVersion))
98+
for k := range byVersion {
99+
filtered = append(filtered, byVersion[k])
83100
}
84101
sort.Sort(filtered)
85102
return filtered

0 commit comments

Comments
 (0)