Skip to content

Commit

Permalink
fix : fail to alter column from smallint to boolean (#165)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jeffry-luqman authored Mar 6, 2023
1 parent a7f2d26 commit ed060a7
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (m Migrator) AlterColumn(value interface{}, field string) error {
return err
}
} else {
if err := m.DB.Exec("ALTER TABLE ? ALTER COLUMN ? TYPE ? USING ?::?",
if err := m.DB.Exec("ALTER TABLE ? ALTER COLUMN ? TYPE ?"+m.genUsingExpression(fileType.SQL, fieldColumnType.DatabaseTypeName()),
m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType, clause.Column{Name: field.DBName}, fileType).Error; err != nil {
return err
}
Expand Down Expand Up @@ -375,6 +375,16 @@ func (m Migrator) AlterColumn(value interface{}, field string) error {
return nil
}

func (m Migrator) genUsingExpression(targetType, sourceType string) string {
if targetType == "boolean" {
switch sourceType {
case "int2", "int8", "numeric":
return " USING ?::INT::?"
}
}
return " USING ?::?"
}

func (m Migrator) HasConstraint(value interface{}, name string) bool {
var count int64
m.RunWithValue(value, func(stmt *gorm.Statement) error {
Expand Down

0 comments on commit ed060a7

Please sign in to comment.