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

Function TestMigrateExistingBoolColumnPGAndGaussDB

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

Source from the content-addressed store, hash-verified

1768}
1769
1770func TestMigrateExistingBoolColumnPGAndGaussDB(t *testing.T) {
1771 if DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "gaussdb" {
1772 return
1773 }
1774
1775 type ColumnStruct struct {
1776 gorm.Model
1777 Name string
1778 StringBool string
1779 SmallintBool int `gorm:"type:smallint"`
1780 }
1781
1782 type ColumnStruct2 struct {
1783 gorm.Model
1784 Name string
1785 StringBool bool // change existing boolean column from string to boolean
1786 SmallintBool bool // change existing boolean column from smallint or other to boolean
1787 }
1788
1789 DB.Migrator().DropTable(&ColumnStruct{})
1790
1791 if err := DB.AutoMigrate(&ColumnStruct{}); err != nil {
1792 t.Errorf("Failed to migrate, got %v", err)
1793 }
1794
1795 if err := DB.Table("column_structs").AutoMigrate(&ColumnStruct2{}); err != nil {
1796 t.Fatalf("no error should happened when auto migrate column, but got %v", err)
1797 }
1798
1799 if columnTypes, err := DB.Migrator().ColumnTypes(&ColumnStruct{}); err != nil {
1800 t.Fatalf("no error should returns for ColumnTypes")
1801 } else {
1802 stmt := &gorm.Statement{DB: DB}
1803 stmt.Parse(&ColumnStruct2{})
1804
1805 for _, columnType := range columnTypes {
1806 switch columnType.Name() {
1807 case "id":
1808 if v, ok := columnType.PrimaryKey(); !ok || !v {
1809 t.Fatalf("column id primary key should be correct, name: %v, column: %#v", columnType.Name(),
1810 columnType)
1811 }
1812 case "string_bool":
1813 dataType := DB.Dialector.DataTypeOf(stmt.Schema.LookUpField(columnType.Name()))
1814 if !strings.Contains(strings.ToUpper(dataType), strings.ToUpper(columnType.DatabaseTypeName())) {
1815 t.Fatalf("column name type should be correct, name: %v, length: %v, expects: %v, column: %#v",
1816 columnType.Name(), columnType.DatabaseTypeName(), dataType, columnType)
1817 }
1818 case "smallint_bool":
1819 dataType := DB.Dialector.DataTypeOf(stmt.Schema.LookUpField(columnType.Name()))
1820 if !strings.Contains(strings.ToUpper(dataType), strings.ToUpper(columnType.DatabaseTypeName())) {
1821 t.Fatalf("column name type should be correct, name: %v, length: %v, expects: %v, column: %#v",
1822 columnType.Name(), columnType.DatabaseTypeName(), dataType, columnType)
1823 }
1824 }
1825 }
1826 }
1827}

Callers

nothing calls this directly

Calls 12

ParseMethod · 0.95
LookUpFieldMethod · 0.80
ContainsMethod · 0.80
NameMethod · 0.65
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
TableMethod · 0.65
ColumnTypesMethod · 0.65
PrimaryKeyMethod · 0.65
DataTypeOfMethod · 0.65
DatabaseTypeNameMethod · 0.65

Tested by

no test coverage detected