@@ -141,15 +141,55 @@ func TestAutoMigrateSelfReferential(t *testing.T) {
141
141
}
142
142
}
143
143
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
+
144
186
func TestSmartMigrateColumn (t * testing.T ) {
145
187
fullSupported := map [string ]bool {"mysql" : true , "postgres" : true }[DB .Dialector .Name ()]
146
188
147
189
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
153
193
Birthday time.Time `gorm:"precision:4"`
154
194
}
155
195
@@ -158,11 +198,9 @@ func TestSmartMigrateColumn(t *testing.T) {
158
198
DB .AutoMigrate (& UserMigrateColumn {})
159
199
160
200
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"`
166
204
Birthday time.Time `gorm:"precision:2"`
167
205
NameIgnoreMigration string `gorm:"size:100"`
168
206
}
@@ -186,16 +224,6 @@ func TestSmartMigrateColumn(t *testing.T) {
186
224
if precision , o , _ := columnType .DecimalSize (); (fullSupported || precision != 0 ) && precision != 2 {
187
225
t .Fatalf ("salary's precision should be 2, but got %v %v" , precision , o )
188
226
}
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
- // }
199
227
case "birthday" :
200
228
if precision , _ , _ := columnType .DecimalSize (); (fullSupported || precision != 0 ) && precision != 2 {
201
229
t .Fatalf ("birthday's precision should be 2, but got %v" , precision )
0 commit comments