Skip to content

Commit ed060a7

Browse files
fix : fail to alter column from smallint to boolean (#165)
* fix : fail to alter column from smallint to boolean * fix : fail to alter column from smallint to boolean * fix : fail to alter column from string to boolean * fix : fail to alter column from string to boolean if the value is "false" in string * move using conversion expression to func * move using expression to func
1 parent a7f2d26 commit ed060a7

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

migrator.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ func (m Migrator) AlterColumn(value interface{}, field string) error {
315315
return err
316316
}
317317
} else {
318-
if err := m.DB.Exec("ALTER TABLE ? ALTER COLUMN ? TYPE ? USING ?::?",
318+
if err := m.DB.Exec("ALTER TABLE ? ALTER COLUMN ? TYPE ?"+m.genUsingExpression(fileType.SQL, fieldColumnType.DatabaseTypeName()),
319319
m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType, clause.Column{Name: field.DBName}, fileType).Error; err != nil {
320320
return err
321321
}
@@ -375,6 +375,16 @@ func (m Migrator) AlterColumn(value interface{}, field string) error {
375375
return nil
376376
}
377377

378+
func (m Migrator) genUsingExpression(targetType, sourceType string) string {
379+
if targetType == "boolean" {
380+
switch sourceType {
381+
case "int2", "int8", "numeric":
382+
return " USING ?::INT::?"
383+
}
384+
}
385+
return " USING ?::?"
386+
}
387+
378388
func (m Migrator) HasConstraint(value interface{}, name string) bool {
379389
var count int64
380390
m.RunWithValue(value, func(stmt *gorm.Statement) error {

0 commit comments

Comments
 (0)