-- Layer 2 + Layer 3 of the double-entry enforcement design (see the implementation plan, -- section "Double-Entry Enforcement"). Layer 1 is PostingService.postEntry() in -- src/modules/gl/posting.service.ts, which is the primary guard. These are the DB-level -- backstops that make an unbalanced journal impossible to persist even via a bypass of -- that service (raw SQL, a future bug, a manual DB edit). -- -- Not expressible in Prisma's schema.prisma DSL, so this is applied as hand-written SQL -- inside an otherwise-empty Prisma migration. See SETUP.md for how to wire this in. -- Layer 2: row-level guards on JournalEntryLine. ALTER TABLE "JournalEntryLine" ADD CONSTRAINT chk_line_nonnegative CHECK (debit >= 0 AND credit >= 0); ALTER TABLE "JournalEntryLine" ADD CONSTRAINT chk_line_debit_xor_credit CHECK ((debit > 0 AND credit = 0) OR (credit > 0 AND debit = 0)); -- Layer 3: deferred trigger that re-sums debits/credits per journal_entry_id at commit -- and rejects the transaction if they don't match. This is the real hard guarantee. CREATE OR REPLACE FUNCTION trg_check_journal_entry_balance() RETURNS trigger AS $$ DECLARE affected_entry_id INTEGER; total_debit NUMERIC(18,2); total_credit NUMERIC(18,2); BEGIN affected_entry_id := COALESCE(NEW."journalEntryId", OLD."journalEntryId"); SELECT COALESCE(SUM(debit), 0), COALESCE(SUM(credit), 0) INTO total_debit, total_credit FROM "JournalEntryLine" WHERE "journalEntryId" = affected_entry_id; IF total_debit <> total_credit THEN RAISE EXCEPTION 'JournalEntry % does not balance: debits % != credits %', affected_entry_id, total_debit, total_credit; END IF; RETURN NULL; END; $$ LANGUAGE plpgsql; CREATE CONSTRAINT TRIGGER journal_entry_line_balance_check AFTER INSERT OR UPDATE OR DELETE ON "JournalEntryLine" DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION trg_check_journal_entry_balance(); -- Extra guard: at most one AccountSegmentDefinition may be flagged as the branch segment. -- Prisma's schema DSL can't express a partial unique index, so it's added here. CREATE UNIQUE INDEX one_branch_segment_only ON "AccountSegmentDefinition" (("isBranchSegment")) WHERE "isBranchSegment" = true;