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

Function TestSmartMigrateColumn

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

Source from the content-addressed store, hash-verified

183}
184
185func TestSmartMigrateColumn(t *testing.T) {
186 fullSupported := map[string]bool{"mysql": true, "postgres": true, "gaussdb": true}[DB.Dialector.Name()]
187
188 type UserMigrateColumn struct {
189 ID uint
190 Name string
191 Salary float64
192 Birthday time.Time `gorm:"precision:4"`
193 }
194
195 DB.Migrator().DropTable(&UserMigrateColumn{})
196
197 DB.AutoMigrate(&UserMigrateColumn{})
198
199 type UserMigrateColumn2 struct {
200 ID uint
201 Name string `gorm:"size:128"`
202 Salary float64 `gorm:"precision:2"`
203 Birthday time.Time `gorm:"precision:2"`
204 NameIgnoreMigration string `gorm:"size:100"`
205 }
206
207 if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn2{}); err != nil {
208 t.Fatalf("failed to auto migrate, got error: %v", err)
209 }
210
211 columnTypes, err := DB.Table("user_migrate_columns").Migrator().ColumnTypes(&UserMigrateColumn{})
212 if err != nil {
213 t.Fatalf("failed to get column types, got error: %v", err)
214 }
215
216 for _, columnType := range columnTypes {
217 switch columnType.Name() {
218 case "name":
219 if length, _ := columnType.Length(); (fullSupported || length != 0) && length != 128 {
220 t.Fatalf("name's length should be 128, but got %v", length)
221 }
222 case "salary":
223 if precision, o, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
224 t.Fatalf("salary's precision should be 2, but got %v %v", precision, o)
225 }
226 case "birthday":
227 if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
228 t.Fatalf("birthday's precision should be 2, but got %v", precision)
229 }
230 }
231 }
232
233 type UserMigrateColumn3 struct {
234 ID uint
235 Name string `gorm:"size:256"`
236 Salary float64 `gorm:"precision:3"`
237 Birthday time.Time `gorm:"precision:3"`
238 NameIgnoreMigration string `gorm:"size:128;-:migration"`
239 }
240
241 if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn3{}); err != nil {
242 t.Fatalf("failed to auto migrate, got error: %v", err)

Callers

nothing calls this directly

Calls 8

NameMethod · 0.65
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
TableMethod · 0.65
ColumnTypesMethod · 0.65
LengthMethod · 0.65
DecimalSizeMethod · 0.65

Tested by

no test coverage detected