سامانه صدور فاکتور و حسابداری

test(phase1a): store read-only production legacy RPC definitions for isolated rollback replay

davoodmehraban89 committed Oct 16, 2026 at 14:21 UTC 51d59524bfecf771c03e5018e9dfe9c9dee1a51b
1 file changed +51
supabase/tests/phase1a_restore_legacy_sync_for_rollback.sql new
+51
@@ -0,0 +1,51 @@
1 +-- LEGACY SYNC FUNCTION BODY REPLAY IN DISPOSABLE TEST ROLLBACK ONLY.
2 +-- Captured read-only from Production 2026-10-16. NEVER run against Production.
3 +-- Only SQL function definitions; no tenant records, PII or credentials.
4 +DROP FUNCTION IF EXISTS public.finora_sync_records(jsonb,jsonb);
5 +DROP FUNCTION IF EXISTS public.finora_sync_records(jsonb,jsonb,uuid);
6 +CREATE OR REPLACE FUNCTION public.finora_sync_records(p_upserts jsonb DEFAULT '[]'::jsonb, p_deletes jsonb DEFAULT '[]'::jsonb)
7 + RETURNS void
8 + LANGUAGE plpgsql
9 + SET search_path TO 'public'
10 +AS $function$
11 +begin
12 + if auth.uid() is null then raise exception 'authentication required'; end if;
13 + if not public.has_active_license() then raise exception 'active license required'; end if;
14 + if exists (
15 + select 1 from jsonb_to_recordset(coalesce(p_upserts,'[]'::jsonb)) as x(collection text,id text,data jsonb)
16 + where not private.has_collection_entitlement(x.collection)
17 + ) or exists (
18 + select 1 from jsonb_to_recordset(coalesce(p_deletes,'[]'::jsonb)) as d(collection text,id text)
19 + where not private.has_collection_entitlement(d.collection)
20 + ) then raise exception 'module entitlement required' using errcode='42501'; end if;
21 +
22 + insert into public.records(owner_id, collection, id, data)
23 + select auth.uid(), x.collection, x.id, x.data
24 + from jsonb_to_recordset(coalesce(p_upserts,'[]'::jsonb)) as x(collection text,id text,data jsonb)
25 + on conflict (owner_id,collection,id) do update set data=excluded.data;
26 +
27 + delete from public.records r
28 + using jsonb_to_recordset(coalesce(p_deletes,'[]'::jsonb)) as d(collection text,id text)
29 + where r.owner_id=auth.uid() and r.collection=d.collection and r.id=d.id;
30 +end $function$;
31 +
32 +CREATE OR REPLACE FUNCTION public.finora_sync_records(p_upserts jsonb DEFAULT '[]'::jsonb, p_deletes jsonb DEFAULT '[]'::jsonb, p_organization_id uuid DEFAULT NULL::uuid)
33 + RETURNS void
34 + LANGUAGE plpgsql
35 + SET search_path TO ''
36 +AS $function$
37 +declare v_org uuid:=coalesce(p_organization_id,private.default_organization_id());
38 +begin
39 + if (select auth.uid()) is null then raise exception 'authentication required'; end if;
40 + if v_org is null or v_org not in (select private.current_organization_ids()) then raise exception 'organization membership required' using errcode='42501'; end if;
41 + if not private.organization_has_module_entitlement(v_org,'core',true) then raise exception 'active license required' using errcode='42501'; end if;
42 + if exists(select 1 from jsonb_to_recordset(coalesce(p_upserts,'[]'::jsonb)) as x(collection text,id text,data jsonb) where not private.organization_has_module_entitlement(v_org,private.collection_module(x.collection),true)) or exists(select 1 from jsonb_to_recordset(coalesce(p_deletes,'[]'::jsonb)) as d(collection text,id text) where not private.organization_has_module_entitlement(v_org,private.collection_module(d.collection),true)) then raise exception 'module entitlement required' using errcode='42501'; end if;
43 + insert into public.records(organization_id,owner_id,collection,id,data)
44 + select v_org,(select auth.uid()),x.collection,x.id,x.data from jsonb_to_recordset(coalesce(p_upserts,'[]'::jsonb)) as x(collection text,id text,data jsonb)
45 + on conflict (organization_id,collection,id) do update set data=excluded.data;
46 + delete from public.records r using jsonb_to_recordset(coalesce(p_deletes,'[]'::jsonb)) as d(collection text,id text) where r.organization_id=v_org and r.collection=d.collection and r.id=d.id;
47 +end $function$;
48 +REVOKE ALL ON FUNCTION public.finora_sync_records(jsonb,jsonb) FROM PUBLIC,anon;
49 +REVOKE ALL ON FUNCTION public.finora_sync_records(jsonb,jsonb,uuid) FROM PUBLIC,anon;
50 +GRANT EXECUTE ON FUNCTION public.finora_sync_records(jsonb,jsonb) TO authenticated;
51 +GRANT EXECUTE ON FUNCTION public.finora_sync_records(jsonb,jsonb,uuid) TO authenticated;