update migrations.py

This commit is contained in:
dni ⚡
2024-10-17 10:42:28 +02:00
parent 75698bfd5c
commit 3a1064c841
+38 -8
View File
@@ -1,3 +1,4 @@
import json
from time import time from time import time
from loguru import logger from loguru import logger
@@ -99,9 +100,8 @@ async def m002_add_fields_to_apipayments(db):
await db.execute("ALTER TABLE apipayments ADD COLUMN bolt11 TEXT") await db.execute("ALTER TABLE apipayments ADD COLUMN bolt11 TEXT")
await db.execute("ALTER TABLE apipayments ADD COLUMN extra TEXT") await db.execute("ALTER TABLE apipayments ADD COLUMN extra TEXT")
import json result = await db.execute("SELECT * FROM apipayments")
rows = result.mappings().all()
rows = await db.fetchall("SELECT * FROM apipayments")
for row in rows: for row in rows:
if not row["memo"] or not row["memo"].startswith("#"): if not row["memo"] or not row["memo"].startswith("#"):
continue continue
@@ -211,7 +211,7 @@ async def m007_set_invoice_expiries(db):
Precomputes invoice expiry for existing pending incoming payments. Precomputes invoice expiry for existing pending incoming payments.
""" """
try: try:
rows = await db.fetchall( result = await db.execute(
f""" f"""
SELECT bolt11, checking_id SELECT bolt11, checking_id
FROM apipayments FROM apipayments
@@ -222,6 +222,7 @@ async def m007_set_invoice_expiries(db):
AND time < {db.timestamp_now} AND time < {db.timestamp_now}
""" """
) )
rows = result.mappings().all()
if len(rows): if len(rows):
logger.info(f"Migration: Checking expiry of {len(rows)} invoices") logger.info(f"Migration: Checking expiry of {len(rows)} invoices")
for i, ( for i, (
@@ -339,7 +340,7 @@ async def m014_set_deleted_wallets(db):
Sets deleted column to wallets. Sets deleted column to wallets.
""" """
try: try:
rows = await db.fetchall( result = await db.execute(
""" """
SELECT * SELECT *
FROM wallets FROM wallets
@@ -348,12 +349,13 @@ async def m014_set_deleted_wallets(db):
AND inkey LIKE 'del:%' AND inkey LIKE 'del:%'
""" """
) )
rows = result.mappings().all()
for row in rows: for row in rows:
try: try:
user = row[2].split(":")[1] user = row["user"].split(":")[1]
adminkey = row[3].split(":")[1] adminkey = row["adminkey"].split(":")[1]
inkey = row[4].split(":")[1] inkey = row["inkey"].split(":")[1]
await db.execute( await db.execute(
""" """
UPDATE wallets SET UPDATE wallets SET
@@ -586,3 +588,31 @@ async def m025_refresh_view(db):
GROUP BY apipayments.wallet_id GROUP BY apipayments.wallet_id
""" """
) )
async def m026_update_payment_table(db):
await db.execute("ALTER TABLE apipayments ADD COLUMN tag TEXT")
await db.execute("ALTER TABLE apipayments ADD COLUMN extension TEXT")
await db.execute("ALTER TABLE apipayments ADD COLUMN created_at TIMESTAMP")
await db.execute("ALTER TABLE apipayments ADD COLUMN updated_at TIMESTAMP")
result = await db.execute("SELECT * FROM apipayments")
payments = result.mappings().all()
for payment in payments:
tag = None
created_at = payment.get("time")
if payment.get("extra"):
extra = json.loads(payment.get("extra"))
tag = extra.get("tag")
tsph = db.timestamp_placeholder("created_at")
await db.execute(
f"""
UPDATE apipayments
SET tag = :tag, created_at = {tsph}, updated_at = {tsph}
WHERE checking_id = :checking_id
""",
{
"tag": tag,
"created_at": created_at,
"checking_id": payment.get("checking_id"),
},
)