From 5dbbca0ffbe1d48b3d4d1ff08170d1b2f06f5270 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 14 May 2021 13:12:05 -0300 Subject: [PATCH] improve invoice deleting code. --- lnbits/core/crud.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index b9f02070..47623cc2 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -267,12 +267,23 @@ async def get_payments( async def delete_expired_invoices( conn: Optional[Connection] = None, ) -> None: + # first we delete all invoices older than one month + await (conn or db).execute( + """ + DELETE FROM apipayments + WHERE pending = 1 AND amount > 0 AND time < strftime('%s', 'now') - 2592000 + """ + ) + + # then we delete all expired invoices, checking one by one rows = await (conn or db).fetchall( """ SELECT bolt11 FROM apipayments - WHERE pending = 1 AND amount > 0 AND time < strftime('%s', 'now') - 86400 - """ + WHERE pending = 1 + AND bolt11 IS NOT NULL + AND amount > 0 AND time < strftime('%s', 'now') - 86400 + """ ) for (payment_request,) in rows: try: