diff --git a/lnbits/extensions/lnurlp/crud.py b/lnbits/extensions/lnurlp/crud.py index 86300419a..d02ae80eb 100644 --- a/lnbits/extensions/lnurlp/crud.py +++ b/lnbits/extensions/lnurlp/crud.py @@ -1,18 +1,19 @@ from typing import List, Optional, Union -from lnbits.helpers import urlsafe_short_hash +from lnbits.db import SQLITE from . import db from .models import CreatePayLinkData, PayLink async def create_pay_link(data: CreatePayLinkData, wallet_id: str) -> PayLink: - link_id = urlsafe_short_hash() - result = await db.execute( + returning = "" if db.type == SQLITE else "RETURNING ID" + method = db.execute if db.type == SQLITE else db.fetchone + + result = await (method)( f""" INSERT INTO lnurlp.pay_links ( - id, wallet, description, min, @@ -28,11 +29,10 @@ async def create_pay_link(data: CreatePayLinkData, wallet_id: str) -> PayLink: currency, fiat_base_multiplier ) - VALUES (?, ?, ?, ?, ?, 0, 0, ?, ?, ?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, 0, 0, ?, ?, ?, ?, ?, ?, ?, ?) {returning} """, ( - link_id, wallet_id, data.description, data.min, @@ -47,6 +47,10 @@ async def create_pay_link(data: CreatePayLinkData, wallet_id: str) -> PayLink: data.fiat_base_multiplier, ), ) + if db.type == SQLITE: + link_id = result._result_proxy.lastrowid + else: + link_id = result[0] link = await get_pay_link(link_id) assert link, "Newly created link couldn't be retrieved" diff --git a/lnbits/extensions/lnurlp/migrations.py b/lnbits/extensions/lnurlp/migrations.py index 44df5ba9e..c4edd3aa3 100644 --- a/lnbits/extensions/lnurlp/migrations.py +++ b/lnbits/extensions/lnurlp/migrations.py @@ -68,76 +68,3 @@ async def m005_webhook_headers_and_body(db): """ await db.execute("ALTER TABLE lnurlp.pay_links ADD COLUMN webhook_headers TEXT;") await db.execute("ALTER TABLE lnurlp.pay_links ADD COLUMN webhook_body TEXT;") - - -async def m006_redux(db): - """ - Add UUID ID's to links and migrates existing data - """ - await db.execute("ALTER TABLE lnurlp.pay_links RENAME TO pay_links_old") - await db.execute( - f""" - CREATE TABLE lnurlp.pay_links ( - id TEXT PRIMARY KEY, - wallet TEXT NOT NULL, - description TEXT NOT NULL, - min INTEGER NOT NULL, - max INTEGER, - currency TEXT, - fiat_base_multiplier INTEGER DEFAULT 1, - served_meta INTEGER NOT NULL, - served_pr INTEGER NOT NULL, - webhook_url TEXT, - success_text TEXT, - success_url TEXT, - comment_chars INTEGER DEFAULT 0, - webhook_headers TEXT, - webhook_body TEXT - ); - """ - ) - - for row in [ - list(row) for row in await db.fetchall("SELECT * FROM lnurlp.pay_links_old") - ]: - await db.execute( - """ - INSERT INTO lnurlp.pay_links ( - id, - wallet, - description, - min, - served_meta, - served_pr, - webhook_url, - success_text, - success_url, - currency, - comment_chars, - max, - fiat_base_multiplier, - webhook_headers, - webhook_body - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - """, - ( - row[0], - row[1], - row[2], - row[3], - row[4], - row[5], - row[6], - row[7], - row[8], - row[9], - row[10], - row[11], - row[12], - row[13], - row[14], - ), - ) - - await db.execute("DROP TABLE lnurlp.pay_links_old") diff --git a/lnbits/extensions/lnurlp/models.py b/lnbits/extensions/lnurlp/models.py index 42ea29267..2cb4d0ab0 100644 --- a/lnbits/extensions/lnurlp/models.py +++ b/lnbits/extensions/lnurlp/models.py @@ -26,7 +26,7 @@ class CreatePayLinkData(BaseModel): class PayLink(BaseModel): - id: str + id: int wallet: str description: str min: float