Skip to content

Commit 2a9de87

Browse files
zepatrikory-bot
authored andcommitted
fix: deduplicate down migrations
GitOrigin-RevId: 94c68daeded4f3b6f42d079d71415d8935a74e69
1 parent d84193b commit 2a9de87

File tree

4 files changed

+30
-45
lines changed

4 files changed

+30
-45
lines changed

oryx/popx/cmd.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,12 @@ func MigrateSQLDown(cmd *cobra.Command, p MigrationProvider) (err error) {
186186
// Now we need to rollback the last `steps` migrations that have a status of "Applied":
187187
var count int
188188
var rollingBack int
189-
var contents []string
190189
for i := len(status) - 1; i >= 0; i-- {
191190
if status[i].State == Applied {
192191
count++
193192
if steps > 0 && count <= steps {
194193
status[i].State = "Rollback"
195194
rollingBack++
196-
contents = append(contents, status[i].ContentDown)
197195
}
198196
}
199197
}

oryx/popx/migration_box.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/fs"
99
"regexp"
1010
"slices"
11+
"sort"
1112
"strings"
1213
"testing"
1314
"time"
@@ -267,10 +268,11 @@ func (mb *MigrationBox) findMigrations(
267268
})
268269

269270
// Sort descending.
270-
slices.SortFunc(mb.migrationsDown, func(a, b Migration) int { return -compareMigration(a, b) })
271+
sort.Sort(mb.migrationsDown)
272+
slices.Reverse(mb.migrationsDown)
271273

272274
// Sort ascending.
273-
slices.SortFunc(mb.migrationsUp, compareMigration)
275+
sort.Sort(mb.migrationsUp)
274276

275277
return err
276278
}

oryx/popx/migration_info.go

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,53 +54,38 @@ func (mfs Migrations) Less(i, j int) bool { return compareMigration(mfs[i], mfs[
5454
func (mfs Migrations) Swap(i, j int) { mfs[i], mfs[j] = mfs[j], mfs[i] }
5555

5656
func compareMigration(a, b Migration) int {
57-
if a.Version == b.Version {
58-
// Force "all" to be greater.
59-
if a.DBType == "all" && b.DBType != "all" {
60-
return 1
61-
} else if a.DBType != "all" && b.DBType == "all" {
62-
return -1
63-
} else {
64-
return strings.Compare(a.DBType, b.DBType)
65-
}
57+
if a.Version != b.Version {
58+
return strings.Compare(a.Version, b.Version)
59+
}
60+
// Force "all" to be greater.
61+
if a.DBType == "all" && b.DBType != "all" {
62+
return 1
63+
} else if a.DBType != "all" && b.DBType == "all" {
64+
return -1
6665
}
67-
return strings.Compare(a.Version, b.Version)
66+
return strings.Compare(a.DBType, b.DBType)
6867
}
6968

70-
func (mfs Migrations) sortAndFilter(dialect string, modifiers ...func(sort.Interface) sort.Interface) Migrations {
71-
// We need to sort mfs in order to push the dbType=="all" migrations
72-
// to the back.
73-
m := make(Migrations, len(mfs))
74-
copy(m, mfs)
75-
sort.Sort(m)
76-
77-
vsf := make(Migrations, 0, len(m))
78-
for k, v := range m {
79-
if v.DBType == "all" {
80-
// Add "all" only if we can not find a more specific migration for the dialect.
81-
var hasSpecific bool
82-
for kk, vv := range m {
83-
if v.Version == vv.Version && kk != k && vv.DBType == dialect {
84-
hasSpecific = true
85-
break
86-
}
69+
func (mfs Migrations) sortAndFilter(dialect string) Migrations {
70+
usable := make(map[string]Migration, len(mfs))
71+
for _, v := range mfs {
72+
if v.DBType == dialect {
73+
usable[v.Version] = v
74+
} else if v.DBType == "all" {
75+
// Add "all" only if we do not have a more specific migration for the dialect.
76+
// If a more specific migration is found later, it will override this one.
77+
if _, ok := usable[v.Version]; !ok {
78+
usable[v.Version] = v
8779
}
88-
89-
if !hasSpecific {
90-
vsf = append(vsf, v)
91-
}
92-
} else if v.DBType == dialect {
93-
vsf = append(vsf, v)
9480
}
9581
}
9682

97-
mod := sort.Interface(vsf)
98-
for _, m := range modifiers {
99-
mod = m(mod)
83+
filtered := make(Migrations, 0, len(usable))
84+
for k := range usable {
85+
filtered = append(filtered, usable[k])
10086
}
101-
102-
sort.Sort(mod)
103-
return vsf
87+
sort.Sort(filtered)
88+
return filtered
10489
}
10590

10691
func (mfs Migrations) find(version, dbType string) *Migration {

oryx/popx/migrator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"os"
1212
"regexp"
1313
"slices"
14-
"sort"
1514
"strings"
1615
"time"
1716

@@ -154,7 +153,8 @@ func (mb *MigrationBox) Down(ctx context.Context, steps int) (err error) {
154153
}
155154
steps = min(steps, count)
156155

157-
mfs := mb.migrationsDown.sortAndFilter(c.Dialect.Name(), sort.Reverse)
156+
mfs := mb.migrationsDown.sortAndFilter(c.Dialect.Name())
157+
slices.Reverse(mfs)
158158
if len(mfs) > count {
159159
// skip all migrations that were not yet applied
160160
mfs = mfs[len(mfs)-count:]

0 commit comments

Comments
 (0)