MCPcopy
hub / github.com/go-gorm/gorm / TestAutoMigrateNullable

Function TestAutoMigrateNullable

tests/migrate_test.go:143–183  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 7

DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
TableMethod · 0.65
ColumnTypesMethod · 0.65
NameMethod · 0.65
NullableMethod · 0.65

Tested by

no test coverage detected