Repository navigation
fix(postgres-migration): make the seed re-runnable - #99
Merged
Merged
Conversation
A seed that died partway could not be restarted. The second run re-inserted products, hit sku's unique constraint and aborted, so recovering meant manual cleanup before the file would run at all -- and the file gives no hint of that. Guard each INSERT with a count against its own target. Each table is loaded by one statement, so a table is either empty of seed rows or complete and there is no partially-seeded state for the guard to misjudge. The orders and order_items guards compare against the target rather than testing for emptiness, because app/writer.py inserts into both and would otherwise make them look finished while the seed rows are still missing. Verified in four states: fresh load, re-run when fully seeded, re-run after losing only order_items, and re-run with writer rows present. Re-running against an already-seeded RDS instance now reports four INSERT 0 0 and takes 20 seconds. Co-Authored-By: Claude Opus 5 (1M context) <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.
Follow-up to #98, which is already merged. Same workshop, unrelated defect, found while running module 01 end to end on a live RDS stack.
The problem
A seed that died partway could not be restarted. The second run re-inserted products, hit
sku's unique constraint and aborted, so recovering meant manual cleanup before the file would run at all -- and nothing in the file hinted at that.This is not hypothetical. It is exactly what a killed or interrupted seed leaves you holding, and the seed is a 29-minute unattended job that participants are told to start early and walk away from.
The fix
Each
INSERTnow carries a count guard against its own target:Each table is loaded by one statement, so a table is either empty of seed rows or complete -- there is no partially-seeded state for the guard to misjudge.
The
ordersandorder_itemsguards compare against the target rather than testing for emptiness, which matters:app/writer.pyinserts into both, so an emptiness test would see the writer's rows and conclude the seed was finished while its 12M orders and 48M items were still missing.The header now documents that the file is re-runnable, and gives the
TRUNCATE ... RESTART IDENTITYline for the separate case of reseeding at a different size.Verification
Exercised in four states, on both a local Postgres 17 container and the live RDS instance:
INSERT 0 0x4order_itemslost, rest intactorder_itemsordersINSERT 0 0x4Against the already-seeded RDS instance a re-run reports four
INSERT 0 0and completes in 20 seconds. Before this change that run aborted.The playbook half of this workshop's fixes is in WorkshopHouse#60.
🤖 Generated with Claude Code