Repository navigation
fix(postgres): replace a changed view that others read, instead of failing to drop it - #273
Merged
Merged
Conversation
…iling to drop it A changed view was dropped and created again, and PostgreSQL refuses to drop a view other views or policies read, so the whole migration failed, unrelated changes with it. A view whose new columns begin with its old ones is now replaced in place with CREATE OR REPLACE VIEW, keeping what reads it, its grants, comments and triggers. Otherwise what reads it — views at any depth, materialised views, policies, and its own triggers — is dropped first and put back complete afterwards, as the side being migrated to has it. What another diff creates or replaces later in that direction is left to that diff. Changed views now run in dependency order, a new view reading a changed one runs after it (it was created first and read the old columns), and a policy, trigger or materialised view reverted before the view it reads only comes off there. Views across schemas are ordered together. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
A changed view was emitted as
DROP VIEW IF EXISTS v; CREATE VIEW v .... PostgreSQL refuses to drop a view that other views, materialised views or policies read:so the whole migration failed and rolled back, unrelated changes included. Reproduction:
Fix
ViewReplacementPlandecides, per changed view and per direction:CREATE OR REPLACE VIEW. What reads it keeps working, and its grants, comments and triggers stay. Grants are then set exactly to the side being migrated to.PostgresColumnDependants::ofRelationfinds them, reusing the column-change machinery.Ordering issues found along the way, all fixed here:
Tests
ViewReplacementRoundTripPostgresTestround-trips every case: UP applies and converges, and DOWN restores the original, with view metadata (options, grants, comments, triggers, indexes) compared directly.INSTEAD OFtrigger, grants and comments;All fail on master. Run on PostgreSQL 14, 15, 16, 17 and 18. The
programmable_objectsfixtures change by one line each: a view that gains a column is nowCREATE OR REPLACE VIEW.🤖 Generated with Claude Code