سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 18:15 UTC
db: align idempotency index migration filename with applied Supabase version 20261011174445
davoodmehraban89 committed
Oct 11, 2026 at 21:15 UTC
4cda84e426c457487147f7a9e49f3b01f6fe5172
1 file changed
+33
supabase/migrations/20261011174445_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: no duplicate posted/reversed source keys (read-only preflight).
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','')<>'';