Skip to content

Commit

Permalink
[#6372] wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wookie0 committed Nov 8, 2024
1 parent 60f2107 commit d148cb2
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 deletions tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,55 @@ func TestAutoMigrateSelfReferential(t *testing.T) {
}
}

func TestAutoMigrateNullable(t *testing.T) {
type UserMigrateColumn struct {
ID uint
Bonus float64 `gorm:"not null"`
Stock float64
}

DB.Migrator().DropTable(&UserMigrateColumn{})

DB.AutoMigrate(&UserMigrateColumn{})

type UserMigrateColumn2 struct {
ID uint
Bonus float64
Stock float64 `gorm:"not null"`
}

if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn2{}); err != nil {
t.Fatalf("failed to auto migrate, got error: %v", err)
}

columnTypes, err := DB.Table("user_migrate_columns").Migrator().ColumnTypes(&UserMigrateColumn{})
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()]

type UserMigrateColumn struct {
ID uint
Name string
Salary float64
//Bonus float64 `gorm:"not null"`
//Stock float64
ID uint
Name string
Salary float64
Birthday time.Time `gorm:"precision:4"`
}

Expand All @@ -158,11 +198,9 @@ func TestSmartMigrateColumn(t *testing.T) {
DB.AutoMigrate(&UserMigrateColumn{})

type UserMigrateColumn2 struct {
ID uint
Name string `gorm:"size:128"`
Salary float64 `gorm:"precision:2"`
//Bonus float64
//Stock float64 `gorm:"not null"`
ID uint
Name string `gorm:"size:128"`
Salary float64 `gorm:"precision:2"`
Birthday time.Time `gorm:"precision:2"`
NameIgnoreMigration string `gorm:"size:100"`
}
Expand All @@ -186,16 +224,6 @@ func TestSmartMigrateColumn(t *testing.T) {
if precision, o, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
t.Fatalf("salary's precision should be 2, but got %v %v", precision, o)
}
//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)
// }
case "birthday":
if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
t.Fatalf("birthday's precision should be 2, but got %v", precision)
Expand Down

0 comments on commit d148cb2

Please sign in to comment.