feat(writers): quote default values based on SQL column type
Bun and GORM struct tags now emit quoted defaults for string/date/time/UUID columns (e.g. default:'disconnected') and unquoted defaults for numeric and boolean columns (e.g. default:0, default:true). Function-call expressions such as now() or gen_random_uuid() are never quoted regardless of type. Adds QuoteDefaultValue(value, sqlType) helper in pkg/writers and updates both type mappers and the bun writer tests accordingly.
This commit is contained in:
@@ -208,8 +208,8 @@ func (tm *TypeMapper) BuildBunTag(column *models.Column, table *models.Table) st
|
|||||||
|
|
||||||
// Default value
|
// Default value
|
||||||
if column.Default != nil {
|
if column.Default != nil {
|
||||||
// Sanitize default value to remove backticks
|
// Sanitize default value to remove backticks, then quote based on column type
|
||||||
safeDefault := writers.SanitizeStructTagValue(fmt.Sprintf("%v", column.Default))
|
safeDefault := writers.QuoteDefaultValue(writers.SanitizeStructTagValue(fmt.Sprintf("%v", column.Default)), column.Type)
|
||||||
parts = append(parts, fmt.Sprintf("default:%s", safeDefault))
|
parts = append(parts, fmt.Sprintf("default:%s", safeDefault))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -312,7 +312,7 @@ func TestWriter_MultipleReferencesToSameTable(t *testing.T) {
|
|||||||
fieldName string
|
fieldName string
|
||||||
tag string
|
tag string
|
||||||
}{
|
}{
|
||||||
{"RelRIDFilepointerRequestOrgAPIEvents", "join:id_filepointer=rid_filepointer_request"}, // Has many via rid_filepointer_request
|
{"RelRIDFilepointerRequestOrgAPIEvents", "join:id_filepointer=rid_filepointer_request"}, // Has many via rid_filepointer_request
|
||||||
{"RelRIDFilepointerResponseOrgAPIEvents", "join:id_filepointer=rid_filepointer_response"}, // Has many via rid_filepointer_response
|
{"RelRIDFilepointerResponseOrgAPIEvents", "join:id_filepointer=rid_filepointer_response"}, // Has many via rid_filepointer_response
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -461,10 +461,10 @@ func TestWriter_MultipleHasManyRelationships(t *testing.T) {
|
|||||||
|
|
||||||
// Verify all has-many relationships have unique names
|
// Verify all has-many relationships have unique names
|
||||||
hasManyExpectations := []string{
|
hasManyExpectations := []string{
|
||||||
"RelRIDAPIProviderOrgLogins", // Has many via Login
|
"RelRIDAPIProviderOrgLogins", // Has many via Login
|
||||||
"RelRIDAPIProviderOrgFilepointers", // Has many via Filepointer
|
"RelRIDAPIProviderOrgFilepointers", // Has many via Filepointer
|
||||||
"RelRIDAPIProviderOrgAPIEvents", // Has many via APIEvent
|
"RelRIDAPIProviderOrgAPIEvents", // Has many via APIEvent
|
||||||
"RelRIDOwner", // Has one via rid_owner
|
"RelRIDOwner", // Has one via rid_owner
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, exp := range hasManyExpectations {
|
for _, exp := range hasManyExpectations {
|
||||||
@@ -615,14 +615,44 @@ func TestTypeMapper_BuildBunTag(t *testing.T) {
|
|||||||
want: []string{"email,", "type:varchar(255),", "nullzero,"},
|
want: []string{"email,", "type:varchar(255),", "nullzero,"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with default",
|
name: "with default string",
|
||||||
column: &models.Column{
|
column: &models.Column{
|
||||||
Name: "status",
|
Name: "status",
|
||||||
Type: "text",
|
Type: "text",
|
||||||
NotNull: true,
|
NotNull: true,
|
||||||
Default: "active",
|
Default: "active",
|
||||||
},
|
},
|
||||||
want: []string{"status,", "type:text,", "default:active,"},
|
want: []string{"status,", "type:text,", "default:'active',"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with default integer",
|
||||||
|
column: &models.Column{
|
||||||
|
Name: "retries",
|
||||||
|
Type: "integer",
|
||||||
|
NotNull: true,
|
||||||
|
Default: "0",
|
||||||
|
},
|
||||||
|
want: []string{"retries,", "type:integer,", "default:0,"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with default boolean",
|
||||||
|
column: &models.Column{
|
||||||
|
Name: "active",
|
||||||
|
Type: "boolean",
|
||||||
|
NotNull: true,
|
||||||
|
Default: "true",
|
||||||
|
},
|
||||||
|
want: []string{"active,", "type:boolean,", "default:true,"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with default function call",
|
||||||
|
column: &models.Column{
|
||||||
|
Name: "created_at",
|
||||||
|
Type: "timestamp",
|
||||||
|
NotNull: true,
|
||||||
|
Default: "now()",
|
||||||
|
},
|
||||||
|
want: []string{"created_at,", "type:timestamp,", "default:now(),"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "auto increment with AutoIncrement flag",
|
name: "auto increment with AutoIncrement flag",
|
||||||
|
|||||||
@@ -238,8 +238,8 @@ func (tm *TypeMapper) BuildGormTag(column *models.Column, table *models.Table) s
|
|||||||
|
|
||||||
// Default value
|
// Default value
|
||||||
if column.Default != nil {
|
if column.Default != nil {
|
||||||
// Sanitize default value to remove backticks
|
// Sanitize default value to remove backticks, then quote based on column type
|
||||||
safeDefault := writers.SanitizeStructTagValue(fmt.Sprintf("%v", column.Default))
|
safeDefault := writers.QuoteDefaultValue(writers.SanitizeStructTagValue(fmt.Sprintf("%v", column.Default)), column.Type)
|
||||||
parts = append(parts, fmt.Sprintf("default:%s", safeDefault))
|
parts = append(parts, fmt.Sprintf("default:%s", safeDefault))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -81,6 +81,64 @@ func SanitizeFilename(name string) string {
|
|||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QuoteDefaultValue wraps a sanitized default value in single quotes when the SQL
|
||||||
|
// column type requires it (strings, dates, times, UUIDs, enums). Numeric types
|
||||||
|
// (integers, floats, serials) and boolean types are left unquoted. Function-call
|
||||||
|
// expressions such as now() or gen_random_uuid() are always left unquoted regardless
|
||||||
|
// of type, because they contain parentheses.
|
||||||
|
//
|
||||||
|
// Examples (varchar): "disconnected" → "'disconnected'"
|
||||||
|
// Examples (boolean): "true" → "true"
|
||||||
|
// Examples (bigint): "0" → "0"
|
||||||
|
// Examples (timestamp): "now()" → "now()" (function call – never quoted)
|
||||||
|
func QuoteDefaultValue(value, sqlType string) string {
|
||||||
|
// Function calls are never quoted regardless of column type.
|
||||||
|
if strings.Contains(value, "(") || strings.Contains(value, ")") {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalise the SQL type: lowercase, strip length/precision suffix.
|
||||||
|
baseType := strings.ToLower(strings.TrimSpace(sqlType))
|
||||||
|
if idx := strings.Index(baseType, "("); idx > 0 {
|
||||||
|
baseType = baseType[:idx]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Types whose default values must NOT be quoted.
|
||||||
|
unquotedTypes := map[string]bool{
|
||||||
|
// Integer types
|
||||||
|
"integer": true,
|
||||||
|
"int": true,
|
||||||
|
"int2": true,
|
||||||
|
"int4": true,
|
||||||
|
"int8": true,
|
||||||
|
"smallint": true,
|
||||||
|
"bigint": true,
|
||||||
|
"serial": true,
|
||||||
|
"smallserial": true,
|
||||||
|
"bigserial": true,
|
||||||
|
// Float / numeric types
|
||||||
|
"real": true,
|
||||||
|
"float": true,
|
||||||
|
"float4": true,
|
||||||
|
"float8": true,
|
||||||
|
"double precision": true,
|
||||||
|
"numeric": true,
|
||||||
|
"decimal": true,
|
||||||
|
"money": true,
|
||||||
|
// Boolean
|
||||||
|
"boolean": true,
|
||||||
|
"bool": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
if unquotedTypes[baseType] {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Everything else (text, varchar, char, uuid, date, time, timestamp, json, …)
|
||||||
|
// is treated as a quoted literal.
|
||||||
|
return "'" + value + "'"
|
||||||
|
}
|
||||||
|
|
||||||
// SanitizeStructTagValue sanitizes a value to be safely used inside Go struct tags.
|
// SanitizeStructTagValue sanitizes a value to be safely used inside Go struct tags.
|
||||||
// Go struct tags are delimited by backticks, so any backtick in the value would break the syntax.
|
// Go struct tags are delimited by backticks, so any backtick in the value would break the syntax.
|
||||||
// This function:
|
// This function:
|
||||||
|
|||||||
Reference in New Issue
Block a user