From ab2160880e20ac3a46c133ce1edd96637cb0f975 Mon Sep 17 00:00:00 2001 From: Aleksandr Tuliakov Date: Thu, 31 Aug 2023 16:24:50 +0400 Subject: [PATCH] fix: default value with typecast --- migrator.go | 6 ++- migrator_test.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 migrator_test.go diff --git a/migrator.go b/migrator.go index bea383d..0cab805 100644 --- a/migrator.go +++ b/migrator.go @@ -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) } if datetimePrecision.Valid { @@ -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") +} diff --git a/migrator_test.go b/migrator_test.go new file mode 100644 index 0000000..00bf835 --- /dev/null +++ b/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) + } + }) + } +}