Update migrate_test.go
This commit is contained in:
parent
8a9fd3feca
commit
3628a21ece
@ -6,7 +6,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/juliangruber/go-intersect"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
. "gorm.io/gorm/utils/tests"
|
. "gorm.io/gorm/utils/tests"
|
||||||
)
|
)
|
||||||
@ -24,14 +23,22 @@ func TestMigrate(t *testing.T) {
|
|||||||
if err := DB.AutoMigrate(allModels...); err != nil {
|
if err := DB.AutoMigrate(allModels...); err != nil {
|
||||||
t.Fatalf("Failed to auto migrate, but got error %v", err)
|
t.Fatalf("Failed to auto migrate, but got error %v", err)
|
||||||
}
|
}
|
||||||
tableList, tableErr := DB.Migrator().GetTables()
|
|
||||||
if tableErr != nil {
|
if tables, err := DB.Migrator().GetTables(); err != nil {
|
||||||
t.Fatalf("Failed to get database all tables, but got error %v", tableErr)
|
t.Fatalf("Failed to get database all tables, but got error %v", err)
|
||||||
}
|
} else {
|
||||||
//get auto Migrator create tables and databases tables intersection
|
for _, t1 := range []string{"users", "accounts", "pets", "companies", "toys", "languages"} {
|
||||||
intersectList := intersect.Simple(tableList, []string{"users", "accounts", "pets", "companies", "toys", "languages"})
|
hasTable := false
|
||||||
if len(intersectList) != len(allModels) {
|
for _, t2 := range tables {
|
||||||
t.Fatalf("Failed to get auto Migrator create tables and databases tables intersection nums not eq allModels %d intersectList %d", len(allModels), len(intersectList))
|
if t2 == t1 {
|
||||||
|
hasTable = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !hasTable {
|
||||||
|
t.Fatalf("Failed to get table %v when GetTables", t1)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, m := range allModels {
|
for _, m := range allModels {
|
||||||
@ -63,6 +70,25 @@ func TestMigrate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAutoMigrateSelfReferential(t *testing.T) {
|
||||||
|
type MigratePerson struct {
|
||||||
|
ID uint
|
||||||
|
Name string
|
||||||
|
ManagerID *uint
|
||||||
|
Manager *MigratePerson
|
||||||
|
}
|
||||||
|
|
||||||
|
DB.Migrator().DropTable(&MigratePerson{})
|
||||||
|
|
||||||
|
if err := DB.AutoMigrate(&MigratePerson{}); err != nil {
|
||||||
|
t.Fatalf("Failed to auto migrate, but got error %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !DB.Migrator().HasConstraint("migrate_people", "fk_migrate_people_manager") {
|
||||||
|
t.Fatalf("Failed to find has one constraint between people and managers")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSmartMigrateColumn(t *testing.T) {
|
func TestSmartMigrateColumn(t *testing.T) {
|
||||||
fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]
|
fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user