books/backend/Books.Api/Database/Migrations/010_SyncFiscalYearsToLedger.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

28 lines
1.2 KiB
SQL

-- 010_SyncFiscalYearsToLedger.sql
-- Syncs existing fiscal years from Books.Api to Ledger's accounting_periods table.
-- Required because the LedgerPeriodSyncSubscriber was added after fiscal years were created.
-- Only run if both tables exist (Ledger schema must be set up first)
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'accounting_periods')
AND EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'fiscal_year_read_models') THEN
-- Extract GUID from aggregate_id (format: "fiscalyear-{guid}")
INSERT INTO accounting_periods (id, name, start_date, end_date, is_locked, created_at)
SELECT
uuid(substring(aggregate_id from 12))::uuid as id,
name,
start_date,
end_date,
(status = 'locked') as is_locked,
create_time as created_at
FROM fiscal_year_read_models
WHERE aggregate_id LIKE 'fiscalyear-%'
ON CONFLICT (id) DO UPDATE SET
name = EXCLUDED.name,
start_date = EXCLUDED.start_date,
end_date = EXCLUDED.end_date,
is_locked = EXCLUDED.is_locked;
END IF;
END $$;