Skip to content

Commit

Permalink
fix: default value with typecast (#211)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksandr Tuliakov <tuliakov@basetrack.net>
  • Loading branch information
MrChaos1993 and Aleksandr Tuliakov authored Oct 10, 2023
1 parent b3c309a commit 89bd876
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
6 changes: 5 additions & 1 deletion migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType,
}

if column.DefaultValueValue.Valid {
column.DefaultValueValue.String = regexp.MustCompile(`'?(.*\b|)'?:+[\w\s]+$`).ReplaceAllString(column.DefaultValueValue.String, "$1")
column.DefaultValueValue.String = parseDefaultValueValue(column.DefaultValueValue.String)
}

if datetimePrecision.Valid {
Expand Down Expand Up @@ -784,3 +784,7 @@ func (m Migrator) RenameColumn(dst interface{}, oldName, field string) error {
m.resetPreparedStmts()
return nil
}

func parseDefaultValueValue(defaultValue string) string {
return regexp.MustCompile(`^(.*?)(?:::.*)?$`).ReplaceAllString(defaultValue, "$1")
}
102 changes: 102 additions & 0 deletions migrator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package postgres

import "testing"

func Test_parseDefaultValueValue(t *testing.T) {
type args struct {
defaultValue string
}
tests := []struct {
name string
args args
want string
}{
{
name: "it should works with number without colons",
args: args{defaultValue: "0"},
want: "0",
},
{
name: "it should works with number and two colons",
args: args{defaultValue: "0::int8"},
want: "0",
},
{
name: "it should works with number and three colons",
args: args{defaultValue: "0:::int8"},
want: "0",
},
{
name: "it should works with empty string without colons",
args: args{defaultValue: "''"},
want: "''",
},
{
name: "it should works with empty string with two colons",
args: args{defaultValue: "''::character varying"},
want: "''",
},
{
name: "it should works with empty string with three colons",
args: args{defaultValue: "'':::character varying"},
want: "''",
},
{
name: "it should works with string without colons",
args: args{defaultValue: "'field'"},
want: "'field'",
},
{
name: "it should works with string with two colons",
args: args{defaultValue: "'field'::character varying"},
want: "'field'",
},
{
name: "it should works with string with three colons",
args: args{defaultValue: "'field':::character varying"},
want: "'field'",
},
{
name: "it should works with value with two colons",
args: args{defaultValue: "field"},
want: "field",
},
{
name: "it should works with value without colons",
args: args{defaultValue: "field::character varying"},
want: "field",
},
{
name: "it should works with value with three colons",
args: args{defaultValue: "field:::character varying"},
want: "field",
},
{
name: "it should works with function without colons",
args: args{defaultValue: "now()"},
want: "now()",
},
{
name: "it should works with function with two colons",
args: args{defaultValue: "now()::timestamp without time zone"},
want: "now()",
},
{
name: "it should works with json without colons",
args: args{defaultValue: "{}"},
want: "{}",
},
{
name: "it should works with json with two colons",
args: args{defaultValue: "{}::jsonb"},
want: "{}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseDefaultValueValue(tt.args.defaultValue); got != tt.want {
t.Errorf("parseDefaultValueValue() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 89bd876

Please sign in to comment.