Update migrator.go

This commit is contained in:
Jinzhu 2022-12-24 12:05:30 +08:00 committed by GitHub
parent c035852cb1
commit ac9262f736
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -424,13 +424,29 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
fullDataType := strings.TrimSpace(strings.ToLower(m.DB.Migrator().FullDataTypeOf(field).SQL)) fullDataType := strings.TrimSpace(strings.ToLower(m.DB.Migrator().FullDataTypeOf(field).SQL))
realDataType := strings.ToLower(columnType.DatabaseTypeName()) realDataType := strings.ToLower(columnType.DatabaseTypeName())
alterColumn := false var (
alterColumn, isSameType bool
)
if !field.PrimaryKey {
// check type // check type
if !field.PrimaryKey && !strings.HasPrefix(fullDataType, realDataType) { if !strings.HasPrefix(fullDataType, realDataType) {
alterColumn = true // check type aliases
aliases := m.DB.Migrator().GetTypeAliases(realDataType)
for _, alias := range aliases {
if strings.HasPrefix(fullDataType, alias) {
isSameType = true
break
}
} }
if !isSameType {
alterColumn = true
}
}
}
if !isSameType {
// check size // check size
if length, ok := columnType.Length(); length != int64(field.Size) { if length, ok := columnType.Length(); length != int64(field.Size) {
if length > 0 && field.Size > 0 { if length > 0 && field.Size > 0 {
@ -452,6 +468,7 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
alterColumn = true alterColumn = true
} }
} }
}
// check nullable // check nullable
if nullable, ok := columnType.Nullable(); ok && nullable == field.NotNull { if nullable, ok := columnType.Nullable(); ok && nullable == field.NotNull {
@ -471,17 +488,19 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
// check default value // check default value
if !field.PrimaryKey { if !field.PrimaryKey {
currentDefaultNotNull := field.HasDefaultValue && !strings.EqualFold(field.DefaultValue, "NULL")
dv, dvNotNull := columnType.DefaultValue() dv, dvNotNull := columnType.DefaultValue()
if dvNotNull && field.DefaultValueInterface == nil { if dvNotNull && !currentDefaultNotNull {
// defalut value -> null // defalut value -> null
alterColumn = true alterColumn = true
} else if !dvNotNull && field.DefaultValueInterface != nil { } else if !dvNotNull && currentDefaultNotNull {
// null -> default value // null -> default value
alterColumn = true alterColumn = true
} else if dv != field.DefaultValue { } else if (field.GORMDataType != schema.Time && dv != field.DefaultValue) ||
(field.GORMDataType == schema.Time && !strings.EqualFold(strings.TrimSuffix(dv, "()"), strings.TrimSuffix(field.DefaultValue, "()"))) {
// default value not equal // default value not equal
// not both null // not both null
if !(field.DefaultValueInterface == nil && !dvNotNull) { if currentDefaultNotNull || dvNotNull {
alterColumn = true alterColumn = true
} }
} }
@ -496,7 +515,7 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
} }
if alterColumn && !field.IgnoreMigration { if alterColumn && !field.IgnoreMigration {
return m.DB.Migrator().AlterColumn(value, field.Name) return m.DB.Migrator().AlterColumn(value, field.DBName)
} }
return nil return nil
@ -881,3 +900,8 @@ func (m Migrator) CurrentTable(stmt *gorm.Statement) interface{} {
func (m Migrator) GetIndexes(dst interface{}) ([]gorm.Index, error) { func (m Migrator) GetIndexes(dst interface{}) ([]gorm.Index, error) {
return nil, errors.New("not support") return nil, errors.New("not support")
} }
// GetTypeAliases return database type aliases
func (m Migrator) GetTypeAliases(databaseTypeName string) []string {
return nil
}