From b0d70a26d1ec69afdac65958b3ec7db5d3533ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=99=8D=EC=84=B1=EC=9A=B1?= <12810379+wookie0@users.noreply.github.com> Date: Thu, 14 Nov 2024 18:40:18 +0900 Subject: [PATCH] [#6372] Fixed nullable constraint bug for columns during auto migration (#7269) * [#6372] Fixed nullable constraint bug for columns during auto migration * [#6372] fix comment * [#6372] Add test code * [#6372] Add test code * [#6372] Fix failed test case * [#6372] Fix failed test case * [#6372] wip * [#6372] wip * [#6372] wip * [#6372] wip --- migrator/migrator.go | 4 ++-- tests/go.mod | 2 +- tests/migrate_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/migrator/migrator.go b/migrator/migrator.go index 189a141f..cec4e30f 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -524,8 +524,8 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy // check nullable if nullable, ok := columnType.Nullable(); ok && nullable == field.NotNull { - // not primary key & database is nullable - if !field.PrimaryKey && nullable { + // not primary key & current database is non-nullable(to be nullable) + if !field.PrimaryKey && !nullable { alterColumn = true } } diff --git a/tests/go.mod b/tests/go.mod index ba8a84a8..37286110 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -10,7 +10,7 @@ require ( gorm.io/driver/mysql v1.5.7 gorm.io/driver/postgres v1.5.9 gorm.io/driver/sqlite v1.5.6 - gorm.io/driver/sqlserver v1.5.3 + gorm.io/driver/sqlserver v1.5.4 gorm.io/gorm v1.25.12 ) diff --git a/tests/migrate_test.go b/tests/migrate_test.go index d955c8d7..6c8a4216 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -141,6 +141,48 @@ func TestAutoMigrateSelfReferential(t *testing.T) { } } +func TestAutoMigrateNullable(t *testing.T) { + type MigrateNullableColumn struct { + ID uint + Bonus float64 `gorm:"not null"` + Stock float64 + } + + DB.Migrator().DropTable(&MigrateNullableColumn{}) + + DB.AutoMigrate(&MigrateNullableColumn{}) + + type MigrateNullableColumn2 struct { + ID uint + Bonus float64 + Stock float64 `gorm:"not null"` + } + + if err := DB.Table("migrate_nullable_columns").AutoMigrate(&MigrateNullableColumn2{}); err != nil { + t.Fatalf("failed to auto migrate, got error: %v", err) + } + + columnTypes, err := DB.Table("migrate_nullable_columns").Migrator().ColumnTypes(&MigrateNullableColumn{}) + if err != nil { + t.Fatalf("failed to get column types, got error: %v", err) + } + + for _, columnType := range columnTypes { + switch columnType.Name() { + case "bonus": + // allow to change non-nullable to nullable + if nullable, _ := columnType.Nullable(); !nullable { + t.Fatalf("bonus's nullable should be true, bug got %t", nullable) + } + case "stock": + // do not allow to change nullable to non-nullable + if nullable, _ := columnType.Nullable(); !nullable { + t.Fatalf("stock's nullable should be true, bug got %t", nullable) + } + } + } +} + func TestSmartMigrateColumn(t *testing.T) { fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]