diff --git a/migration_info.go b/migration_info.go index e8361672..9ca60200 100644 --- a/migration_info.go +++ b/migration_info.go @@ -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 } diff --git a/migration_info_test.go b/migration_info_test.go new file mode 100644 index 00000000..0f76824e --- /dev/null +++ b/migration_info_test.go @@ -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) + }) +}