Skip to content

Commit 89bd876

Browse files
MrChaos1993Aleksandr Tuliakov
and
Aleksandr Tuliakov
authored
fix: default value with typecast (#211)
Co-authored-by: Aleksandr Tuliakov <tuliakov@basetrack.net>
1 parent b3c309a commit 89bd876

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

migrator.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType,
478478
}
479479

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

484484
if datetimePrecision.Valid {
@@ -784,3 +784,7 @@ func (m Migrator) RenameColumn(dst interface{}, oldName, field string) error {
784784
m.resetPreparedStmts()
785785
return nil
786786
}
787+
788+
func parseDefaultValueValue(defaultValue string) string {
789+
return regexp.MustCompile(`^(.*?)(?:::.*)?$`).ReplaceAllString(defaultValue, "$1")
790+
}

migrator_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package postgres
2+
3+
import "testing"
4+
5+
func Test_parseDefaultValueValue(t *testing.T) {
6+
type args struct {
7+
defaultValue string
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want string
13+
}{
14+
{
15+
name: "it should works with number without colons",
16+
args: args{defaultValue: "0"},
17+
want: "0",
18+
},
19+
{
20+
name: "it should works with number and two colons",
21+
args: args{defaultValue: "0::int8"},
22+
want: "0",
23+
},
24+
{
25+
name: "it should works with number and three colons",
26+
args: args{defaultValue: "0:::int8"},
27+
want: "0",
28+
},
29+
{
30+
name: "it should works with empty string without colons",
31+
args: args{defaultValue: "''"},
32+
want: "''",
33+
},
34+
{
35+
name: "it should works with empty string with two colons",
36+
args: args{defaultValue: "''::character varying"},
37+
want: "''",
38+
},
39+
{
40+
name: "it should works with empty string with three colons",
41+
args: args{defaultValue: "'':::character varying"},
42+
want: "''",
43+
},
44+
{
45+
name: "it should works with string without colons",
46+
args: args{defaultValue: "'field'"},
47+
want: "'field'",
48+
},
49+
{
50+
name: "it should works with string with two colons",
51+
args: args{defaultValue: "'field'::character varying"},
52+
want: "'field'",
53+
},
54+
{
55+
name: "it should works with string with three colons",
56+
args: args{defaultValue: "'field':::character varying"},
57+
want: "'field'",
58+
},
59+
{
60+
name: "it should works with value with two colons",
61+
args: args{defaultValue: "field"},
62+
want: "field",
63+
},
64+
{
65+
name: "it should works with value without colons",
66+
args: args{defaultValue: "field::character varying"},
67+
want: "field",
68+
},
69+
{
70+
name: "it should works with value with three colons",
71+
args: args{defaultValue: "field:::character varying"},
72+
want: "field",
73+
},
74+
{
75+
name: "it should works with function without colons",
76+
args: args{defaultValue: "now()"},
77+
want: "now()",
78+
},
79+
{
80+
name: "it should works with function with two colons",
81+
args: args{defaultValue: "now()::timestamp without time zone"},
82+
want: "now()",
83+
},
84+
{
85+
name: "it should works with json without colons",
86+
args: args{defaultValue: "{}"},
87+
want: "{}",
88+
},
89+
{
90+
name: "it should works with json with two colons",
91+
args: args{defaultValue: "{}::jsonb"},
92+
want: "{}",
93+
},
94+
}
95+
for _, tt := range tests {
96+
t.Run(tt.name, func(t *testing.T) {
97+
if got := parseDefaultValueValue(tt.args.defaultValue); got != tt.want {
98+
t.Errorf("parseDefaultValueValue() = %v, want %v", got, tt.want)
99+
}
100+
})
101+
}
102+
}

0 commit comments

Comments
 (0)