feat(writers): quote default values based on SQL column type
Some checks failed
CI / Test (1.24) (push) Successful in -22m47s
CI / Lint (push) Failing after -24m34s
Integration Tests / Integration Tests (push) Successful in -25m0s
CI / Test (1.25) (push) Successful in -22m35s
CI / Build (push) Successful in -24m43s
Release / Build and Release (push) Successful in -21m46s
Some checks failed
CI / Test (1.24) (push) Successful in -22m47s
CI / Lint (push) Failing after -24m34s
Integration Tests / Integration Tests (push) Successful in -25m0s
CI / Test (1.25) (push) Successful in -22m35s
CI / Build (push) Successful in -24m43s
Release / Build and Release (push) Successful in -21m46s
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:
@@ -81,6 +81,64 @@ func SanitizeFilename(name string) string {
|
||||
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.
|
||||
// Go struct tags are delimited by backticks, so any backtick in the value would break the syntax.
|
||||
// This function:
|
||||
|
||||
Reference in New Issue
Block a user