From edd72fd5db3f4c50b8d28adf8334ed2e3fe9828e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Wed, 2 Oct 2024 11:31:07 +0200 Subject: [PATCH] add migration for view --- lnbits/core/migrations.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lnbits/core/migrations.py b/lnbits/core/migrations.py index 03ec27401..a11e14984 100644 --- a/lnbits/core/migrations.py +++ b/lnbits/core/migrations.py @@ -565,3 +565,22 @@ async def m023_add_column_column_to_apipayments(db): async def m024_drop_pending(db): await db.execute("ALTER TABLE apipayments DROP COLUMN pending") + + +async def m025_refresh_view(db): + await db.execute("DROP VIEW balances") + await db.execute( + """ + CREATE VIEW balances AS + SELECT apipayments.wallet_id, + SUM(apipayments.amount - ABS(apipayments.fee)) AS balance + FROM wallets + LEFT JOIN apipayments ON apipayments.wallet_id = wallets.id + WHERE (wallets.deleted = false OR wallets.deleted is NULL) + AND ( + (apipayments.status = 'success' AND apipayments.amount > 0) + OR (apipayments.status IN ('success', 'pending') AND apipayments.amount < 0) + ) + GROUP BY apipayments.wallet_id + """ + )