Skip to content

Commit

Permalink
Correctly search persisted migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
dnnyjns committed Feb 20, 2020
1 parent cf08dee commit 2d221e1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func Create(dir, name string) {
panic(err)
}
f, err := os.Create(m.file())
defer f.Close()
if err != nil {
panic(err)
}
defer f.Close()
err = tmpl.Execute(f, *m)
if err != nil {
panic(err)
Expand Down
7 changes: 4 additions & 3 deletions migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ func (m *migrations) Migrate(db *gorm.DB) error {
db.Model(&Migration{}).Where(ids).Order("version").Find(&persisted)

m.sort()
length := len(persisted)
for _, migration := range *m {
length := len(persisted)
i := sort.Search(length, func(i int) bool { return persisted[i].Version == migration.Version })
if i == length {
version := migration.Version
i := sort.Search(length, func(i int) bool { return persisted[i].Version >= version })
if !(i < length && persisted[i].Version == version) {
migration.Migrate(db)
}
}
Expand Down

0 comments on commit 2d221e1

Please sign in to comment.