Files
backend/database/migrations/097_invoice_account_templates.sql

28 lines
1.1 KiB
SQL

-- Reusable bank account templates (platform or business), parallel to invoice_item_templates.
CREATE TABLE IF NOT EXISTS invoice_account_templates (
id BIGSERIAL PRIMARY KEY,
owner_scope invoice_owner_scope NOT NULL,
business_id BIGINT NULL REFERENCES businesses (id) ON DELETE CASCADE ON UPDATE NO ACTION,
bank_name VARCHAR(255) NOT NULL,
account_holder_name VARCHAR(255) NULL,
card_number VARCHAR(64) NULL,
iban VARCHAR(64) NULL,
sort_order INT NOT NULL DEFAULT 0,
is_active BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_invoice_account_templates_owner_scope
ON invoice_account_templates (owner_scope, sort_order);
CREATE INDEX IF NOT EXISTS idx_invoice_account_templates_business_id
ON invoice_account_templates (business_id);
DROP TRIGGER IF EXISTS invoice_account_templates_set_updated_at ON invoice_account_templates;
CREATE TRIGGER invoice_account_templates_set_updated_at
BEFORE UPDATE ON invoice_account_templates
FOR EACH ROW
EXECUTE FUNCTION set_updated_at();