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

25 lines
1 KiB
SQL

-- 023_ResyncFiscalYearsToLedger.sql
-- Re-syncs all fiscal years that are missing from accounting_periods.
-- This fixes periods that failed to sync due to the unique constraint on name
-- (which was removed in migration 022).
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
-- Insert any missing fiscal years
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-%'
AND uuid(substring(aggregate_id from 12))::uuid NOT IN (SELECT id FROM accounting_periods)
ON CONFLICT (id) DO NOTHING;
END IF;
END $$;