books/backend/Books.Api/Database/Migrations/027_Attachments.sql
Nicolaj Hartmann 1f75c5d791 Add all backend domain, commands, repositories, and tests
This commit includes all previously untracked backend files:

Domain:
- Accounts, Attachments, BankConnections, Customers
- FiscalYears, Invoices, JournalEntryDrafts
- Orders, Products, UserAccess

Commands & Handlers:
- Full CQRS command structure for all domains

Repositories:
- PostgreSQL repositories for all read models
- Bank transaction and ledger repositories

GraphQL:
- Input types, scalars, and types for all entities
- Mutations and queries

Infrastructure:
- Banking integration (Enable Banking client)
- File storage, Invoicing, Reporting, SAF-T export
- Database migrations (003-029)

Tests:
- Integration tests for GraphQL endpoints
- Domain tests
- Invoicing and reporting tests

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 22:19:42 +01:00

36 lines
1.6 KiB
SQL

-- 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;