-- 027_Attachments.sql -- Creates attachment read model table for document/file storage -- Used by AI Bookkeeper for uploaded invoices/receipts CREATE TABLE IF NOT EXISTS attachment_read_models ( -- EventFlow standard columns aggregate_id VARCHAR(255) PRIMARY KEY, create_time TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_time TIMESTAMPTZ NOT NULL DEFAULT NOW(), last_aggregate_sequence_number INT NOT NULL DEFAULT 1, -- Business columns company_id VARCHAR(255) NOT NULL, file_name VARCHAR(500) NOT NULL, original_file_name VARCHAR(500) NOT NULL, content_type VARCHAR(100) NOT NULL, file_size BIGINT NOT NULL, storage_path VARCHAR(1000) NOT NULL, uploaded_by VARCHAR(255) NOT NULL, uploaded_at TIMESTAMPTZ NOT NULL, draft_id VARCHAR(255), transaction_id VARCHAR(255), retention_end_date TIMESTAMPTZ NOT NULL, is_deleted BOOLEAN NOT NULL DEFAULT FALSE, deleted_by VARCHAR(255), delete_reason TEXT, deleted_at TIMESTAMPTZ, CONSTRAINT fk_attachment_company FOREIGN KEY (company_id) REFERENCES company_read_models(aggregate_id) ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS idx_attachment_company ON attachment_read_models(company_id); CREATE INDEX IF NOT EXISTS idx_attachment_draft ON attachment_read_models(draft_id) WHERE draft_id IS NOT NULL; CREATE INDEX IF NOT EXISTS idx_attachment_transaction ON attachment_read_models(transaction_id) WHERE transaction_id IS NOT NULL; CREATE INDEX IF NOT EXISTS idx_attachment_not_deleted ON attachment_read_models(company_id, is_deleted) WHERE is_deleted = FALSE;