diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index a9af0f97..9233a902 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -587,11 +587,6 @@ async def get_total_balance(conn: Optional[Connection] = None): return 0 if row[0] is None else row[0] -async def get_active_wallet_total_balance(conn: Optional[Connection] = None): - row = await (conn or db).fetchone("SELECT SUM(balance) FROM balances") - return 0 if row[0] is None else row[0] - - # wallet payments # --------------- diff --git a/lnbits/core/migrations.py b/lnbits/core/migrations.py index 3f511c36..ac5b411c 100644 --- a/lnbits/core/migrations.py +++ b/lnbits/core/migrations.py @@ -471,3 +471,23 @@ async def m017_add_timestamp_columns_to_accounts_and_wallets(db): except OperationalError as exc: logger.error(f"Migration 17 failed: {exc}") pass + + +async def m018_balances_view_exclude_deleted(db): + """ + Make deleted wallets not show up in the balances view. + """ + 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 apipayments + LEFT JOIN wallets 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 wallet + """ + )