-
Notifications
You must be signed in to change notification settings - Fork 53
Fix: Support GENERATED ALWAYS AS columns to reduce migration failures (#212)
#232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@bplunkett-stripe @alexaub-stripe LGTM, I believe it's ready |
GENERATED ALWAYS AS columns (#212)
GENERATED ALWAYS AS columns (#212)GENERATED ALWAYS AS columns to reduce migration failures (#212)
|
I expect we will want a more advanced solution. I actually prototyped something a few months ago But we can merge this in the interim to unblock the use case |
|
Looks like there are some build issues! |
- Regenerate sqlc files with v1.29.0 to match queries.sql changes - Apply gofmt -s formatting to fix spacing and import order issues - Fixes assert-no-diff and golangci-lint CI checks Resolves build issues from PR stripe#232
That makes sense |
|
build issues now passing on my local machine the test failures appear unrelated |
|
You will need to bump the schema SHA! I haven't looked at the unrestrict/restrict stuff, but it relates to somehow pg dumping autogenerating some identifier. Let's try to fix that before merging |
Sounds good, getting more familiar with the docker workflow today and looking in to it |
|
This commit should fix the |
|
local docker testing confirmed:
|
bplunkett-stripe
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Thanks for doing this!
|
Great, thanks for the collaboration on the CI! The fix properly handles GENERATED ALWAYS AS columns now. I'll keep an eye out for any related issues that pop up |
Problem Statement (#212)
pg-schema-diff incorrectly treats
GENERATED ALWAYS AS (expression) STOREDcolumns asDEFAULTcolumns, causing migration failures:Example: A
tsvectorcolumn withGENERATED ALWAYS AS (to_tsvector('simple', title || ' ' || coalesce(artist, ''))) STOREDgets incorrectly converted toDEFAULT to_tsvector(...), which fails because DEFAULT expressions cannot reference other columns.Root Cause Analysis
Schema Introspection (
internal/queries/queries.sql:91-151):GetColumnsForTablequery doesn't checkpg_attribute.attgeneratedDDL Generation (
pkg/diff/sql_generator.go:2671):buildColumnDefinitionfunction only handles DEFAULT expressionsGENERATED ALWAYS AS ... STOREDsyntaxSolution
1. Update Schema Introspection ✅
Modified
internal/queries/queries.sql:94-106:pg_attribute.attgenerateddetection ('s' = STORED generated column)Updated
internal/queries/queries.sql.gowith new struct fields:GenerationExpression stringIsGenerated bool2. Extend Column Model ✅
Enhanced
internal/schema/schema.go:268-275:Updated
internal/schema/schema.go:992-996column building logic:3. Fix DDL Generation ✅
Fixed
pkg/diff/sql_generator.go:2677-2681:Test Plan for Reviewers
1. Automated Test Validation (Required)
Run the automated test suite to verify all generated column functionality:
Success Criteria:
2. Manual End-to-End Validation (Optional)
To manually verify the fix resolves the original issue:
Manual Test Steps
Expected Behavior:
GENERATED ALWAYS AS ... STORED(notDEFAULT)3. Regression Testing (Required)
Verify existing functionality remains intact:
Full Regression Testing (Recommended):
Note: Docker-based testing provides consistent environment and runs all integration tests including schema hash validations. This is the same test suite used in CI.
Impact
This fix properly supports PostgreSQL's generated columns feature (PostgreSQL 12+), commonly used for:
tsvectorcolumns)price * tax_rate)(data->>'field')::type)upper(name),lower(email))Docs:
https://www.postgresql.org/docs/current/ddl-generated-columns.html