Skip to content

Commit

Permalink
only trim paired single-quotes for column.DefaultValue (#143)
Browse files Browse the repository at this point in the history
* only trim paired single-quotes for column.DefaultValue

* replace \' and ' to _ for supporting golang StructTag.Lookup()

* rollback replacing
  • Loading branch information
stephenfire committed Mar 9, 2024
1 parent 739f97b commit e724f92
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion migrator.go
Expand Up @@ -372,7 +372,13 @@ func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
column.AutoIncrementValue = sql.NullBool{Bool: true, Valid: true}
}

column.DefaultValueValue.String = strings.Trim(column.DefaultValueValue.String, "'")
// only trim paired single-quotes
s := column.DefaultValueValue.String
for (len(s) >= 3 && s[0] == '\'' && s[len(s)-1] == '\'' && s[len(s)-2] != '\\') ||
(len(s) == 2 && s == "''") {
s = s[1 : len(s)-1]
}
column.DefaultValueValue.String = s
if m.Dialector.DontSupportNullAsDefaultValue {
// rewrite mariadb default value like other version
if column.DefaultValueValue.Valid && column.DefaultValueValue.String == "NULL" {
Expand Down

0 comments on commit e724f92

Please sign in to comment.