-- Run separately BEFORE migration 000110, against the intended database. -- Review docs/subscription-plan-cleanup.md first. No automatic winner selection. BEGIN; SET LOCAL lock_timeout = '10s'; SET LOCAL statement_timeout = '60s'; SET LOCAL search_path = public, pg_catalog; -- Prevent concurrent plan/reference changes between archival and consolidation. LOCK TABLE subscription_plans, user_subscriptions, payments IN EXCLUSIVE MODE; DO $$ BEGIN IF current_setting('rezina.approve_plan_consolidation', true) IS DISTINCT FROM 'yes' THEN RAISE EXCEPTION 'Review the historical-plan change, then explicitly SET rezina.approve_plan_consolidation = ''yes'' in this session'; END IF; IF (SELECT count(*) FROM subscription_plans WHERE id IN (2, 9, 14) AND category = 'LEARN_ENGLISH' AND NOT is_lifetime AND duration_value = 1 AND duration_unit = 'MONTH' AND currency = 'ETB' AND ((id = 2 AND price = 300 AND is_active) OR (id = 9 AND price = 1 AND NOT is_active) OR (id = 14 AND price = 0 AND NOT is_active))) <> 3 THEN RAISE EXCEPTION 'Plans differ from the reviewed backup; stop and review current data'; END IF; IF EXISTS (SELECT 1 FROM payments WHERE plan_id IN (9, 14) AND status IN ('PENDING', 'PROCESSING')) THEN RAISE EXCEPTION 'Duplicate plans have in-flight payments; resolve them before consolidation'; END IF; -- Unknown references must be reviewed, even if their FK would silently SET NULL/CASCADE. IF EXISTS (SELECT 1 FROM pg_constraint WHERE contype = 'f' AND confrelid = 'public.subscription_plans'::regclass AND NOT (conrelid IN ('public.payments'::regclass, 'public.user_subscriptions'::regclass) AND conkey = ARRAY[(SELECT attnum FROM pg_attribute WHERE attrelid = conrelid AND attname = 'plan_id')]::smallint[] AND confkey = ARRAY[(SELECT attnum FROM pg_attribute WHERE attrelid = confrelid AND attname = 'id')]::smallint[])) THEN RAISE EXCEPTION 'Unexpected foreign key references subscription_plans; review before consolidation'; END IF; END $$; -- Deliberately fail on a repeated run; never overwrite the original archive. CREATE SCHEMA plan_cleanup_000110; REVOKE ALL ON SCHEMA plan_cleanup_000110 FROM PUBLIC; CREATE TABLE plan_cleanup_000110.mapping ( old_plan_id bigint PRIMARY KEY, canonical_plan_id bigint NOT NULL, archived_at timestamptz NOT NULL DEFAULT now() ); INSERT INTO plan_cleanup_000110.mapping (old_plan_id, canonical_plan_id) VALUES (9, 2), (14, 2); CREATE TABLE plan_cleanup_000110.subscription_plans AS SELECT * FROM public.subscription_plans WHERE id IN (2, 9, 14); CREATE TABLE plan_cleanup_000110.user_subscriptions AS SELECT * FROM public.user_subscriptions WHERE plan_id IN (9, 14); CREATE TABLE plan_cleanup_000110.payments AS SELECT * FROM public.payments WHERE plan_id IN (9, 14); REVOKE ALL ON ALL TABLES IN SCHEMA plan_cleanup_000110 FROM PUBLIC; UPDATE public.user_subscriptions AS s SET plan_id = m.canonical_plan_id FROM plan_cleanup_000110.mapping AS m WHERE s.plan_id = m.old_plan_id; UPDATE public.payments AS p SET plan_id = m.canonical_plan_id FROM plan_cleanup_000110.mapping AS m WHERE p.plan_id = m.old_plan_id; DELETE FROM public.subscription_plans WHERE id IN (9, 14); -- Ensure the full catalog satisfies the intended rule before committing anything. -- Migration 000110 subsequently sees this exact index and leaves it in place. CREATE UNIQUE INDEX idx_subscription_plans_category_period ON public.subscription_plans (category, is_lifetime, duration_value, duration_unit); DO $$ BEGIN IF EXISTS ( SELECT 1 FROM plan_cleanup_000110.payments a LEFT JOIN public.payments p USING (id) WHERE p.id IS NULL OR p.plan_id <> 2 OR (to_jsonb(p) - 'plan_id') IS DISTINCT FROM (to_jsonb(a) - 'plan_id') ) OR EXISTS ( SELECT 1 FROM plan_cleanup_000110.user_subscriptions a LEFT JOIN public.user_subscriptions s USING (id) WHERE s.id IS NULL OR s.plan_id <> 2 OR (to_jsonb(s) - 'plan_id') IS DISTINCT FROM (to_jsonb(a) - 'plan_id') ) THEN RAISE EXCEPTION 'Reference consolidation changed fields other than plan_id'; END IF; END $$; COMMIT;