Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: default value with typecast and tests #211

Merged
merged 1 commit into from Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion migrator.go
Expand Up @@ -477,7 +477,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)
a631807682 marked this conversation as resolved.
Show resolved Hide resolved
}

if datetimePrecision.Valid {
Expand Down Expand Up @@ -783,3 +783,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
@@ -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)
}
})
}
}