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

Function TestMigrateColumns

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

Source from the content-addressed store, hash-verified

662}
663
664func TestMigrateColumns(t *testing.T) {
665 tidbSkip(t, "use another test case")
666
667 sqlite := DB.Dialector.Name() == "sqlite"
668 sqlserver := DB.Dialector.Name() == "sqlserver"
669
670 type ColumnStruct struct {
671 gorm.Model
672 Name string
673 Age int `gorm:"default:18;comment:my age"`
674 Code string `gorm:"unique;comment:my code;"`
675 Code2 string
676 Code3 string `gorm:"unique"`
677 }
678
679 DB.Migrator().DropTable(&ColumnStruct{})
680
681 if err := DB.AutoMigrate(&ColumnStruct{}); err != nil {
682 t.Errorf("Failed to migrate, got %v", err)
683 }
684
685 type ColumnStruct2 struct {
686 gorm.Model
687 Name string `gorm:"size:100"`
688 Code string `gorm:"unique;comment:my code2;default:hello"`
689 Code2 string `gorm:"unique"`
690 // Code3 string
691 }
692
693 if err := DB.Table("column_structs").Migrator().AlterColumn(&ColumnStruct{}, "Name"); err != nil {
694 t.Fatalf("no error should happened when alter column, but got %v", err)
695 }
696
697 if err := DB.Table("column_structs").AutoMigrate(&ColumnStruct2{}); err != nil {
698 t.Fatalf("no error should happened when auto migrate column, but got %v", err)
699 }
700
701 if columnTypes, err := DB.Migrator().ColumnTypes(&ColumnStruct{}); err != nil {
702 t.Fatalf("no error should returns for ColumnTypes")
703 } else {
704 stmt := &gorm.Statement{DB: DB}
705 stmt.Parse(&ColumnStruct2{})
706
707 for _, columnType := range columnTypes {
708 switch columnType.Name() {
709 case "id":
710 if v, ok := columnType.PrimaryKey(); !ok || !v {
711 t.Fatalf("column id primary key should be correct, name: %v, column: %#v", columnType.Name(),
712 columnType)
713 }
714 case "name":
715 dataType := DB.Dialector.DataTypeOf(stmt.Schema.LookUpField(columnType.Name()))
716 if !strings.Contains(strings.ToUpper(dataType), strings.ToUpper(columnType.DatabaseTypeName())) {
717 t.Fatalf("column name type should be correct, name: %v, length: %v, expects: %v, column: %#v",
718 columnType.Name(), columnType.DatabaseTypeName(), dataType, columnType)
719 }
720 if length, ok := columnType.Length(); !sqlite && (!ok || length != 100) {
721 t.Fatalf("column name length should be correct, name: %v, length: %v, expects: %v, column: %#v",

Callers

nothing calls this directly

Calls 15

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

Tested by

no test coverage detected