2020-05-23 18:50:48 +03:00
|
|
|
package tests_test
|
2020-02-22 14:41:01 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-05-30 17:27:20 +03:00
|
|
|
"github.com/jinzhu/gorm"
|
2020-05-23 18:50:48 +03:00
|
|
|
. "github.com/jinzhu/gorm/tests"
|
2020-02-22 14:41:01 +03:00
|
|
|
)
|
|
|
|
|
2020-05-23 18:50:48 +03:00
|
|
|
func TestMigrate(t *testing.T) {
|
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-05-23 18:50:48 +03:00
|
|
|
if err := DB.Migrator().DropTable(allModels...); err != nil {
|
2020-02-22 19:18:12 +03:00
|
|
|
t.Errorf("Failed to drop table, got error %v", err)
|
2020-02-22 15:57:29 +03:00
|
|
|
}
|
|
|
|
|
2020-05-23 18:50:48 +03:00
|
|
|
if err := DB.AutoMigrate(allModels...); err != nil {
|
2020-02-22 15:57:29 +03:00
|
|
|
t.Errorf("Failed to auto migrate, but got error %v", err)
|
|
|
|
}
|
2020-02-22 14:41:01 +03:00
|
|
|
|
|
|
|
for _, m := range allModels {
|
2020-05-23 18:50:48 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 17:27:20 +03:00
|
|
|
|
|
|
|
func TestIndexes(t *testing.T) {
|
2020-05-30 19:42:52 +03:00
|
|
|
type IndexStruct struct {
|
2020-05-30 17:27:20 +03:00
|
|
|
gorm.Model
|
2020-05-30 19:42:52 +03:00
|
|
|
Name string `gorm:"size:255;index"`
|
2020-05-30 17:27:20 +03:00
|
|
|
}
|
|
|
|
|
2020-05-30 19:42:52 +03:00
|
|
|
DB.Migrator().DropTable(&IndexStruct{})
|
|
|
|
DB.AutoMigrate(&IndexStruct{})
|
|
|
|
|
|
|
|
if err := DB.Migrator().DropIndex(&IndexStruct{}, "Name"); err != nil {
|
|
|
|
t.Errorf("Failed to drop index for user's name, got err %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Migrator().CreateIndex(&IndexStruct{}, "Name"); err != nil {
|
2020-05-30 17:27:20 +03:00
|
|
|
t.Errorf("Got error when tried to create index: %+v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-30 19:42:52 +03:00
|
|
|
if !DB.Migrator().HasIndex(&IndexStruct{}, "Name") {
|
2020-05-30 17:27:20 +03:00
|
|
|
t.Errorf("Failed to find index for user's name")
|
|
|
|
}
|
|
|
|
|
2020-05-30 19:42:52 +03:00
|
|
|
if err := DB.Migrator().DropIndex(&IndexStruct{}, "Name"); err != nil {
|
2020-05-30 17:27:20 +03:00
|
|
|
t.Errorf("Failed to drop index for user's name, got err %v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-30 19:42:52 +03:00
|
|
|
if DB.Migrator().HasIndex(&IndexStruct{}, "Name") {
|
2020-05-30 17:27:20 +03:00
|
|
|
t.Errorf("Should not find index for user's name after delete")
|
|
|
|
}
|
|
|
|
|
2020-05-30 19:42:52 +03:00
|
|
|
if err := DB.Migrator().CreateIndex(&IndexStruct{}, "Name"); err != nil {
|
2020-05-30 17:27:20 +03:00
|
|
|
t.Errorf("Got error when tried to create index: %+v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-30 19:42:52 +03:00
|
|
|
if err := DB.Migrator().RenameIndex(&IndexStruct{}, "idx_index_structs_name", "idx_users_name_1"); err != nil {
|
2020-05-30 17:27:20 +03:00
|
|
|
t.Errorf("no error should happen when rename index, but got %v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-30 19:42:52 +03:00
|
|
|
if !DB.Migrator().HasIndex(&IndexStruct{}, "idx_users_name_1") {
|
2020-05-30 17:27:20 +03:00
|
|
|
t.Errorf("Should find index for user's name after rename")
|
|
|
|
}
|
|
|
|
|
2020-05-30 19:42:52 +03:00
|
|
|
if err := DB.Migrator().DropIndex(&IndexStruct{}, "idx_users_name_1"); err != nil {
|
2020-05-30 17:27:20 +03:00
|
|
|
t.Errorf("Failed to drop index for user's name, got err %v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-30 19:42:52 +03:00
|
|
|
if DB.Migrator().HasIndex(&IndexStruct{}, "idx_users_name_1") {
|
2020-05-30 17:27:20 +03:00
|
|
|
t.Errorf("Should not find index for user's name after delete")
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 19:42:52 +03:00
|
|
|
|
|
|
|
func TestColumns(t *testing.T) {
|
|
|
|
type ColumnStruct struct {
|
|
|
|
gorm.Model
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
DB.Migrator().DropTable(&ColumnStruct{})
|
|
|
|
|
|
|
|
if err := DB.AutoMigrate(&ColumnStruct{}); err != nil {
|
|
|
|
t.Errorf("Failed to migrate, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type NewColumnStruct struct {
|
|
|
|
gorm.Model
|
|
|
|
Name string
|
|
|
|
NewName string
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Table("column_structs").Migrator().AddColumn(&NewColumnStruct{}, "NewName"); err != nil {
|
|
|
|
t.Errorf("Failed to add column, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "NewName") {
|
|
|
|
t.Errorf("Failed to find added column")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Table("column_structs").Migrator().DropColumn(&NewColumnStruct{}, "NewName"); err != nil {
|
|
|
|
t.Errorf("Failed to add column, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "NewName") {
|
|
|
|
t.Errorf("Found deleted column")
|
|
|
|
}
|
|
|
|
}
|