-- VA-P26a feature-branch helper only. This is deliberately NOT a migration. -- -- The frozen 20260713190000_appwide_rls_cutover migration installs policies -- whose expressions join the old per-PortalUser commerce tables. Prisma cannot -- drop those referenced tables during `db push` while the policies still -- exist. Remove only the seven reviewed policies before syncing a disposable -- local/CI database to the feature schema. -- -- PortalUserRegistration is an eighth removed legacy table, but its policy -- does not reference the commerce tables and therefore is deliberately not -- part of this narrowly scoped db-push unblocker. Merge migration contract -- (AD-21): the eventual forward-only migration must contain these exact seven -- commerce-policy drops plus the separately reviewed PortalUserRegistration -- policy/table drop before child-first DROP TABLE statements. -- Its Inverse-SQL comment must recreate the tables, indexes, foreign keys and -- policies in parent-first order and explicitly state that deleted row data is -- not recoverable from DDL alone. Do not copy this helper in place of a merge -- migration and do not edit the historical cutover migration. BEGIN; DO $va_p26a_portal_legacy_commerce_recut$ DECLARE legacy_tables CONSTANT text[] := ARRAY[ 'PortalCheckoutSessionStatus', 'PortalSubscriptionFeature', 'PortalSubscriptionPrice', 'PortalSubscriptionProduct', 'PortalUserSubscription', 'PortalUserSubscriptionProduct', 'PortalUserSubscriptionProductPrice' ]; legacy_policies CONSTANT text[] := ARRAY[ 'hg_PortalCheckoutSessionStatus_all', 'hg_PortalSubscriptionFeature_all', 'hg_PortalSubscriptionPrice_all', 'hg_PortalSubscriptionProduct_all', 'hg_PortalUserSubscription_all', 'hg_PortalUserSubscriptionProduct_all', 'hg_PortalUserSubscriptionProductPrice_all' ]; legacy record; remaining_policies text[]; BEGIN IF cardinality(legacy_tables) <> cardinality(legacy_policies) THEN RAISE EXCEPTION 'VA-P26a legacy commerce recut allowlist is malformed'; END IF; FOR legacy IN SELECT * FROM unnest(legacy_tables, legacy_policies) AS allowlist(table_name, policy_name) LOOP IF to_regclass(format('%I.%I', 'public', legacy.table_name)) IS NULL THEN CONTINUE; END IF; EXECUTE format( 'DROP POLICY IF EXISTS %I ON %I.%I', legacy.policy_name, 'public', legacy.table_name ); END LOOP; -- Unknown policies are not removed implicitly. Failing here keeps the whole -- transaction atomic and exposes a new dependency for explicit review. SELECT array_agg( format('%I.%I', relation.relname, policy.polname) ORDER BY relation.relname, policy.polname ) INTO remaining_policies FROM pg_policy AS policy JOIN pg_class AS relation ON relation.oid = policy.polrelid JOIN pg_namespace AS namespace ON namespace.oid = relation.relnamespace WHERE namespace.nspname = 'public' AND relation.relname = ANY (legacy_tables); IF remaining_policies IS NOT NULL THEN RAISE EXCEPTION 'VA-P26a legacy commerce recut found non-allowlisted policies: %', remaining_policies; END IF; END $va_p26a_portal_legacy_commerce_recut$; COMMIT;