Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(accounts): null transactions.import_id before deleting import_logs (#110) (#127)

Tássio Noronha734a9e9b711f
diff --git a/backend/app/services/account_service.py b/backend/app/services/account_service.py
index d96eb05b..98c23cb3 100644
--- a/backend/app/services/account_service.py
+++ b/backend/app/services/account_service.py
@@ -415,9 +415,20 @@ async def delete_account(session: AsyncSession, account_id: uuid.UUID, user_id:
await cleanup_attachment_files(session, tx_ids)
# Break FK references before deleting the account. In production these are
- # also enforced at the DB level (see migration 032) — this code path makes
+ # also enforced at the DB level (see migration 039) — this code path makes
# the behavior explicit and keeps the FK bug from #110 from regressing for
- # any of the three dependent tables.
+ # any of the dependent tables.
+ #
+ # Order matters: transactions imported from a file reference import_logs
+ # via transactions.import_id. We must null that out *before* deleting the
+ # log rows, otherwise the log delete trips transactions_import_id_fkey.
+ # The transaction rows themselves cascade-delete via Account.transactions
+ # when session.delete(account) flushes below.
+ await session.execute(
+ Transaction.__table__.update()
+ .where(Transaction.account_id == account_id)
+ .values(import_id=None)
+ )
await session.execute(
ImportLog.__table__.delete().where(ImportLog.account_id == account_id)
)
diff --git a/backend/tests/test_account_service.py b/backend/tests/test_account_service.py
index 6035b344..e2fffa09 100644
--- a/backend/tests/test_account_service.py
+++ b/backend/tests/test_account_service.py
@@ -373,6 +373,48 @@ async def test_delete_account_with_recurring_transactions(session: AsyncSession,
assert orphan.scalar_one_or_none() is None
+async def test_delete_account_with_imported_transactions(session: AsyncSession, test_user):
+ """Regression (#110 v2, @ivancarlosti): deleting an account whose transactions
+ were imported from a file must succeed. The imported rows reference the
+ import_log via transactions.import_id — deleting import_logs first would
+ trip transactions_import_id_fkey until that reference is cleared."""
+ from sqlalchemy import select
+
+ account = await _make_account(session, test_user.id, "Imported Txns")
+ log = ImportLog(
+ id=uuid.uuid4(), user_id=test_user.id, account_id=account.id,
+ filename="stmt.ofx", format="ofx", transaction_count=1,
+ )
+ session.add(log)
+ await session.flush()
+
+ tx = await _add_txn(
+ session, test_user.id, account.id,
+ amount=42.0, txn_type="debit", txn_date=date.today(),
+ source="import",
+ )
+ tx.import_id = log.id
+ await session.commit()
+ log_id = log.id
+ tx_id = tx.id
+
+ result = await delete_account(session, account.id, test_user.id)
+ assert result is True
+
+ assert await get_account(session, account.id, test_user.id) is None
+
+ session.expire_all()
+ orphan_log = await session.execute(
+ select(ImportLog).where(ImportLog.id == log_id)
+ )
+ assert orphan_log.scalar_one_or_none() is None
+ orphan_tx = await session.execute(
+ select(Transaction).where(Transaction.id == tx_id)
+ )
+ assert orphan_tx.scalar_one_or_none() is None
+
+
@pytest.mark.asyncio
async def test_delete_account_with_linked_goal(session: AsyncSession, test_user):
"""Regression (#110): deleting an account tracked by a goal must succeed;