سامانه صدور فاکتور و حسابداری
fix(db): restore exact source guard in WP1 rollback
davoodmehraban89 committed
Nov 17, 2026 at 21:29 UTC
e7f18de32ea045d71f4ebc0b6afaba713630a819
1 file changed
+33
-32
supabase/rollback/20261117120000_wp1_stage3_canonical_source_link_projection.sql
renamed
+33
-32
@@ -38,45 +38,45 @@ REVOKE ALL ON FUNCTION private.finora_stage3_guard_posted_source()
38
FROM PUBLIC, anon, authenticated;
39
40
41
-create or replace function public.finora_guard_posted_source_records()
42
-returns trigger
43
-language plpgsql
44
-security invoker
45
-set search_path = public
46
-as $$
47
-declare
41
+-- Restore the exact source guard that existed before this projection migration.
42
+-- Keep the existing trigger untouched; this migration changed only the function body.
43
+CREATE OR REPLACE FUNCTION public.finora_guard_posted_source_records()
44
+RETURNS trigger
45
+LANGUAGE plpgsql
46
+SET search_path = public
47
+AS $$
48
+DECLARE
49
vid text := coalesce(old.data->>'journalVoucherId','');
50
voucher_status text := '';
51
source_locked boolean := false;
51
-begin
52
- if old.collection not in ('invoices','purchases','payments','expenses','cheques','contractStatements') then
53
- return case when tg_op='DELETE' then old else new end;
54
- end if;
52
+BEGIN
53
+ IF old.collection NOT IN (
54
+ 'invoices','purchases','payments','expenses','cheques','contractStatements',
55
+ 'payrollRuns','employeeBenefitAccruals','pettyCashTransactions'
56
+ ) THEN
57
+ RETURN CASE WHEN tg_op='DELETE' THEN old ELSE new END;
58
+ END IF;
59
56
- if vid <> '' then
57
- select coalesce(r.data->>'status','') into voucher_status
58
- from public.records r
59
- where r.owner_id=old.owner_id
60
- and r.collection='journalVouchers'
61
- and r.id=vid;
62
- end if;
60
+ IF vid <> '' THEN
61
+ SELECT coalesce(r.data->>'status','') INTO voucher_status
62
+ FROM public.records r
63
+ WHERE r.owner_id=old.owner_id
64
+ AND r.collection='journalVouchers'
65
+ AND r.id=vid;
66
+ END IF;
67
68
source_locked :=
69
coalesce(old.data->>'accountingStatus','')='posted'
66
- or (old.collection='cheques' and coalesce(old.data->>'status','') in ('cleared','bounced','cancelled'))
67
- or (old.collection='contractStatements' and coalesce(old.data->>'status','') in ('posted','reversed'))
68
- or voucher_status in ('posted','reversed');
70
+ OR (old.collection='cheques' AND coalesce(old.data->>'status','') IN ('cleared','bounced','cancelled'))
71
+ OR (old.collection='contractStatements' AND coalesce(old.data->>'status','') IN ('posted','reversed'))
72
+ OR (old.collection IN ('payrollRuns','employeeBenefitAccruals','pettyCashTransactions')
73
+ AND coalesce(old.data->>'status','') IN ('posted','reversed'))
74
+ OR voucher_status IN ('posted','reversed');
75
70
- if not source_locked then
71
- return case when tg_op='DELETE' then old else new end;
72
- end if;
73
-
74
- if tg_op='UPDATE' and new.data=old.data then return new; end if;
75
- raise exception 'source record linked to posted/reversed accounting history is immutable';
76
-end $$;
76
+ IF NOT source_locked THEN
77
+ RETURN CASE WHEN tg_op='DELETE' THEN old ELSE new END;
78
+ END IF;
79
78
-drop trigger if exists trg_finora_guard_posted_source_records on public.records;
79
-create trigger trg_finora_guard_posted_source_records
80
-before update or delete on public.records
81
-for each row execute function public.finora_guard_posted_source_records();
80
+ IF tg_op='UPDATE' AND new.data=old.data THEN RETURN new; END IF;
81
+ RAISE EXCEPTION 'source record linked to posted/reversed accounting history is immutable';
82
+END $$;