Support RENAME VALUE for PostgreSQL ENUM types#1065
Conversation
Replace regex-based parseEnumValuesWithRename with tokenizer-based extractEnumValueComments for consistency with extractColumnComments.
Match the pattern used by Table, Column, and Index for consistency.
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
| } | ||
|
|
||
| // stripQuotes removes surrounding single quotes from a string if present | ||
| func stripQuotes(s string) string { |
There was a problem hiding this comment.
Is it really needed? I can't believe enum vares are stored with quotes.
There was a problem hiding this comment.
Thank you for the review!
Good question! The parser explicitly adds quotes around enum values in parser/parser.y at lines 4123 and 4127:
$$ = append($$, "'" + $1 + "'")Lines 4119 to 4128 in ba33464
So enum values are stored as 'active', 'pending', etc. with surrounding quotes. The stripQuotes function removes these quotes to generate correct SQL like:
ALTER TYPE "public"."status" RENAME VALUE 'pending' TO 'waiting';Without it, we would get double quotes:
ALTER TYPE "public"."status" RENAME VALUE ''pending'' TO ''waiting'';There was a problem hiding this comment.
We could also fix parser.y to not add quotes. In that case, the following changes would be needed:
| File | Line | Change |
|---|---|---|
parser/parser.y |
4123, 4127 | Remove quote addition |
parser/node.go |
897 | Add quotes when outputting |
schema/generator.go |
2756 | Add quotes when outputting |
schema/generator.go |
2371, 2381, 2399-2410 | Remove stripQuotes function and calls |
Total: 5 locations across 3 files.
I'm happy to make this change in this PR if you prefer. Please let me know which approach you'd like me to take.
There was a problem hiding this comment.
Wow! It is surprising.
That parser behavior is totally wrong, and should be fixed.
Would be nice if you can fix the behavir in a separate PR. I'm waiting the next release until that fix.
|
Thank you for your contribution. I'm about to merge this PR, but can you check it a comment I've left? |
Summary
This PR adds support for renaming PostgreSQL ENUM values using the
@renamedannotation, following the same pattern as column, table, and index renaming.Features
ALTER TYPE ... RENAME VALUEgeneration (PostgreSQL 10+)/* @renamed from=old_value */) and line comments (-- @renamed from=old_value) are supportedmyschema.status)ADD VALUEfor simultaneous rename and add operationsExample
Generates:
Implementation Details
EnumValuestruct withrenamedFromfield (usingIdenttype for consistency with column/table/index)extractEnumValueComments) following the same pattern asextractColumnCommentsgenerateDDLsForCreateTypeto handle RENAME VALUE before ADD VALUETest Plan
make buildpassesmake test-all-flavorspasses (except mssqldef due to SQL Server connection)make formatpassesmake lintpassesmake vulncheck- no vulnerabilities foundmake modernizepassesTest Cases Added
RenameEnumValue- Basic rename with block commentRenameEnumValueLineComment- Rename with line commentRenameMultipleEnumValues- Renaming multiple values at onceRenameAndAddEnumValue- Combining rename with addRenameEnumValueWithSchema- Schema-qualified type rename