(t *testing.T)
| 523 | } |
| 524 | |
| 525 | func TestTiDBMigrateColumns(t *testing.T) { |
| 526 | if !isTiDB() { |
| 527 | t.Skip() |
| 528 | } |
| 529 | |
| 530 | // TiDB can't change column constraint and has auto_random feature |
| 531 | type ColumnStruct struct { |
| 532 | ID int `gorm:"primarykey;default:auto_random()"` |
| 533 | Name string |
| 534 | Age int `gorm:"default:18;comment:my age"` |
| 535 | Code string `gorm:"unique;comment:my code;"` |
| 536 | Code2 string |
| 537 | Code3 string `gorm:"unique"` |
| 538 | } |
| 539 | |
| 540 | DB.Migrator().DropTable(&ColumnStruct{}) |
| 541 | |
| 542 | if err := DB.AutoMigrate(&ColumnStruct{}); err != nil { |
| 543 | t.Errorf("Failed to migrate, got %v", err) |
| 544 | } |
| 545 | |
| 546 | type ColumnStruct2 struct { |
| 547 | ID int `gorm:"primarykey;default:auto_random()"` |
| 548 | Name string `gorm:"size:100"` |
| 549 | Code string `gorm:"unique;comment:my code2;default:hello"` |
| 550 | Code2 string `gorm:"comment:my code2;default:hello"` |
| 551 | } |
| 552 | |
| 553 | if err := DB.Table("column_structs").Migrator().AlterColumn(&ColumnStruct{}, "Name"); err != nil { |
| 554 | t.Fatalf("no error should happened when alter column, but got %v", err) |
| 555 | } |
| 556 | |
| 557 | if err := DB.Table("column_structs").AutoMigrate(&ColumnStruct2{}); err != nil { |
| 558 | t.Fatalf("no error should happened when auto migrate column, but got %v", err) |
| 559 | } |
| 560 | |
| 561 | if columnTypes, err := DB.Migrator().ColumnTypes(&ColumnStruct{}); err != nil { |
| 562 | t.Fatalf("no error should returns for ColumnTypes") |
| 563 | } else { |
| 564 | stmt := &gorm.Statement{DB: DB} |
| 565 | stmt.Parse(&ColumnStruct2{}) |
| 566 | |
| 567 | for _, columnType := range columnTypes { |
| 568 | switch columnType.Name() { |
| 569 | case "id": |
| 570 | if v, ok := columnType.PrimaryKey(); !ok || !v { |
| 571 | t.Fatalf("column id primary key should be correct, name: %v, column: %#v", columnType.Name(), |
| 572 | columnType) |
| 573 | } |
| 574 | case "name": |
| 575 | dataType := DB.Dialector.DataTypeOf(stmt.Schema.LookUpField(columnType.Name())) |
| 576 | if !strings.Contains(strings.ToUpper(dataType), strings.ToUpper(columnType.DatabaseTypeName())) { |
| 577 | t.Fatalf("column name type should be correct, name: %v, length: %v, expects: %v, column: %#v", |
| 578 | columnType.Name(), columnType.DatabaseTypeName(), dataType, columnType) |
| 579 | } |
| 580 | if length, ok := columnType.Length(); !ok || length != 100 { |
| 581 | t.Fatalf("column name length should be correct, name: %v, length: %v, expects: %v, column: %#v", |
| 582 | columnType.Name(), length, 100, columnType) |
nothing calls this directly
no test coverage detected