(t *testing.T)
| 141 | } |
| 142 | |
| 143 | func 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 | |
| 185 | func TestSmartMigrateColumn(t *testing.T) { |
| 186 | fullSupported := map[string]bool{"mysql": true, "postgres": true, "gaussdb": true}[DB.Dialector.Name()] |
nothing calls this directly
no test coverage detected