From 32596758cca5e09fa1696cf73b5a80bedfb8265b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Mon, 13 May 2024 19:01:53 +0200 Subject: [PATCH] fix: show proper total balances fix cleanups (#2490) payments are not deleted oif we delete a wallets, so to get a accurate total representation of the lnbits balance we need to create the balances view based on the wallets table, not payments, else deleted balances will still show up. 2nd, delete_unused_wallets and delete_accounts was never working if because they never got an updated_at time, so i just check if its null else i check to timedelta on created_at --- lnbits/commands.py | 2 +- lnbits/core/crud.py | 22 ++++++++++++++++++---- lnbits/core/migrations.py | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lnbits/commands.py b/lnbits/commands.py index 584d0ca4..30b88932 100644 --- a/lnbits/commands.py +++ b/lnbits/commands.py @@ -206,7 +206,7 @@ async def database_delete_wallet(wallet: str): @click.option("-c", "--checking-id", required=True, help="Payment checking Id.") @coro async def database_delete_wallet_payment(wallet: str, checking_id: str): - """Mark wallet as deleted""" + """Delete wallet payment""" async with core_db.connect() as conn: await delete_wallet_payment( wallet_id=wallet, checking_id=checking_id, conn=conn diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index dedb9911..4ad1fca7 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -207,14 +207,21 @@ async def delete_accounts_no_wallets( time_delta: int, conn: Optional[Connection] = None, ) -> None: + delta = int(time()) - time_delta await (conn or db).execute( f""" DELETE FROM accounts WHERE NOT EXISTS ( SELECT wallets.id FROM wallets WHERE wallets.user = accounts.id - ) AND updated_at < {db.timestamp_placeholder} + ) AND ( + (updated_at is null AND created_at < {db.timestamp_placeholder}) + OR updated_at < {db.timestamp_placeholder} + ) """, - (int(time()) - time_delta,), + ( + delta, + delta, + ), ) @@ -588,14 +595,21 @@ async def delete_unused_wallets( time_delta: int, conn: Optional[Connection] = None, ) -> None: + delta = int(time()) - time_delta await (conn or db).execute( f""" DELETE FROM wallets WHERE ( SELECT COUNT(*) FROM apipayments WHERE wallet = wallets.id - ) = 0 AND updated_at < {db.timestamp_placeholder} + ) = 0 AND ( + (updated_at is null AND created_at < {db.timestamp_placeholder}) + OR updated_at < {db.timestamp_placeholder} + ) """, - (int(time()) - time_delta,), + ( + delta, + delta, + ), ) diff --git a/lnbits/core/migrations.py b/lnbits/core/migrations.py index ac5b411c..6dde83f4 100644 --- a/lnbits/core/migrations.py +++ b/lnbits/core/migrations.py @@ -491,3 +491,24 @@ async def m018_balances_view_exclude_deleted(db): GROUP BY wallet """ ) + + +async def m019_balances_view_based_on_wallets(db): + """ + Make deleted wallets not show up in the balances view. + Important for querying whole lnbits balances. + """ + await db.execute("DROP VIEW balances") + await db.execute( + """ + CREATE VIEW balances AS + SELECT apipayments.wallet, + SUM(apipayments.amount - ABS(apipayments.fee)) AS balance + FROM wallets + LEFT JOIN apipayments ON apipayments.wallet = wallets.id + WHERE (wallets.deleted = false OR wallets.deleted is NULL) + AND ((apipayments.pending = false AND apipayments.amount > 0) + OR apipayments.amount < 0) + GROUP BY apipayments.wallet + """ + )