Merge 274791f730abea90b61b9de3ec656fbe554e1c46 into 725aa5b5ff4c0687b06d9a01096b8e4cf96b6c9e

This commit is contained in:
general252 2025-08-18 20:12:01 +08:00 committed by GitHub
commit fd379f06e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 2 deletions

View File

@ -559,9 +559,9 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
case schema.Bool:
v1, _ := strconv.ParseBool(dv)
v2, _ := strconv.ParseBool(field.DefaultValue)
alterColumn = v1 != v2
alterColumn = alterColumn || (v1 != v2)
default:
alterColumn = dv != field.DefaultValue
alterColumn = alterColumn || (dv != field.DefaultValue)
}
}
}

View File

@ -801,6 +801,45 @@ func TestMigrateColumns(t *testing.T) {
}
}
func TestMigrateColumnUpdateSize(t *testing.T) {
sqlite := DB.Dialector.Name() == "sqlite"
type UpdateColumnSizeStruct struct {
gorm.Model
Name string `gorm:"column:name;default:'';size:16"`
}
_ = DB.Migrator().DropTable(&UpdateColumnSizeStruct{})
if err := DB.AutoMigrate(&UpdateColumnSizeStruct{}); err != nil {
t.Errorf("Failed to migrate, got %v", err)
}
type UpdateColumnSizeStruct2 struct {
gorm.Model
Name string `gorm:"column:name;default:'';size:100"` // size change 16 -> 100
}
if err := DB.Table("update_column_size_structs").AutoMigrate(&UpdateColumnSizeStruct2{}); err != nil {
t.Fatalf("no error should happened when auto migrate column, but got %v", err)
}
if columnTypes, err := DB.Migrator().ColumnTypes(&UpdateColumnSizeStruct{}); err != nil {
t.Fatalf("no error should returns for ColumnTypes")
} else {
for _, columnType := range columnTypes {
if columnType.Name() == "name" {
if length, ok := columnType.Length(); !sqlite && (!ok || length != 100) {
t.Fatalf("column name length should be correct, name: %v, length: %v, expects: %v, column: %#v",
columnType.Name(), length, 100, columnType)
}
}
}
}
}
func TestMigrateConstraint(t *testing.T) {
names := []string{"Account", "fk_users_account", "Pets", "fk_users_pets", "Company", "fk_users_company", "Team", "fk_users_team", "Languages", "fk_users_languages"}