-- 41_triggers_examples_audit.sql
-- Banking Pattern Pack — PostgreSQL
-- Purpose: Common BEFORE/AFTER triggers in banking.
-- Context: Retail bank example with customers, accounts, transactions, cards, loans.
-- Date: 2025-08-18
-- Notes:
--   * Safe to read; run on a development DB.
--   * Requires: CREATE privileges; some files may need superuser for extensions.
--   * Search path set to 'bank' schema for clarity.

-- BEFORE/AFTER trigger examples
CREATE OR REPLACE FUNCTION bank.trg_set_tx_desc() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
  IF NEW.description IS NULL THEN
    NEW.description := NEW.tx_type || ' via app';
  END IF;
  RETURN NEW;
END $$;

DROP TRIGGER IF EXISTS trg_tx_desc ON bank.transactions;
CREATE TRIGGER trg_tx_desc BEFORE INSERT ON bank.transactions
FOR EACH ROW EXECUTE FUNCTION bank.trg_set_tx_desc();
