Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enforce precedence for specific migrations #533

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions migration_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (mfs Migrations) Len() int {
}

func (mfs Migrations) Less(i, j int) bool {
if mfs[i].Version == mfs[j].Version {
// force "all" to the back
return mfs[i].DBType != "all"
}
return mfs[i].Version < mfs[j].Version
}

Expand Down
50 changes: 50 additions & 0 deletions migration_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package pop

import (
"github.com/stretchr/testify/assert"
"sort"
"testing"
)

func TestSortingMigrations(t *testing.T) {
t.Run("case=enforces precedence for specific migrations", func(t *testing.T) {
migrations := Migrations{
{
Version: "1",
DBType: "all",
},
{
Version: "1",
DBType: "postgres",
},
{
Version: "2",
DBType: "cockroach",
},
{
Version: "2",
DBType: "all",
},
{
Version: "3",
DBType: "all",
},
{
Version: "3",
DBType: "mysql",
},
}
expectedOrder := Migrations{
migrations[1],
migrations[0],
migrations[2],
migrations[3],
migrations[5],
migrations[4],
}

sort.Sort(migrations)

assert.Equal(t, expectedOrder, migrations)
})
}