Skip to content

Commit

Permalink
fix: enforce precedence for specific migrations (#533)
Browse files Browse the repository at this point in the history
closes #532
  • Loading branch information
zepatrik authored Apr 7, 2020
1 parent 2aad8bd commit 3b189e2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
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)
})
}

0 comments on commit 3b189e2

Please sign in to comment.