Skip to content

Commit

Permalink
Fix migrate columns with default value
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Oct 8, 2022
1 parent a4858c5 commit af97cb4
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,35 +279,32 @@ func (m Migrator) AlterColumn(value interface{}, field string) error {
}
}

return m.DB.Connection(func(tx *gorm.DB) error {
fileType := clause.Expr{SQL: m.DataTypeOf(field)}
if fieldColumnType.DatabaseTypeName() != fileType.SQL {
filedColumnAutoIncrement, _ := fieldColumnType.AutoIncrement()
if field.AutoIncrement && filedColumnAutoIncrement { // update
serialDatabaseType, _ := getSerialDatabaseType(fileType.SQL)
if t, _ := fieldColumnType.ColumnType(); t != serialDatabaseType {
if err := m.UpdateSequence(tx, stmt, field, serialDatabaseType); err != nil {
return err
}
}
} else if field.AutoIncrement && !filedColumnAutoIncrement { // create
serialDatabaseType, _ := getSerialDatabaseType(fileType.SQL)
if err := m.CreateSequence(tx, stmt, field, serialDatabaseType); err != nil {
return err
}
} else if !field.AutoIncrement && filedColumnAutoIncrement { // delete
if err := m.DeleteSequence(tx, stmt, field, fileType); err != nil {
return err
}
} else {
if err := tx.Exec("ALTER TABLE ? ALTER COLUMN ? TYPE ? USING ?::?",
m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType, clause.Column{Name: field.DBName}, fileType).Error; err != nil {
fileType := clause.Expr{SQL: m.DataTypeOf(field)}
if fieldColumnType.DatabaseTypeName() != fileType.SQL {
filedColumnAutoIncrement, _ := fieldColumnType.AutoIncrement()
if field.AutoIncrement && filedColumnAutoIncrement { // update
serialDatabaseType, _ := getSerialDatabaseType(fileType.SQL)
if t, _ := fieldColumnType.ColumnType(); t != serialDatabaseType {
if err := m.UpdateSequence(m.DB, stmt, field, serialDatabaseType); err != nil {
return err
}
}
} else if field.AutoIncrement && !filedColumnAutoIncrement { // create
serialDatabaseType, _ := getSerialDatabaseType(fileType.SQL)
if err := m.CreateSequence(m.DB, stmt, field, serialDatabaseType); err != nil {
return err
}
} else if !field.AutoIncrement && filedColumnAutoIncrement { // delete
if err := m.DeleteSequence(m.DB, stmt, field, fileType); err != nil {
return err
}
} else {
if err := m.DB.Exec("ALTER TABLE ? ALTER COLUMN ? TYPE ? USING ?::?",
m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType, clause.Column{Name: field.DBName}, fileType).Error; err != nil {
return err
}
}
return nil
})
}

if null, _ := fieldColumnType.Nullable(); null == field.NotNull {
if field.NotNull {
Expand Down Expand Up @@ -695,4 +692,4 @@ func groupByIndexName(indexList []*Index) map[string][]*Index {

func (m Migrator) GetTypeAliases(databaseTypeName string) []string {
return typeAliasMap[databaseTypeName]
}
}

0 comments on commit af97cb4

Please sign in to comment.