-- v1.0.9 Day 4 — Backfill hyperswitch_payment_id + payment_status on orders. -- -- Migration 080 (`add_payment_fields`) wraps its ALTERs in a "skip if -- the orders table doesn't exist" guard. At the time 080 was authored, -- orders existed earlier in the migration sequence; that ordering has -- since shifted (orders is now created at 099_z_create_orders.sql, -- AFTER 080). Result: in any freshly-migrated DB (CI runs, fresh dev -- envs, future restore drills) migration 080 takes the skip branch -- and the columns are never added — even though the Order model and -- the marketplace code rely on them. -- -- Symptoms in CI: the periodic ledger_metrics worker -- (internal/monitoring/ledger_metrics.go:164) logs -- pq: column "hyperswitch_payment_id" does not exist -- on every run. The order checkout flow would also fail to persist -- the payment_id at write time, breaking any reconciliation that -- depends on the linkage. -- -- The fix is idempotent (`ADD COLUMN IF NOT EXISTS`) so it safely -- applies to: -- - production envs that *did* see migration 080 in the original -- order (column already there, this migration is a no-op); -- - fresh envs that picked up the broken ordering (column missing, -- this migration adds it). -- -- Adding a backfill at 987 instead of patching 080 in place keeps the -- migration history append-only — re-running the suite from scratch -- arrives at the same end state regardless of when the migration was -- merged. ALTER TABLE orders ADD COLUMN IF NOT EXISTS hyperswitch_payment_id TEXT; ALTER TABLE orders ADD COLUMN IF NOT EXISTS payment_status TEXT DEFAULT 'pending'; -- Index supporting the reconciliation worker's hot path -- (`SELECT ... WHERE status='pending' AND hyperswitch_payment_id IS NOT NULL`). -- Partial because the universe of pending+linked rows is much smaller -- than the full orders table. CREATE INDEX IF NOT EXISTS idx_orders_hyperswitch_payment_id ON orders(hyperswitch_payment_id) WHERE hyperswitch_payment_id IS NOT NULL AND hyperswitch_payment_id <> '';