optimize balances view (#1779)

* optimize balances view

flake8

flake8

* fix line length
This commit is contained in:
jackstar12
2023-08-24 12:20:15 +02:00
committed by GitHub
parent 39d717e34c
commit ac0ef07a77

View File

@@ -302,3 +302,20 @@ async def m010_create_installed_extensions_table(db):
);
"""
)
async def m011_optimize_balances_view(db):
"""
Make the calculation of the balance a single aggregation
over the payments table instead of 2.
"""
await db.execute("DROP VIEW balances")
await db.execute(
"""
CREATE VIEW balances AS
SELECT wallet, SUM(amount - abs(fee)) AS balance
FROM apipayments
WHERE (pending = false AND amount > 0) OR amount < 0
GROUP BY wallet
"""
)