@@ -148,8 +148,6 @@ func TestSmartMigrateColumn(t *testing.T) {
148148 ID uint
149149 Name string
150150 Salary float64
151- Bonus float64 `gorm:"not null"`
152- Stock float64
153151 Birthday time.Time `gorm:"precision:4"`
154152 }
155153
@@ -159,10 +157,8 @@ func TestSmartMigrateColumn(t *testing.T) {
159157
160158 type UserMigrateColumn2 struct {
161159 ID uint
162- Name string `gorm:"size:128"`
163- Salary float64 `gorm:"precision:2"`
164- Bonus float64
165- Stock float64 `gorm:"not null"`
160+ Name string `gorm:"size:128"`
161+ Salary float64 `gorm:"precision:2"`
166162 Birthday time.Time `gorm:"precision:2"`
167163 NameIgnoreMigration string `gorm:"size:100"`
168164 }
@@ -186,16 +182,6 @@ func TestSmartMigrateColumn(t *testing.T) {
186182 if precision , o , _ := columnType .DecimalSize (); (fullSupported || precision != 0 ) && precision != 2 {
187183 t .Fatalf ("salary's precision should be 2, but got %v %v" , precision , o )
188184 }
189- case "bonus" :
190- // allow to change non-nullable to nullable
191- if nullable , _ := columnType .Nullable (); ! nullable {
192- t .Fatalf ("bonus's nullable should be true, bug got %t" , nullable )
193- }
194- case "stock" :
195- // do not allow to change nullable to non-nullable
196- if nullable , _ := columnType .Nullable (); ! nullable {
197- t .Fatalf ("stock's nullable should be true, bug got %t" , nullable )
198- }
199185 case "birthday" :
200186 if precision , _ , _ := columnType .DecimalSize (); (fullSupported || precision != 0 ) && precision != 2 {
201187 t .Fatalf ("birthday's precision should be 2, but got %v" , precision )
@@ -1959,3 +1945,66 @@ func TestMigrateWithUniqueIndexAndUnique(t *testing.T) {
19591945 }
19601946 }
19611947}
1948+
1949+ func TestSmartMigrateColumnNullable (t * testing.T ) {
1950+ fullSupported := map [string ]bool {"mysql" : true , "postgres" : true }[DB .Dialector .Name ()]
1951+
1952+ type UserMigrateColumn struct {
1953+ ID uint
1954+ Name string
1955+ Salary float64
1956+ Bonus float64 `gorm:"not null"`
1957+ Stock float64
1958+ Birthday time.Time `gorm:"precision:4"`
1959+ }
1960+
1961+ DB .Migrator ().DropTable (& UserMigrateColumn {})
1962+
1963+ DB .AutoMigrate (& UserMigrateColumn {})
1964+
1965+ type UserMigrateColumn2 struct {
1966+ ID uint
1967+ Name string `gorm:"size:128"`
1968+ Salary float64 `gorm:"precision:2"`
1969+ Bonus float64
1970+ Stock float64 `gorm:"not null"`
1971+ Birthday time.Time `gorm:"precision:2"`
1972+ NameIgnoreMigration string `gorm:"size:100"`
1973+ }
1974+
1975+ if err := DB .Table ("user_migrate_columns" ).AutoMigrate (& UserMigrateColumn2 {}); err != nil {
1976+ t .Fatalf ("failed to auto migrate, got error: %v" , err )
1977+ }
1978+
1979+ columnTypes , err := DB .Table ("user_migrate_columns" ).Migrator ().ColumnTypes (& UserMigrateColumn {})
1980+ if err != nil {
1981+ t .Fatalf ("failed to get column types, got error: %v" , err )
1982+ }
1983+
1984+ for _ , columnType := range columnTypes {
1985+ switch columnType .Name () {
1986+ case "name" :
1987+ if length , _ := columnType .Length (); (fullSupported || length != 0 ) && length != 128 {
1988+ t .Fatalf ("name's length should be 128, but got %v" , length )
1989+ }
1990+ case "salary" :
1991+ if precision , o , _ := columnType .DecimalSize (); (fullSupported || precision != 0 ) && precision != 2 {
1992+ t .Fatalf ("salary's precision should be 2, but got %v %v" , precision , o )
1993+ }
1994+ case "bonus" :
1995+ // allow to change non-nullable to nullable
1996+ if nullable , _ := columnType .Nullable (); ! nullable {
1997+ t .Fatalf ("bonus's nullable should be true, bug got %t" , nullable )
1998+ }
1999+ case "stock" :
2000+ // do not allow to change nullable to non-nullable
2001+ if nullable , _ := columnType .Nullable (); ! nullable {
2002+ t .Fatalf ("stock's nullable should be true, bug got %t" , nullable )
2003+ }
2004+ case "birthday" :
2005+ if precision , _ , _ := columnType .DecimalSize (); (fullSupported || precision != 0 ) && precision != 2 {
2006+ t .Fatalf ("birthday's precision should be 2, but got %v" , precision )
2007+ }
2008+ }
2009+ }
2010+ }
0 commit comments