fix: drop column with table str (#5795)

This commit is contained in:
black 2023-05-09 09:32:38 +08:00
parent e61b98d696
commit a1b7e47e75
2 changed files with 21 additions and 3 deletions

View File

@ -55,6 +55,8 @@ func (m Migrator) RunWithValue(value interface{}, fc func(*gorm.Statement) error
if table, ok := value.(string); ok {
stmt.Table = table
// set schema to avoid panic
stmt.Schema = &schema.Schema{}
} else if err := stmt.ParseWithSpecialTableName(value, stmt.Table); err != nil {
return err
}

View File

@ -616,7 +616,7 @@ func TestMigrateColumns(t *testing.T) {
}
if err := DB.Table("column_structs").Migrator().DropColumn(&NewColumnStruct{}, "NewName"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
t.Fatalf("Failed to drop column, got %v", err)
}
if DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "NewName") {
@ -628,7 +628,7 @@ func TestMigrateColumns(t *testing.T) {
}
if err := DB.Table("column_structs").Migrator().RenameColumn(&NewColumnStruct{}, "NewName", "new_new_name"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
t.Fatalf("Failed to rename column, got %v", err)
}
if !DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "new_new_name") {
@ -636,12 +636,28 @@ func TestMigrateColumns(t *testing.T) {
}
if err := DB.Table("column_structs").Migrator().DropColumn(&NewColumnStruct{}, "new_new_name"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
t.Fatalf("Failed to drop column, got %v", err)
}
if DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "new_new_name") {
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.Migrator().RenameColumn("column_structs", "new_name", "new_new_name"); err != nil {
t.Fatalf("Failed to rename column, got %v", err)
}
if !DB.Migrator().HasColumn("column_structs", "new_new_name") {
t.Fatalf("Failed to find added column")
}
if err := DB.Migrator().DropColumn("column_structs", "new_new_name"); err != nil {
t.Fatalf("Failed to drop column, got %v", err)
}
}
func TestMigrateConstraint(t *testing.T) {