سامانه صدور فاکتور و حسابداری
feat: implement isolated transactional attachment allocator candidate (#216)
davoodmehraban89 committed
Nov 18, 2026 at 09:37 UTC
62ff61b3e0d4b1c1a828c8249c2afae3a42283dc
1 file changed
+48
supabase/candidates/0216_attachment_registry.sql
new
+48
@@ -0,0 +1,48 @@
1
+-- Candidate only: not a deployed migration. Internal allocator for ADR 0216.
2
+-- Invoke only from a previously authorized journal transaction.
3
+CREATE TABLE private.finora_attachment_counters (
4
+ organization_id uuid NOT NULL,
5
+ company_id text NOT NULL CHECK (btrim(company_id)<>''),
6
+ last_number bigint NOT NULL CHECK (last_number BETWEEN 1 AND 9007199254740991),
7
+ PRIMARY KEY (organization_id,company_id)
8
+);
9
+CREATE TABLE private.finora_attachment_registry (
10
+ organization_id uuid NOT NULL,
11
+ company_id text NOT NULL CHECK (btrim(company_id)<>''),
12
+ voucher_id text NOT NULL CHECK (btrim(voucher_id)<>''),
13
+ attachment_number bigint NOT NULL CHECK (attachment_number BETWEEN 1 AND 9007199254740991),
14
+ issued_at timestamptz NOT NULL DEFAULT clock_timestamp(),
15
+ PRIMARY KEY (organization_id,voucher_id),
16
+ UNIQUE (organization_id,company_id,attachment_number)
17
+);
18
+ALTER TABLE private.finora_attachment_counters ENABLE ROW LEVEL SECURITY;
19
+ALTER TABLE private.finora_attachment_registry ENABLE ROW LEVEL SECURITY;
20
+REVOKE ALL ON private.finora_attachment_counters,private.finora_attachment_registry FROM PUBLIC,anon,authenticated,service_role;
21
+
22
+CREATE FUNCTION private.finora_reserve_attachment(p_org uuid,p_company text,p_voucher text)
23
+RETURNS bigint LANGUAGE plpgsql SECURITY INVOKER SET search_path='' AS $f$
24
+DECLARE existing private.finora_attachment_registry%ROWTYPE; n bigint;
25
+BEGIN
26
+ IF p_org IS NULL OR coalesce(btrim(p_company),'')='' OR coalesce(btrim(p_voucher),'')='' THEN
27
+ RAISE EXCEPTION 'FINORA_ATTACHMENT_IDENTITY_REQUIRED' USING ERRCODE='23514';
28
+ END IF;
29
+ -- Serialize retries of the same voucher even if they supply different companies.
30
+ PERFORM pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtextextended(p_org::text||':'||p_voucher,216));
31
+ SELECT * INTO existing FROM private.finora_attachment_registry
32
+ WHERE organization_id=p_org AND voucher_id=p_voucher;
33
+ IF FOUND THEN
34
+ IF existing.company_id IS DISTINCT FROM p_company THEN
35
+ RAISE EXCEPTION 'FINORA_ATTACHMENT_COMPANY_IMMUTABLE' USING ERRCODE='23514';
36
+ END IF;
37
+ RETURN existing.attachment_number;
38
+ END IF;
39
+ INSERT INTO private.finora_attachment_counters AS c(organization_id,company_id,last_number)
40
+ VALUES(p_org,p_company,1)
41
+ ON CONFLICT (organization_id,company_id) DO UPDATE SET last_number=c.last_number+1
42
+ RETURNING last_number INTO n;
43
+ INSERT INTO private.finora_attachment_registry(organization_id,company_id,voucher_id,attachment_number)
44
+ VALUES(p_org,p_company,p_voucher,n);
45
+ RETURN n;
46
+END $f$;
47
+REVOKE ALL ON FUNCTION private.finora_reserve_attachment(uuid,text,text) FROM PUBLIC,anon,authenticated,service_role;
48
+-- Do not integrate until historical high-water marks and tombstones are reconciled.