fix datetime conversion

This commit is contained in:
callebtc 2022-12-02 17:38:36 +01:00
parent 79ffbb7bc2
commit 4e69924c9c
3 changed files with 16 additions and 11 deletions

View file

@ -348,6 +348,7 @@ async def delete_expired_invoices(
""" """
) )
# payments # payments
# -------- # --------
@ -399,7 +400,7 @@ async def create_payment(
if extra and extra != {} and type(extra) is dict if extra and extra != {} and type(extra) is dict
else None, else None,
webhook, webhook,
expiration_date, db.datetime_to_timestamp(expiration_date),
), ),
) )

View file

@ -197,6 +197,10 @@ async def m006_add_invoice_expiry_to_apipayments(db):
Adds invoice expiry field to apipayments and precomputes them for Adds invoice expiry field to apipayments and precomputes them for
existing entries existing entries
""" """
try:
await db.execute("ALTER TABLE apipayments ADD COLUMN expiry TIMESTAMP")
except OperationalError:
pass
try: try:
rows = await ( rows = await (
await db.execute( await db.execute(
@ -206,7 +210,7 @@ async def m006_add_invoice_expiry_to_apipayments(db):
WHERE pending = true WHERE pending = true
AND bolt11 IS NOT NULL AND bolt11 IS NOT NULL
AND expiry IS 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() ).fetchall()
@ -237,7 +241,7 @@ async def m006_add_invoice_expiry_to_apipayments(db):
WHERE checking_id = ? AND amount > 0 WHERE checking_id = ? AND amount > 0
""", """,
( (
expiration_date, db.datetime_to_timestamp(expiration_date),
invoice.payment_hash, invoice.payment_hash,
), ),
) )

View file

@ -29,6 +29,13 @@ class Compat:
return f"{seconds}" return f"{seconds}"
return "<nothing>" return "<nothing>"
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 "<nothing>"
@property @property
def timestamp_now(self) -> str: def timestamp_now(self) -> str:
if self.type in {POSTGRES, COCKROACH}: if self.type in {POSTGRES, COCKROACH}:
@ -149,14 +156,7 @@ class Database(Compat):
psycopg2.extensions.register_type( psycopg2.extensions.register_type(
psycopg2.extensions.new_type( psycopg2.extensions.new_type(
(1184, 1114), (1184, 1114), "TIMESTAMP2INT", _parse_timestamp
"TIMESTAMP2INT",
_parse_timestamp
# lambda value, curs: time.mktime(
# datetime.datetime.strptime(
# value, "%Y-%m-%d %H:%M:%S.%f"
# ).timetuple()
# ),
) )
) )
else: else: