Make schema creation idempotent for weekly index#11
Merged
Conversation
The weekly index pipeline runs 'database schema' every week against the existing production DB, but the CREATE statements were unconditional and failed with DuplicateTable. Use IF NOT EXISTS for tables/schema, CREATE OR REPLACE for the view, and a DO/duplicate_object block for enum types (CREATE TYPE has no IF NOT EXISTS).
There was a problem hiding this comment.
Pull request overview
Makes the create_schema function idempotent so the weekly index job can re-run against an existing production database without failing on duplicate-object errors.
Changes:
- Adds
IF NOT EXISTStoCREATE TABLEandCREATE SCHEMAstatements. - Switches the
recordview toCREATE OR REPLACE VIEW. - Wraps
CREATE TYPEenum statements inDO $$ ... EXCEPTION WHEN duplicate_object THEN null; END $$blocks since Postgres lacksCREATE TYPE IF NOT EXISTS.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Why
After the
LOG_*fix (#10), the Weekly Index run got past import but failed at the Create/update schema step:The pipeline runs
database schemaevery week against the existing production DB, but theCREATEstatements were unconditional.What
Make
create_schemaidempotent so re-running is a no-op:CREATE TABLE/CREATE SCHEMA→... IF NOT EXISTSCREATE VIEW record→CREATE OR REPLACE VIEWDO $$ ... EXCEPTION WHEN duplicate_object THEN null; END $$(PostgresCREATE TYPEhas noIF NOT EXISTS)The connection runs with
autocommit=True, so each statement is independent.