سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 17:00 UTC
fix(finance): prepare additive tenant-scoped uniqueness guard for posted source and version
davoodmehraban89 committed
Oct 11, 2026 at 21:04 UTC
b09c476d6eb3e1636eb08c30d88dc52d8c6251af
1 file changed
+33
supabase/migrations/20261011183500_posted_source_idempotency_index.sql
new
+33
@@ -0,0 +1,33 @@
1
+-- Finora: prevent duplicate accounting finalization of the same source/version
2
+-- Data preserving; no existing rows modified. Rollback:
3
+-- DROP INDEX IF EXISTS public.ux_finora_posted_source_identity;
4
+-- Preflight verified 2026-10-11: seven posted/reversed source vouchers, seven distinct source keys.
5
+DO $guard$
6
+BEGIN
7
+ IF EXISTS (
8
+ SELECT 1 FROM public.records r
9
+ WHERE r.collection='journalVouchers'
10
+ AND r.data->>'status' IN ('posted','reversed')
11
+ AND r.data->>'sourceType' IN ('sale','purchase','receipt','payment','expense','income')
12
+ AND coalesce(r.data->>'sourceId','')<>''
13
+ GROUP BY coalesce(r.organization_id::text,r.owner_id::text),
14
+ r.data->>'sourceType',r.data->>'sourceId',
15
+ coalesce(nullif(r.data->>'sourceVersion',''),'1')
16
+ HAVING count(*)>1
17
+ ) THEN
18
+ RAISE EXCEPTION 'Duplicate posted sources exist; resolve before enabling idempotency index';
19
+ END IF;
20
+END
21
+$guard$;
22
+
23
+CREATE UNIQUE INDEX IF NOT EXISTS ux_finora_posted_source_identity
24
+ON public.records (
25
+ (coalesce(organization_id::text,owner_id::text)),
26
+ ((data->>'sourceType')),
27
+ ((data->>'sourceId')),
28
+ (coalesce(nullif(data->>'sourceVersion',''),'1'))
29
+)
30
+WHERE collection='journalVouchers'
31
+ AND data->>'status' IN ('posted','reversed')
32
+ AND data->>'sourceType' IN ('sale','purchase','receipt','payment','expense','income')
33
+ AND coalesce(data->>'sourceId','')<>'';