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

Function TestTiDBMigrateColumns

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

Source from the content-addressed store, hash-verified

523}
524
525func 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)

Callers

nothing calls this directly

Calls 15

ParseMethod · 0.95
isTiDBFunction · 0.85
LookUpFieldMethod · 0.80
ContainsMethod · 0.80
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
AlterColumnMethod · 0.65
TableMethod · 0.65
ColumnTypesMethod · 0.65
NameMethod · 0.65
PrimaryKeyMethod · 0.65

Tested by

no test coverage detected