From 4e69924c9c377a16fe6a7d173e2411e6e616fb4d Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Fri, 2 Dec 2022 17:38:36 +0100 Subject: [PATCH] fix datetime conversion --- lnbits/core/crud.py | 3 ++- lnbits/core/migrations.py | 8 ++++++-- lnbits/db.py | 16 ++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index 8c46ae1a..e111abd5 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -348,6 +348,7 @@ async def delete_expired_invoices( """ ) + # payments # -------- @@ -399,7 +400,7 @@ async def create_payment( if extra and extra != {} and type(extra) is dict else None, webhook, - expiration_date, + db.datetime_to_timestamp(expiration_date), ), ) diff --git a/lnbits/core/migrations.py b/lnbits/core/migrations.py index 7dbfe2f1..a6771585 100644 --- a/lnbits/core/migrations.py +++ b/lnbits/core/migrations.py @@ -197,6 +197,10 @@ async def m006_add_invoice_expiry_to_apipayments(db): Adds invoice expiry field to apipayments and precomputes them for existing entries """ + try: + await db.execute("ALTER TABLE apipayments ADD COLUMN expiry TIMESTAMP") + except OperationalError: + pass try: rows = await ( await db.execute( @@ -206,7 +210,7 @@ async def m006_add_invoice_expiry_to_apipayments(db): WHERE pending = true AND bolt11 IS NOT NULL AND expiry IS NULL - AND amount > 0 AND time < {db.timestamp_now} - {db.interval_seconds(86400)} + AND amount > 0 AND time < {db.timestamp_now} """ ) ).fetchall() @@ -237,7 +241,7 @@ async def m006_add_invoice_expiry_to_apipayments(db): WHERE checking_id = ? AND amount > 0 """, ( - expiration_date, + db.datetime_to_timestamp(expiration_date), invoice.payment_hash, ), ) diff --git a/lnbits/db.py b/lnbits/db.py index e83b4bf8..e4ee3882 100644 --- a/lnbits/db.py +++ b/lnbits/db.py @@ -29,6 +29,13 @@ class Compat: return f"{seconds}" return "" + def datetime_to_timestamp(self, date: datetime.datetime): + if self.type in {POSTGRES, COCKROACH}: + return date.strftime("%Y-%m-%d %H:%M:%S") + elif self.type == SQLITE: + return time.mktime(date.timetuple()) + return "" + @property def timestamp_now(self) -> str: if self.type in {POSTGRES, COCKROACH}: @@ -149,14 +156,7 @@ class Database(Compat): psycopg2.extensions.register_type( psycopg2.extensions.new_type( - (1184, 1114), - "TIMESTAMP2INT", - _parse_timestamp - # lambda value, curs: time.mktime( - # datetime.datetime.strptime( - # value, "%Y-%m-%d %H:%M:%S.%f" - # ).timetuple() - # ), + (1184, 1114), "TIMESTAMP2INT", _parse_timestamp ) ) else: