From d49eb5cc2b7eb8d3a6a4b801e759f95c3d7b3d0a Mon Sep 17 00:00:00 2001 From: jawad-arb123 Date: Fri, 22 Aug 2025 12:31:42 +0100 Subject: [PATCH 1/4] fix Migrate nullable Test --- oracle/migrator.go | 44 +++++++++++++++++++++++++++++++++++++------ tests/migrate_test.go | 3 +-- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/oracle/migrator.go b/oracle/migrator.go index 49fef2b..9686a5f 100644 --- a/oracle/migrator.go +++ b/oracle/migrator.go @@ -308,15 +308,47 @@ func (m Migrator) DropColumn(value interface{}, name string) error { func (m Migrator) AlterColumn(value interface{}, field string) error { return m.RunWithValue(value, func(stmt *gorm.Statement) error { if stmt.Schema != nil { - if field := stmt.Schema.LookUpField(field); field != nil { - fileType := m.FullDataTypeOf(field) + if f := stmt.Schema.LookUpField(field); f != nil { + columnTypes, err := m.ColumnTypes(value) + if err != nil { + return err + } + + var currentNullable bool + var currentType string + for _, col := range columnTypes { + if strings.EqualFold(col.Name(), f.DBName) { + currentNullable, _ = col.Nullable() + currentType = strings.ToUpper(col.DatabaseTypeName()) + break + } + } + + desiredNullable := !f.NotNull + desiredType := strings.ToUpper(m.DataTypeOf(f)) + + // nullable → non-nullable → skip + if currentNullable && !desiredNullable { + return nil + } + + // same type + same nullability → skip + if currentNullable == desiredNullable && strings.Contains(currentType, desiredType) { + return nil + } + + sql := "ALTER TABLE ? MODIFY ? " + m.DataTypeOf(f) + if f.NotNull { + sql += " NOT NULL" + } else if !currentNullable && desiredNullable { + sql += " NULL" + } + return m.DB.Exec( - "ALTER TABLE ? MODIFY ? ?", + sql, clause.Table{Name: stmt.Schema.Table}, - clause.Column{Name: field.DBName}, - fileType, + clause.Column{Name: f.DBName}, ).Error - } } return fmt.Errorf("failed to look up field with name: %s", field) diff --git a/tests/migrate_test.go b/tests/migrate_test.go index a9683e9..37783d8 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -186,7 +186,6 @@ func TestAutoMigrateSelfReferential(t *testing.T) { } func TestAutoMigrateNullable(t *testing.T) { - t.Skip() type MigrateNullableColumn struct { ID uint Bonus float64 `gorm:"not null"` @@ -1397,7 +1396,7 @@ func TestMigrateExistingBoolColumnPG(t *testing.T) { func TestMigrateWithUniqueIndexAndUnique(t *testing.T) { t.Skip() - + const table = "unique_struct" checkField := func(model interface{}, fieldName string, unique bool, uniqueIndex string) { From a59654f5fde76a0c05fd6caf59c694e96a517c96 Mon Sep 17 00:00:00 2001 From: jawad-arb123 Date: Fri, 22 Aug 2025 12:32:42 +0100 Subject: [PATCH 2/4] uncomment TestAutoMigrateNullable --- tests/passed-tests.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/passed-tests.txt b/tests/passed-tests.txt index 6afac4d..559e1b1 100644 --- a/tests/passed-tests.txt +++ b/tests/passed-tests.txt @@ -163,7 +163,7 @@ TestSetAndGet TestMigrate TestAutoMigrateInt8PG TestAutoMigrateSelfReferential -#TestAutoMigrateNullable +TestAutoMigrateNullable TestSmartMigrateColumn TestMigrateWithColumnComment TestMigrateWithUniqueIndex From 84d3048a3f916eca7bd36fa341c0e62b5a18928c Mon Sep 17 00:00:00 2001 From: jawad-arb123 Date: Fri, 29 Aug 2025 21:42:12 +0100 Subject: [PATCH 3/4] Revert unintended changes after merge --- tests/benchmark_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/benchmark_test.go b/tests/benchmark_test.go index e28dfc3..dd77dbf 100644 --- a/tests/benchmark_test.go +++ b/tests/benchmark_test.go @@ -43,7 +43,7 @@ import ( "testing" . "github.com/oracle-samples/gorm-oracle/tests/utils" - + "gorm.io/gorm" ) func BenchmarkCreate(b *testing.B) { From 90353cbfb1c394a4f26c0c0c51b326308f1a3d6b Mon Sep 17 00:00:00 2001 From: jawad ARBAHI Date: Fri, 29 Aug 2025 21:46:27 +0100 Subject: [PATCH 4/4] Update benchmark_test.go --- tests/benchmark_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/benchmark_test.go b/tests/benchmark_test.go index dd77dbf..2fd37b7 100644 --- a/tests/benchmark_test.go +++ b/tests/benchmark_test.go @@ -42,8 +42,8 @@ import ( "fmt" "testing" - . "github.com/oracle-samples/gorm-oracle/tests/utils" "gorm.io/gorm" + . "github.com/oracle-samples/gorm-oracle/tests/utils" ) func BenchmarkCreate(b *testing.B) {