Merge conflict detection compared bigserial (DBML) against bigint (live PostgreSQL read of an existing serial column) as incompatible types, since serial is sugar over an integer column plus a sequence default and PostgreSQL always reports back the underlying integer type. Add SerialUnderlyingType and use it when comparing column types for conflicts. QuoteDefaultValue also wrapped bare keyword expressions like CURRENT_DATE in string quotes because they contain no parentheses, unlike function-call defaults such as now(). Recognize known bare keyword defaults and leave them unquoted across CREATE TABLE, ALTER TABLE ADD COLUMN, and ALTER COLUMN SET DEFAULT generation.
73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
package writers
|
|
|
|
import "testing"
|
|
|
|
func TestQuoteDefaultValue(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
value string
|
|
sqlType string
|
|
want string
|
|
}{
|
|
{
|
|
name: "text default is quoted",
|
|
value: "active",
|
|
sqlType: "text",
|
|
want: "'active'",
|
|
},
|
|
{
|
|
name: "array default from bare literal is quoted once",
|
|
value: "{}",
|
|
sqlType: "text[]",
|
|
want: "'{}'",
|
|
},
|
|
{
|
|
name: "array default from quoted literal is preserved",
|
|
value: "'{}'",
|
|
sqlType: "text[]",
|
|
want: "'{}'",
|
|
},
|
|
{
|
|
name: "array default from double quoted literal is normalized",
|
|
value: "''{}''",
|
|
sqlType: "text[]",
|
|
want: "'{}'",
|
|
},
|
|
{
|
|
name: "function default is left alone",
|
|
value: "now()",
|
|
sqlType: "timestamptz",
|
|
want: "now()",
|
|
},
|
|
{
|
|
name: "bare keyword default CURRENT_DATE is not quoted",
|
|
value: "CURRENT_DATE",
|
|
sqlType: "date",
|
|
want: "CURRENT_DATE",
|
|
},
|
|
{
|
|
name: "bare keyword default is case insensitive",
|
|
value: "current_timestamp",
|
|
sqlType: "timestamptz",
|
|
want: "current_timestamp",
|
|
},
|
|
{
|
|
name: "bare keyword default localtime is not quoted",
|
|
value: "LOCALTIME",
|
|
sqlType: "time",
|
|
want: "LOCALTIME",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := QuoteDefaultValue(tt.value, tt.sqlType)
|
|
if got != tt.want {
|
|
t.Fatalf("QuoteDefaultValue(%q, %q) = %q, want %q", tt.value, tt.sqlType, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|