fix insertion bugs/incompatibilities with lastrowid stuff.

This commit is contained in:
fiatjaf 2021-08-11 22:19:27 -03:00
parent b7ed7fab59
commit 26b3599325
7 changed files with 64 additions and 25 deletions

View file

@ -1,20 +1,28 @@
from typing import List, Optional from typing import List, Optional
from lnbits.core.crud import create_account, create_wallet from lnbits.core.crud import create_account, create_wallet
from lnbits.db import SQLITE
from . import db from . import db
from .models import Livestream, Track, Producer from .models import Livestream, Track, Producer
async def create_livestream(*, wallet_id: str) -> int: async def create_livestream(*, wallet_id: str) -> int:
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 livestream.livestreams (wallet) INSERT INTO livestream.livestreams (wallet)
VALUES (?) VALUES (?)
{returning}
""", """,
(wallet_id,), (wallet_id,),
) )
return result._result_proxy.lastrowid
if db.type == SQLITE:
return result._result_proxy.lastrowid
else:
return result[0]
async def get_livestream(ls_id: int) -> Optional[Livestream]: async def get_livestream(ls_id: int) -> Optional[Livestream]:

View file

@ -1,5 +1,6 @@
from typing import List, Optional, Union from typing import List, Optional, Union
from lnbits.db import SQLITE
from . import db from . import db
from .models import PayLink from .models import PayLink
@ -17,12 +18,10 @@ async def create_pay_link(
success_url: Optional[str] = None, success_url: Optional[str] = None,
) -> PayLink: ) -> PayLink:
if db.type == "POSTGRES" or db.type == "COCKROACH": returning = "" if db.type == SQLITE else "RETURNING ID"
returning = "RETURNING id" method = db.execute if db.type == SQLITE else db.fetchone
else:
returning = ""
result = await db.fetchone( result = await (method)(
f""" f"""
INSERT INTO lnurlp.pay_links ( INSERT INTO lnurlp.pay_links (
wallet, wallet,
@ -52,11 +51,11 @@ async def create_pay_link(
currency, currency,
), ),
) )
if db.type == "POSTGRES" or db.type == "COCKROACH": if db.type == SQLITE:
link_id = result[0]
else:
link_id = result._result_proxy.lastrowid link_id = result._result_proxy.lastrowid
else:
link_id = result[0]
link = await get_pay_link(link_id) link = await get_pay_link(link_id)
assert link, "Newly created link couldn't be retrieved" assert link, "Newly created link couldn't be retrieved"
return link return link

View file

@ -3,7 +3,7 @@ from urllib.parse import urlparse, urlunparse, parse_qs, urlencode, ParseResult
from quart import url_for from quart import url_for
from typing import NamedTuple, Optional, Dict from typing import NamedTuple, Optional, Dict
from sqlite3 import Row from sqlite3 import Row
from lnurl import Lnurl, encode as lnurl_encode # type: ignore from lnbits.lnurl import encode as lnurl_encode # type: ignore
from lnurl.types import LnurlPayMetadata # type: ignore from lnurl.types import LnurlPayMetadata # type: ignore
@ -27,7 +27,7 @@ class PayLink(NamedTuple):
return cls(**data) return cls(**data)
@property @property
def lnurl(self) -> Lnurl: def lnurl(self) -> str:
url = url_for("lnurlp.api_lnurl_response", link_id=self.id, _external=True) url = url_for("lnurlp.api_lnurl_response", link_id=self.id, _external=True)
return lnurl_encode(url) return lnurl_encode(url)

View file

@ -1,19 +1,27 @@
from typing import List, Optional from typing import List, Optional
from lnbits.db import SQLITE
from . import db from . import db
from .wordlists import animals from .wordlists import animals
from .models import Shop, Item from .models import Shop, Item
async def create_shop(*, wallet_id: str) -> int: async def create_shop(*, wallet_id: str) -> int:
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 offlineshop.shops (wallet, wordlist, method) INSERT INTO offlineshop.shops (wallet, wordlist, method)
VALUES (?, ?, 'wordlist') VALUES (?, ?, 'wordlist')
{returning}
""", """,
(wallet_id, "\n".join(animals)), (wallet_id, "\n".join(animals)),
) )
return result._result_proxy.lastrowid if db.type == SQLITE:
return result._result_proxy.lastrowid
else:
return result[0]
async def get_shop(id: int) -> Optional[Shop]: async def get_shop(id: int) -> Optional[Shop]:

View file

@ -1,7 +1,7 @@
from . import db from . import db
from .models import Donation, Service from .models import Donation, Service
from ..satspay.crud import delete_charge from ..satspay.crud import delete_charge # type: ignore
import httpx import httpx
@ -10,6 +10,7 @@ from quart import jsonify
from typing import Optional from typing import Optional
from lnbits.db import SQLITE
from lnbits.helpers import urlsafe_short_hash from lnbits.helpers import urlsafe_short_hash
from lnbits.core.crud import get_wallet from lnbits.core.crud import get_wallet
@ -69,7 +70,10 @@ async def create_donation(
""", """,
(id, wallet, name, message, cur_code, sats, amount, service, posted), (id, wallet, name, message, cur_code, sats, amount, service, posted),
) )
return await get_donation(id)
donation = await get_donation(id)
assert donation, "Newly created donation couldn't be retrieved"
return donation
async def post_donation(donation_id: str) -> tuple: async def post_donation(donation_id: str) -> tuple:
@ -86,6 +90,8 @@ async def post_donation(donation_id: str) -> tuple:
HTTPStatus.BAD_REQUEST, HTTPStatus.BAD_REQUEST,
) )
service = await get_service(donation.service) service = await get_service(donation.service)
assert service, "Couldn't fetch service to donate to"
if service.servicename == "Streamlabs": if service.servicename == "Streamlabs":
url = "https://streamlabs.com/api/v1.0/donations" url = "https://streamlabs.com/api/v1.0/donations"
data = { data = {
@ -123,8 +129,12 @@ async def create_service(
onchain: str = None, onchain: str = None,
) -> Service: ) -> Service:
"""Create a new Service""" """Create a new Service"""
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 streamalerts.Services ( INSERT INTO streamalerts.Services (
twitchuser, twitchuser,
client_id, client_id,
@ -136,6 +146,7 @@ async def create_service(
onchain onchain
) )
VALUES (?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
{returning}
""", """,
( (
twitchuser, twitchuser,
@ -148,8 +159,13 @@ async def create_service(
onchain, onchain,
), ),
) )
service_id = result._result_proxy.lastrowid if db.type == SQLITE:
service_id = result._result_proxy.lastrowid
else:
service_id = result[0]
service = await get_service(service_id) service = await get_service(service_id)
assert service
return service return service

View file

@ -93,7 +93,6 @@ async def create_watch_wallet(*, user: str, masterpub: str, title: str) -> Walle
# address_no is -1 so fresh address on empty wallet can get address with index 0 # address_no is -1 so fresh address on empty wallet can get address with index 0
(wallet_id, user, masterpub, title, -1, 0), (wallet_id, user, masterpub, title, -1, 0),
) )
# weallet_id = db.cursor.lastrowid
return await get_watch_wallet(wallet_id) return await get_watch_wallet(wallet_id)

View file

@ -1,7 +1,16 @@
from bech32 import bech32_decode, convertbits from bech32 import bech32_decode, bech32_encode, convertbits
def decode(lnurl: str) -> str: def decode(lnurl: str) -> str:
hrp, data = bech32_decode(lnurl) hrp, data = bech32_decode(lnurl)
assert data
bech32_data = convertbits(data, 5, 8, False) bech32_data = convertbits(data, 5, 8, False)
assert bech32_data
return bytes(bech32_data).decode("utf-8") return bytes(bech32_data).decode("utf-8")
def encode(url: str) -> str:
bech32_data = convertbits(url.encode("utf-8"), 8, 5, True)
assert bech32_data
lnurl = bech32_encode("lnurl", bech32_data)
return lnurl.upper()