Update account sync to mark orphaned accounts as inactive

- Added update_account_is_active() function in crud.py
- Updated sync_accounts_from_beancount() to:
  * Mark accounts in Castle DB but not in Beancount as inactive
  * Reactivate accounts that return to Beancount
  * Track deactivated and reactivated counts in sync stats
- Improved sync efficiency with lookup maps
- Enhanced logging for deactivation/reactivation events

This completes the soft delete implementation for orphaned accounts.
When accounts are removed from the Beancount ledger, they are now
automatically marked as inactive in Castle DB during the hourly sync.
This commit is contained in:
padreug 2025-11-11 01:54:04 +01:00
parent 3af9b44e39
commit cb62cbb0a2
2 changed files with 73 additions and 13 deletions

21
crud.py
View file

@ -135,6 +135,27 @@ async def get_accounts_by_type(account_type: AccountType) -> list[Account]:
)
async def update_account_is_active(account_id: str, is_active: bool) -> None:
"""
Update the is_active status of an account (soft delete/reactivate).
Args:
account_id: Account ID to update
is_active: True to activate, False to deactivate
"""
await db.execute(
"""
UPDATE accounts
SET is_active = :is_active
WHERE id = :account_id
""",
{"account_id": account_id, "is_active": is_active},
)
# Invalidate cache
account_cache._values.pop(f"account:id:{account_id}", None)
async def get_or_create_user_account(
user_id: str, account_type: AccountType, base_name: str
) -> Account: