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

17 lines
749 B
SQL

-- 024_CleanOrphanIdempotencyKeys.sql
-- Removes orphaned idempotency keys for invoices that were never successfully posted.
-- These occur when the process_transaction function fails AFTER inserting the idempotency key
-- but BEFORE creating ledger entries (e.g., due to missing period).
-- Only run if the processed_transactions table exists (it's in ledger schema)
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'processed_transactions') THEN
DELETE FROM processed_transactions pt
WHERE pt.idempotency_key LIKE 'invoice-send-%'
AND NOT EXISTS (
SELECT 1 FROM ledger_entries le
WHERE le.idempotency_key = pt.idempotency_key
);
END IF;
END $$;