Added support to change numeric precision#363
Conversation
Greptile SummaryThis PR fixes issue #362 by teaching the diff engine to detect and emit migrations when a Key changes:
Minor observations:
Confidence Score: 4/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[columnsEqual called] --> B{DataType equal?}
B -- No --> C[Return false: type change]
B -- Yes --> D{Precision nil-ness equal?}
D -- No --> E[Return false: precision changed]
D -- Yes --> F{Precision values equal?}
F -- No --> G[Return false: precision changed]
F -- Yes --> H{Scale nil-ness equal?}
H -- No --> I[Return false: scale changed]
H -- Yes --> J{Scale values equal?}
J -- No --> K[Return false: scale changed]
J -- Yes --> L[Continue other checks...]
M[generateColumnSQL called] --> N[columnTypeWithModifiers old\ne.g. numeric-15-4]
M --> O[columnTypeWithModifiers new\ne.g. numeric-16-6]
N --> P{oldType != newType?}
O --> P
P -- Yes --> Q[needsUsingClause?\nIsBuiltInType strips parens\nnumeric-15-4 → numeric → true]
Q -- No USING --> R[ALTER TABLE t ALTER COLUMN c TYPE numeric-16-6]
Q -- USING needed --> S[ALTER TABLE t ALTER COLUMN c\nTYPE newType USING c::newType]
|
| func columnTypeWithModifiers(col *ir.Column) string { | ||
| dt := col.DataType | ||
| if col.MaxLength != nil && (dt == "varchar" || dt == "character varying") { | ||
| return fmt.Sprintf("varchar(%d)", *col.MaxLength) | ||
| } else if col.MaxLength != nil && dt == "character" { | ||
| return fmt.Sprintf("character(%d)", *col.MaxLength) | ||
| } else if col.Precision != nil && col.Scale != nil && (dt == "numeric" || dt == "decimal") { | ||
| return fmt.Sprintf("%s(%d,%d)", dt, *col.Precision, *col.Scale) | ||
| } else if col.Precision != nil && (dt == "numeric" || dt == "decimal") { | ||
| return fmt.Sprintf("%s(%d)", dt, *col.Precision) | ||
| } | ||
| return dt | ||
| } |
There was a problem hiding this comment.
Duplicated modifier-formatting logic
columnTypeWithModifiers is essentially the same as formatColumnDataType (in table.go), which already handles the varchar/character/numeric/decimal cases. The only intentional difference is that columnTypeWithModifiers skips the serial → serial/bigserial rewrite, but formatColumnDataType can also return the raw integer type when the column is not a serial.
Reusing formatColumnDataType (or extracting the shared modifier-building block into a small unexported helper) would keep the logic in one place and reduce the risk of future drift — e.g., if bit(n) or timestamp(p) precision support is added later, it would need to be patched in both files.
Fixes #362