Files
token-circles/worker/migrations/0008_import_idempotency.sql
Komediruzecki 4374535524 fix(import): idempotent /execute via a stable client import_id (CR-7)
The import runs in non-atomic stages (create accounts/categories, then chunked transaction inserts,
then a balance recompute), so a mid-request failure or a lost response could leave a partial import
that a naive retry would duplicate. The client now sends a stable importId (reused across retries,
cleared on reset); the worker stamps it on every inserted transaction (migration 0008) and, on a
re-run, first deletes any rows already created for that id. Balances are recomputed from the
survivors, so they stay correct either way. Old clients (no importId) are unchanged.
2026-06-30 00:54:29 +02:00

11 lines
750 B
SQL

-- Import idempotency (migration 0008).
--
-- POST /api/import/execute does several non-atomic stages (create accounts/categories, then insert
-- transactions in chunks, then recompute balances), so a mid-request failure or a lost response can
-- leave a partial import — and a naive retry would duplicate transactions. The client now sends a
-- stable import_id stamped on every row it creates; on a retry the worker deletes any rows already
-- inserted for that id before re-inserting, making the import safe to re-run. (Balances are then
-- recomputed from the surviving rows, so they stay correct either way.)
ALTER TABLE transactions ADD COLUMN import_id TEXT;
CREATE INDEX IF NOT EXISTS idx_transactions_import_id ON transactions(import_id);