[PR #159 follow-up] Fix Supabase migration idempotency and preserve orders tracking#184
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
This pull request has been ignored for the connected project Preview Branches by Supabase. |
|
Caution Review failedPull request was closed or merged during review WalkthroughCinco migrações Supabase SQL são refatoradas para idempotência. Novas tabelas de token e índices agora usam ChangesPadronização de idempotência em migrações Supabase
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Justificativa: O padrão de mudança é repetitivo e homogêneo (5 migrações seguindo o mesmo princípio de idempotência), reduzindo esforço. Porém, a migração 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Pull request overview
This PR hardens several Supabase SQL migrations so they can be replayed safely (idempotent creation of tables/indexes/policies/triggers) and attempts to preserve orders tracking data when migrating from tracking_code to tracking_number.
Changes:
- Added
IF NOT EXISTS/ existence-guardedDO $$ ... $$blocks around table/index/policy/trigger creation to prevent duplicate-object failures on migration replays. - Guarded
quote_templateslegacy column renames so they only run when the source column exists and the target does not. - Updated the
ordersalignment migration to be more replay-tolerant and to backfilltracking_codeintotracking_numberbefore dropping the legacy column.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| supabase/migrations/20260522145140_wave_3_3_c_1_oficial_quote_templates_unificado.sql | Makes quote_templates column renames idempotent via guarded DO $$ blocks. |
| supabase/migrations/20260522143654_wave_3_2_a_orders_oficial_align_client_v2.sql | Adds replay guards for orders column changes and backfills tracking data. |
| supabase/migrations/20260522114122_lote_c_02_kit_share_tokens.sql | Makes kit_share_tokens table/index/policy/trigger creation idempotent. |
| supabase/migrations/20260522113314_lote_a_04_quote_approval_tokens.sql | Makes quote_approval_tokens table/index/policy/trigger creation idempotent. |
| supabase/migrations/20260522113220_lote_a_01_public_token_failures.sql | Makes public_token_failures table/index/policy creation idempotent. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ALTER TABLE public.orders DROP COLUMN IF EXISTS total; | ||
| DO $$ | ||
| BEGIN | ||
| IF EXISTS ( | ||
| SELECT 1 FROM information_schema.columns | ||
| WHERE table_schema='public' AND table_name='orders' AND column_name='total_amount' | ||
| ) AND NOT EXISTS ( | ||
| SELECT 1 FROM information_schema.columns | ||
| WHERE table_schema='public' AND table_name='orders' AND column_name='total' | ||
| ) THEN | ||
| ALTER TABLE public.orders RENAME COLUMN total_amount TO total; |
| UPDATE public.orders | ||
| SET tracking_number = COALESCE(NULLIF(tracking_number, ''), tracking_code) | ||
| WHERE tracking_code IS NOT NULL; | ||
|
|
||
| ALTER TABLE public.orders DROP COLUMN tracking_code; |
Motivation
orders.tracking_codewhen schema evolves.Description
IF NOT EXISTStoCREATE TABLE/CREATE INDEXforpublic_token_failures,quote_approval_tokens, andkit_share_tokensand wrapped policy creation in guardedDO $$ ... IF NOT EXISTS ... $$blocks againstpg_policiesto avoid duplicate-policy errors (files:20260522113220_*,20260522113314_*,20260522114122_*).pg_triggerexistence checks and replaced unconditional policy creation with existence-checkedDO $$blocks forquote_approval_tokensandkit_share_tokensto prevent trigger/policy collisions on replay.ordersmigration resilient by usingDROP COLUMN IF EXISTSfortotal, performing a guarded renametotal_amount -> totalonly when source exists and target does not, and preserving tracking data by backfillingtracking_codeintotracking_number(without overwriting non-empty values) before dropping the legacy column (file:20260522143654_*).quote_templateslegacy-column renames so each rename runs only when the legacy column exists and the target column is absent to avoidcolumn already existserrors (file:20260522145140_*).Testing
git diff --checkand it completed with no whitespace or conflict marker issues.git status --shortand commit succeeded (Fix migration replay idempotency and preserve orders tracking data).supabase db resetor full migration replay in this environment, so runtime migration replays remain to be validated in CI or a staging database.Codex Task
Summary by cubic
Makes Supabase migrations safe to replay and preserves orders tracking data when moving from tracking_code to tracking_number. Prevents duplicate-object errors and protects existing data during rollouts.
Bug Fixes
public_token_failures,quote_approval_tokens, andkit_share_tokens.pg_policieschecks and trigger creation viapg_triggerchecks.ordersmigration: conditional drop/rename fortotal_amount -> total; backfilltracking_codeintotracking_numberwithout overwriting non-empty values; then drop the legacy column.quote_templatescolumn renames to avoid “column already exists” errors.Migration
Written for commit 4896ff0. Summary will update on new commits. Review in cubic
Summary by CodeRabbit
Bug Fixes
Chores