Skip to content

Commit

Permalink
Improve Smart AutoMigrate
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Feb 19, 2022
1 parent 3b89559 commit ce06e3b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
23 changes: 15 additions & 8 deletions ddlmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func parseDDL(strs ...string) (*ddl, error) {
for _, name := range matches[1:] {
for idx, column := range result.columns {
if column.NameValue.String == name {
column.PrimayKeyValue = sql.NullBool{Bool: true, Valid: true}
column.PrimaryKeyValue = sql.NullBool{Bool: true, Valid: true}
result.columns[idx] = column
break
}
Expand All @@ -105,21 +105,28 @@ func parseDDL(strs ...string) (*ddl, error) {
}
} else if matches := columnRegexp.FindStringSubmatch(f); len(matches) > 0 {
columnType := migrator.ColumnType{
NameValue: sql.NullString{String: matches[1], Valid: true},
DataTypeValue: sql.NullString{String: matches[2], Valid: true},
ColumnTypeValue: sql.NullString{String: matches[2], Valid: true},
NameValue: sql.NullString{String: matches[1], Valid: true},
DataTypeValue: sql.NullString{String: matches[2], Valid: true},
ColumnTypeValue: sql.NullString{String: matches[2], Valid: true},
PrimaryKeyValue: sql.NullBool{Valid: true},
UniqueValue: sql.NullBool{Valid: true},
NullableValue: sql.NullBool{Valid: true},
DefaultValueValue: sql.NullString{Valid: true},
}

matchUpper := strings.ToUpper(matches[3])
if strings.Contains(matchUpper, " NOT NULL") {
columnType.NullableValue = sql.NullBool{Bool: false, Valid: true}
} else if strings.Contains(matchUpper, " NULL") {
columnType.NullableValue = sql.NullBool{Bool: true, Valid: true}
} else if strings.Contains(matchUpper, " UNIQUE") {
}
if strings.Contains(matchUpper, " UNIQUE") {
columnType.UniqueValue = sql.NullBool{Bool: true, Valid: true}
} else if strings.Contains(matchUpper, " PRIMARY") {
columnType.PrimayKeyValue = sql.NullBool{Bool: true, Valid: true}
} else if defaultMatches := defaultValueRegexp.FindStringSubmatch(matches[3]); len(defaultMatches) > 1 {
}
if strings.Contains(matchUpper, " PRIMARY") {
columnType.PrimaryKeyValue = sql.NullBool{Bool: true, Valid: true}
}
if defaultMatches := defaultValueRegexp.FindStringSubmatch(matches[3]); len(defaultMatches) > 1 {
columnType.DefaultValueValue = sql.NullString{String: strings.Trim(defaultMatches[1], `"`), Valid: true}
}

Expand Down
1 change: 0 additions & 1 deletion migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func (m Migrator) AlterColumn(value interface{}, name string) error {
createSQL := reg.ReplaceAllString(rawDDL, fmt.Sprintf("`%v` ?,", field.DBName))

return createSQL, []interface{}{m.FullDataTypeOf(field)}, nil

}
return "", nil, fmt.Errorf("failed to alter field with name %v", name)
})
Expand Down

0 comments on commit ce06e3b

Please sign in to comment.