سامانه صدور فاکتور و حسابداری
fix(db): restore byte-exact prior source guard on rollback
davoodmehraban89 committed
Nov 17, 2026 at 21:34 UTC
125d844956ae2b000b5d55ff168c2420f651ce18
1 file changed
+12
-40
supabase/rollback/20261117120000_wp1_stage3_canonical_source_link_projection.sql
renamed
+12
-40
@@ -38,45 +38,15 @@ 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
-SET search_path = public
45
-AS $$
46
-DECLARE
47
- vid text := coalesce(old.data->>'journalVoucherId','');
48
- voucher_status text := '';
49
- source_locked boolean := false;
50
-BEGIN
51
- IF old.collection NOT IN (
52
- 'invoices','purchases','payments','expenses','cheques','contractStatements',
53
- 'payrollRuns','employeeBenefitAccruals','pettyCashTransactions'
54
- ) THEN
55
- RETURN CASE WHEN tg_op='DELETE' THEN old ELSE new END;
56
- END IF;
57
-
58
- IF vid <> '' THEN
59
- SELECT coalesce(r.data->>'status','') INTO voucher_status
60
- FROM public.records r
61
- WHERE r.owner_id=old.owner_id
62
- AND r.collection='journalVouchers'
63
- AND r.id=vid;
64
- END IF;
65
-
66
- source_locked :=
67
- coalesce(old.data->>'accountingStatus','')='posted'
68
- OR (old.collection='cheques' AND coalesce(old.data->>'status','') IN ('cleared','bounced','cancelled'))
69
- OR (old.collection='contractStatements' AND coalesce(old.data->>'status','') IN ('posted','reversed'))
70
- OR (old.collection IN ('payrollRuns','employeeBenefitAccruals','pettyCashTransactions')
71
- AND coalesce(old.data->>'status','') IN ('posted','reversed'))
72
- OR voucher_status IN ('posted','reversed');
73
-
74
- IF NOT source_locked THEN
75
- RETURN CASE WHEN tg_op='DELETE' THEN old ELSE new END;
76
- END IF;
77
-
78
- IF tg_op='UPDATE' AND new.data=old.data THEN RETURN new; END IF;
79
- RAISE EXCEPTION 'source record linked to posted/reversed accounting history is immutable';
80
-END $$;
41
+-- Restore the exact pre-projection function body from the original migration.
42
+create or replace function public.finora_guard_posted_source_records()
43
+returns trigger language plpgsql set search_path=public as $$
44
+declare vid text:=coalesce(old.data->>'journalVoucherId',''); voucher_status text:=''; source_locked boolean:=false;
45
+begin
46
+ if old.collection not in ('invoices','purchases','payments','expenses','cheques','contractStatements','payrollRuns','employeeBenefitAccruals','pettyCashTransactions') then return case when tg_op='DELETE' then old else new end; end if;
47
+ if vid<>'' then select coalesce(r.data->>'status','') into voucher_status from public.records r where r.owner_id=old.owner_id and r.collection='journalVouchers' and r.id=vid; end if;
48
+ source_locked:=coalesce(old.data->>'accountingStatus','')='posted' or (old.collection='cheques' and coalesce(old.data->>'status','') in ('cleared','bounced','cancelled')) or (old.collection='contractStatements' and coalesce(old.data->>'status','') in ('posted','reversed')) or (old.collection in ('payrollRuns','employeeBenefitAccruals','pettyCashTransactions') and coalesce(old.data->>'status','') in ('posted','reversed')) or voucher_status in ('posted','reversed');
49
+ if not source_locked then return case when tg_op='DELETE' then old else new end; end if;
50
+ if tg_op='UPDATE' and new.data=old.data then return new; end if;
51
+ raise exception 'source record linked to posted/reversed accounting history is immutable';
52
+end $$;