Add tests for Tables

This commit is contained in:
Jinzhu 2020-05-31 10:24:49 +08:00
parent 24285060d5
commit d81179557d
4 changed files with 102 additions and 16 deletions

View File

@ -23,6 +23,36 @@ func (m Migrator) HasTable(value interface{}) bool {
return count > 0 return count > 0
} }
func (m Migrator) RenameTable(oldName, newName interface{}) error {
var oldTable, newTable string
if v, ok := oldName.(string); ok {
oldTable = v
} else {
stmt := &gorm.Statement{DB: m.DB}
if err := stmt.Parse(oldName); err == nil {
oldTable = stmt.Table
} else {
return err
}
}
if v, ok := newName.(string); ok {
newTable = v
} else {
stmt := &gorm.Statement{DB: m.DB}
if err := stmt.Parse(newName); err == nil {
newTable = stmt.Table
} else {
return err
}
}
return m.DB.Exec(
"sp_rename @objname = ?, @newname = ?;",
clause.Table{Name: oldTable}, clause.Table{Name: newTable},
).Error
}
func (m Migrator) HasColumn(value interface{}, field string) bool { func (m Migrator) HasColumn(value interface{}, field string) bool {
var count int64 var count int64
m.RunWithValue(value, func(stmt *gorm.Statement) error { m.RunWithValue(value, func(stmt *gorm.Statement) error {

View File

@ -27,7 +27,7 @@ type Migrator interface {
CreateTable(dst ...interface{}) error CreateTable(dst ...interface{}) error
DropTable(dst ...interface{}) error DropTable(dst ...interface{}) error
HasTable(dst interface{}) bool HasTable(dst interface{}) bool
RenameTable(oldName, newName string) error RenameTable(oldName, newName interface{}) error
// Columns // Columns
AddColumn(dst interface{}, field string) error AddColumn(dst interface{}, field string) error

View File

@ -227,8 +227,31 @@ func (m Migrator) HasTable(value interface{}) bool {
return count > 0 return count > 0
} }
func (m Migrator) RenameTable(oldName, newName string) error { func (m Migrator) RenameTable(oldName, newName interface{}) error {
return m.DB.Exec("RENAME TABLE ? TO ?", oldName, newName).Error var oldTable, newTable string
if v, ok := oldName.(string); ok {
oldTable = v
} else {
stmt := &gorm.Statement{DB: m.DB}
if err := stmt.Parse(oldName); err == nil {
oldTable = stmt.Table
} else {
return err
}
}
if v, ok := newName.(string); ok {
newTable = v
} else {
stmt := &gorm.Statement{DB: m.DB}
if err := stmt.Parse(newName); err == nil {
newTable = stmt.Table
} else {
return err
}
}
return m.DB.Exec("ALTER TABLE ? RENAME TO ?", clause.Table{Name: oldTable}, clause.Table{Name: newTable}).Error
} }
func (m Migrator) AddColumn(value interface{}, field string) error { func (m Migrator) AddColumn(value interface{}, field string) error {

View File

@ -15,20 +15,53 @@ func TestMigrate(t *testing.T) {
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] }) rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })
if err := DB.Migrator().DropTable(allModels...); err != nil { if err := DB.Migrator().DropTable(allModels...); err != nil {
t.Errorf("Failed to drop table, got error %v", err) t.Fatalf("Failed to drop table, got error %v", err)
} }
if err := DB.AutoMigrate(allModels...); err != nil { if err := DB.AutoMigrate(allModels...); err != nil {
t.Errorf("Failed to auto migrate, but got error %v", err) t.Fatalf("Failed to auto migrate, but got error %v", err)
} }
for _, m := range allModels { for _, m := range allModels {
if !DB.Migrator().HasTable(m) { if !DB.Migrator().HasTable(m) {
t.Errorf("Failed to create table for %#v", m) t.Fatalf("Failed to create table for %#v", m)
} }
} }
} }
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")
}
}
func TestIndexes(t *testing.T) { func TestIndexes(t *testing.T) {
type IndexStruct struct { type IndexStruct struct {
gorm.Model gorm.Model
@ -39,43 +72,43 @@ func TestIndexes(t *testing.T) {
DB.AutoMigrate(&IndexStruct{}) DB.AutoMigrate(&IndexStruct{})
if err := DB.Migrator().DropIndex(&IndexStruct{}, "Name"); err != nil { if err := DB.Migrator().DropIndex(&IndexStruct{}, "Name"); err != nil {
t.Errorf("Failed to drop index for user's name, got err %v", err) t.Fatalf("Failed to drop index for user's name, got err %v", err)
} }
if err := DB.Migrator().CreateIndex(&IndexStruct{}, "Name"); err != nil { if err := DB.Migrator().CreateIndex(&IndexStruct{}, "Name"); err != nil {
t.Errorf("Got error when tried to create index: %+v", err) t.Fatalf("Got error when tried to create index: %+v", err)
} }
if !DB.Migrator().HasIndex(&IndexStruct{}, "Name") { if !DB.Migrator().HasIndex(&IndexStruct{}, "Name") {
t.Errorf("Failed to find index for user's name") t.Fatalf("Failed to find index for user's name")
} }
if err := DB.Migrator().DropIndex(&IndexStruct{}, "Name"); err != nil { if err := DB.Migrator().DropIndex(&IndexStruct{}, "Name"); err != nil {
t.Errorf("Failed to drop index for user's name, got err %v", err) t.Fatalf("Failed to drop index for user's name, got err %v", err)
} }
if DB.Migrator().HasIndex(&IndexStruct{}, "Name") { if DB.Migrator().HasIndex(&IndexStruct{}, "Name") {
t.Errorf("Should not find index for user's name after delete") t.Fatalf("Should not find index for user's name after delete")
} }
if err := DB.Migrator().CreateIndex(&IndexStruct{}, "Name"); err != nil { if err := DB.Migrator().CreateIndex(&IndexStruct{}, "Name"); err != nil {
t.Errorf("Got error when tried to create index: %+v", err) t.Fatalf("Got error when tried to create index: %+v", err)
} }
if err := DB.Migrator().RenameIndex(&IndexStruct{}, "idx_index_structs_name", "idx_users_name_1"); err != nil { if err := DB.Migrator().RenameIndex(&IndexStruct{}, "idx_index_structs_name", "idx_users_name_1"); err != nil {
t.Errorf("no error should happen when rename index, but got %v", err) t.Fatalf("no error should happen when rename index, but got %v", err)
} }
if !DB.Migrator().HasIndex(&IndexStruct{}, "idx_users_name_1") { if !DB.Migrator().HasIndex(&IndexStruct{}, "idx_users_name_1") {
t.Errorf("Should find index for user's name after rename") t.Fatalf("Should find index for user's name after rename")
} }
if err := DB.Migrator().DropIndex(&IndexStruct{}, "idx_users_name_1"); err != nil { if err := DB.Migrator().DropIndex(&IndexStruct{}, "idx_users_name_1"); err != nil {
t.Errorf("Failed to drop index for user's name, got err %v", err) t.Fatalf("Failed to drop index for user's name, got err %v", err)
} }
if DB.Migrator().HasIndex(&IndexStruct{}, "idx_users_name_1") { if DB.Migrator().HasIndex(&IndexStruct{}, "idx_users_name_1") {
t.Errorf("Should not find index for user's name after delete") t.Fatalf("Should not find index for user's name after delete")
} }
} }