Skip to content

Commit d148cb2

Browse files
committed
[#6372] wip
1 parent 60f2107 commit d148cb2

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

tests/migrate_test.go

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,55 @@ func TestAutoMigrateSelfReferential(t *testing.T) {
141141
}
142142
}
143143

144+
func TestAutoMigrateNullable(t *testing.T) {
145+
type UserMigrateColumn struct {
146+
ID uint
147+
Bonus float64 `gorm:"not null"`
148+
Stock float64
149+
}
150+
151+
DB.Migrator().DropTable(&UserMigrateColumn{})
152+
153+
DB.AutoMigrate(&UserMigrateColumn{})
154+
155+
type UserMigrateColumn2 struct {
156+
ID uint
157+
Bonus float64
158+
Stock float64 `gorm:"not null"`
159+
}
160+
161+
if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn2{}); err != nil {
162+
t.Fatalf("failed to auto migrate, got error: %v", err)
163+
}
164+
165+
columnTypes, err := DB.Table("user_migrate_columns").Migrator().ColumnTypes(&UserMigrateColumn{})
166+
if err != nil {
167+
t.Fatalf("failed to get column types, got error: %v", err)
168+
}
169+
170+
for _, columnType := range columnTypes {
171+
switch columnType.Name() {
172+
case "bonus":
173+
// allow to change non-nullable to nullable
174+
if nullable, _ := columnType.Nullable(); !nullable {
175+
t.Fatalf("bonus's nullable should be true, bug got %t", nullable)
176+
}
177+
case "stock":
178+
// do not allow to change nullable to non-nullable
179+
if nullable, _ := columnType.Nullable(); !nullable {
180+
t.Fatalf("stock's nullable should be true, bug got %t", nullable)
181+
}
182+
}
183+
}
184+
}
185+
144186
func TestSmartMigrateColumn(t *testing.T) {
145187
fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]
146188

147189
type UserMigrateColumn struct {
148-
ID uint
149-
Name string
150-
Salary float64
151-
//Bonus float64 `gorm:"not null"`
152-
//Stock float64
190+
ID uint
191+
Name string
192+
Salary float64
153193
Birthday time.Time `gorm:"precision:4"`
154194
}
155195

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

160200
type UserMigrateColumn2 struct {
161-
ID uint
162-
Name string `gorm:"size:128"`
163-
Salary float64 `gorm:"precision:2"`
164-
//Bonus float64
165-
//Stock float64 `gorm:"not null"`
201+
ID uint
202+
Name string `gorm:"size:128"`
203+
Salary float64 `gorm:"precision:2"`
166204
Birthday time.Time `gorm:"precision:2"`
167205
NameIgnoreMigration string `gorm:"size:100"`
168206
}
@@ -186,16 +224,6 @@ func TestSmartMigrateColumn(t *testing.T) {
186224
if precision, o, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
187225
t.Fatalf("salary's precision should be 2, but got %v %v", precision, o)
188226
}
189-
//case "bonus":
190-
// // allow to change non-nullable to nullable
191-
// if nullable, _ := columnType.Nullable(); !nullable {
192-
// t.Fatalf("bonus's nullable should be true, bug got %t", nullable)
193-
// }
194-
//case "stock":
195-
// // do not allow to change nullable to non-nullable
196-
// if nullable, _ := columnType.Nullable(); !nullable {
197-
// t.Fatalf("stock's nullable should be true, bug got %t", nullable)
198-
// }
199227
case "birthday":
200228
if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
201229
t.Fatalf("birthday's precision should be 2, but got %v", precision)

0 commit comments

Comments
 (0)