gorm/tests/migrate.go

30 lines
725 B
Go
Raw Normal View History

2020-02-22 14:41:01 +03:00
package tests
import (
2020-02-22 19:18:12 +03:00
"math/rand"
2020-02-22 14:41:01 +03:00
"testing"
2020-02-22 19:18:12 +03:00
"time"
2020-02-22 14:41:01 +03:00
"github.com/jinzhu/gorm"
)
func TestMigrate(t *testing.T, db *gorm.DB) {
2020-02-22 18:08:20 +03:00
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}}
2020-02-22 19:18:12 +03:00
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })
2020-02-22 14:41:01 +03:00
2020-02-22 19:18:12 +03:00
if err := db.Migrator().DropTable(allModels...); err != nil {
t.Errorf("Failed to drop table, got error %v", err)
2020-02-22 15:57:29 +03:00
}
if err := db.AutoMigrate(allModels...); err != nil {
t.Errorf("Failed to auto migrate, but got error %v", err)
}
2020-02-22 14:41:01 +03:00
for _, m := range allModels {
if !db.Migrator().HasTable(m) {
2020-02-22 15:57:29 +03:00
t.Errorf("Failed to create table for %#v", m)
2020-02-22 14:41:01 +03:00
}
}
}