gorm/tests/migrate_test.go

195 lines
5.5 KiB
Go
Raw Normal View History

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-05-31 05:38:01 +03:00
"strings"
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-05-31 05:24:49 +03:00
t.Fatalf("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-05-31 05:24:49 +03:00
t.Fatalf("Failed to auto migrate, but got error %v", err)
2020-02-22 15:57:29 +03:00
}
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-05-31 05:24:49 +03:00
t.Fatalf("Failed to create table for %#v", m)
2020-02-22 14:41:01 +03:00
}
}
}
2020-05-30 17:27:20 +03:00
2020-05-31 05:24:49 +03:00
func TestTable(t *testing.T) {
type TableStruct struct {
gorm.Model
Name string
}
DB.Migrator().DropTable(&TableStruct{})
DB.AutoMigrate(&TableStruct{})
if !DB.Migrator().HasTable(&TableStruct{}) {
t.Fatalf("should found created table")
}
type NewTableStruct struct {
gorm.Model
Name string
}
if err := DB.Migrator().RenameTable(&TableStruct{}, &NewTableStruct{}); err != nil {
t.Fatalf("Failed to rename table, got error %v", err)
}
if !DB.Migrator().HasTable("new_table_structs") {
t.Fatal("should found renamed table")
}
DB.Migrator().DropTable("new_table_structs")
if DB.Migrator().HasTable(&NewTableStruct{}) {
t.Fatal("should not found droped table")
}
}
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 {
2020-05-31 05:24:49 +03:00
t.Fatalf("Failed to drop index for user's name, got err %v", err)
2020-05-30 19:42:52 +03:00
}
if err := DB.Migrator().CreateIndex(&IndexStruct{}, "Name"); err != nil {
2020-05-31 05:24:49 +03:00
t.Fatalf("Got error when tried to create index: %+v", err)
2020-05-30 17:27:20 +03:00
}
2020-05-30 19:42:52 +03:00
if !DB.Migrator().HasIndex(&IndexStruct{}, "Name") {
2020-05-31 05:24:49 +03:00
t.Fatalf("Failed to find index for user's name")
2020-05-30 17:27:20 +03:00
}
2020-05-30 19:42:52 +03:00
if err := DB.Migrator().DropIndex(&IndexStruct{}, "Name"); err != nil {
2020-05-31 05:24:49 +03:00
t.Fatalf("Failed to drop index for user's name, got err %v", err)
2020-05-30 17:27:20 +03:00
}
2020-05-30 19:42:52 +03:00
if DB.Migrator().HasIndex(&IndexStruct{}, "Name") {
2020-05-31 05:24:49 +03:00
t.Fatalf("Should not find index for user's name after delete")
2020-05-30 17:27:20 +03:00
}
2020-05-30 19:42:52 +03:00
if err := DB.Migrator().CreateIndex(&IndexStruct{}, "Name"); err != nil {
2020-05-31 05:24:49 +03:00
t.Fatalf("Got error when tried to create index: %+v", err)
2020-05-30 17:27:20 +03:00
}
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-31 05:24:49 +03:00
t.Fatalf("no error should happen when rename index, but got %v", err)
2020-05-30 17:27:20 +03:00
}
2020-05-30 19:42:52 +03:00
if !DB.Migrator().HasIndex(&IndexStruct{}, "idx_users_name_1") {
2020-05-31 05:24:49 +03:00
t.Fatalf("Should find index for user's name after rename")
2020-05-30 17:27:20 +03:00
}
2020-05-30 19:42:52 +03:00
if err := DB.Migrator().DropIndex(&IndexStruct{}, "idx_users_name_1"); err != nil {
2020-05-31 05:24:49 +03:00
t.Fatalf("Failed to drop index for user's name, got err %v", err)
2020-05-30 17:27:20 +03:00
}
2020-05-30 19:42:52 +03:00
if DB.Migrator().HasIndex(&IndexStruct{}, "idx_users_name_1") {
2020-05-31 05:24:49 +03:00
t.Fatalf("Should not find index for user's name after delete")
2020-05-30 17:27:20 +03:00
}
}
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)
}
2020-05-31 05:38:01 +03:00
type ColumnStruct2 struct {
gorm.Model
Name string `gorm:"size:100"`
}
if err := DB.Table("column_structs").Migrator().AlterColumn(&ColumnStruct2{}, "Name"); err != nil {
t.Fatalf("no error should happend when alter column, but got %v", err)
}
if columnTypes, err := DB.Migrator().ColumnTypes(&ColumnStruct{}); err != nil {
t.Fatalf("no error should returns for ColumnTypes")
} else {
stmt := &gorm.Statement{DB: DB}
stmt.Parse(&ColumnStruct2{})
for _, columnType := range columnTypes {
if columnType.Name() == "name" {
dataType := DB.Dialector.DataTypeOf(stmt.Schema.LookUpField(columnType.Name()))
if !strings.Contains(strings.ToUpper(dataType), strings.ToUpper(columnType.DatabaseTypeName())) {
t.Errorf("column type should be correct, name: %v, length: %v, expects: %v", columnType.Name(), columnType.DatabaseTypeName(), dataType)
}
}
}
}
2020-05-30 19:42:52 +03:00
type NewColumnStruct struct {
gorm.Model
Name string
NewName string
}
if err := DB.Table("column_structs").Migrator().AddColumn(&NewColumnStruct{}, "NewName"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
2020-05-30 19:42:52 +03:00
}
if !DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "NewName") {
t.Fatalf("Failed to find added column")
2020-05-30 19:42:52 +03:00
}
if err := DB.Table("column_structs").Migrator().DropColumn(&NewColumnStruct{}, "NewName"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
2020-05-30 19:42:52 +03:00
}
if DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "NewName") {
t.Fatalf("Found deleted column")
}
if err := DB.Table("column_structs").Migrator().AddColumn(&NewColumnStruct{}, "NewName"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
}
if err := DB.Table("column_structs").Migrator().RenameColumn(&NewColumnStruct{}, "NewName", "new_new_name"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
}
if !DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "new_new_name") {
t.Fatalf("Failed to found renamed column")
}
if err := DB.Table("column_structs").Migrator().DropColumn(&NewColumnStruct{}, "new_new_name"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
}
if DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "new_new_name") {
t.Fatalf("Found deleted column")
2020-05-30 19:42:52 +03:00
}
}