(t *testing.T)
| 662 | } |
| 663 | |
| 664 | func 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", |
nothing calls this directly
no test coverage detected