Skip to content

Commit

Permalink
IsComplete helper to track when migrations are all done
Browse files Browse the repository at this point in the history
  • Loading branch information
dnnyjns committed Dec 22, 2019
1 parent 5d54484 commit cf08dee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func Create(dir, name string) {
}
}

func IsComplete(db *gorm.DB) bool {
return internalMigrations.IsComplete(db)
}

func Migrate(db *gorm.DB) error {
db.AutoMigrate(&Migration{})
return internalMigrations.Migrate(db)
Expand Down
15 changes: 15 additions & 0 deletions migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ import (

type migrations []*Migration

func (m *migrations) IsComplete(db *gorm.DB) bool {
var (
count int
total = len(*m)
ids = make([]string, total)
)

for i, migration := range *m {
ids[i] = migration.Version
}
db.Model(&Migration{}).Where(ids).Count(&count)

return total == count
}

func (m *migrations) Migrate(db *gorm.DB) error {
var (
ids = make([]string, len(*m))
Expand Down

0 comments on commit cf08dee

Please sign in to comment.