Skip to content

Commit

Permalink
feat: update to lnbits 1.0.0 (#27)
Browse files Browse the repository at this point in the history
* feat: update to lnbits 1.0.0
* fix select wallet
* fix splits
* fix: types, postgres errors with cache
---------

Co-authored-by: Tiago Vasconcelos <[email protected]>
dni and talvasconcelos authored Nov 28, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 32bf4ae commit 5042d40
Showing 11 changed files with 1,300 additions and 1,218 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
"name": "Split Payments",
"short_description": "Split incoming payments across wallets",
"tile": "/splitpayments/static/image/split-payments.png",
"min_lnbits_version": "0.11.0",
"min_lnbits_version": "1.0.0",
"contributors": [
{
"name": "cryptograffiti",
32 changes: 9 additions & 23 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
from typing import List

from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash

from .models import Target

db = Database("ext_splitpayments")


async def get_targets(source_wallet: str) -> List[Target]:
rows = await db.fetchall(
"SELECT * FROM splitpayments.targets WHERE source = ?", (source_wallet,)
async def get_targets(source_wallet: str) -> list[Target]:
return await db.fetchall(
"SELECT * FROM splitpayments.targets WHERE source = :source_wallet",
{"source_wallet": source_wallet},
Target,
)
return [Target(**row) for row in rows]


async def set_targets(source_wallet: str, targets: List[Target]):
async def set_targets(source_wallet: str, targets: list[Target]):
async with db.connect() as conn:
await conn.execute(
"DELETE FROM splitpayments.targets WHERE source = ?", (source_wallet,)
"DELETE FROM splitpayments.targets WHERE source = :source_wallet",
{"source_wallet": source_wallet},
)
for target in targets:
await conn.execute(
"""
INSERT INTO splitpayments.targets
(id, source, wallet, percent, alias)
VALUES (?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
source_wallet,
target.wallet,
target.percent,
target.alias,
),
)
await conn.insert("splitpayments.targets", target)
72 changes: 27 additions & 45 deletions migrations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from lnbits.db import Connection
from lnbits.helpers import urlsafe_short_hash


async def m001_initial(db):
async def m001_initial(db: Connection):
"""
Initial split payment table.
"""
@@ -19,49 +20,24 @@ async def m001_initial(db):
)


async def m002_float_percent(db):
async def m002_float_percent(db: Connection):
"""
Add float percent and migrates the existing data.
"""
await db.execute("ALTER TABLE splitpayments.targets RENAME TO splitpayments_old")
await db.execute(
"""
CREATE TABLE splitpayments.targets (
wallet TEXT NOT NULL,
source TEXT NOT NULL,
percent REAL NOT NULL CHECK (percent >= 0 AND percent <= 100),
alias TEXT,
UNIQUE (source, wallet)
);
"""
ALTER TABLE splitpayments.targets
ADD COLUMN percent REAL NOT NULL CHECK (percent >= 0 AND percent <= 100)
"""
)

for row in [
list(row)
for row in await db.fetchall("SELECT * FROM splitpayments.splitpayments_old")
]:
await db.execute(
"""
INSERT INTO splitpayments.targets (
wallet,
source,
percent,
alias
)
VALUES (?, ?, ?, ?)
""",
(row[0], row[1], row[2], row[3]),
)

await db.execute("DROP TABLE splitpayments.splitpayments_old")


async def m003_add_id_and_tag(db):
async def m003_add_id_and_tag(db: Connection):
"""
Add float percent and migrates the existing data.
Add id, tag and migrates the existing data.
"""
await db.execute("ALTER TABLE splitpayments.targets RENAME TO splitpayments_old")
await db.execute("ALTER TABLE splitpayments.targets RENAME TO splitpayments_m002")

await db.execute(
"""
CREATE TABLE splitpayments.targets (
@@ -76,11 +52,9 @@ async def m003_add_id_and_tag(db):
);
"""
)

for row in [
list(row)
for row in await db.fetchall("SELECT * FROM splitpayments.splitpayments_old")
]:
result = await db.execute("SELECT * FROM splitpayments.splitpayments_m002")
rows = result.mappings().all()
for row in rows:
await db.execute(
"""
INSERT INTO splitpayments.targets (
@@ -91,23 +65,31 @@ async def m003_add_id_and_tag(db):
tag,
alias
)
VALUES (?, ?, ?, ?, ?, ?)
VALUES (:id, :wallet, :source, :percent, :tag, :alias)
""",
(urlsafe_short_hash(), row[0], row[1], row[2], "", row[3]),
{
"id": urlsafe_short_hash(),
"wallet": row["wallet"],
"source": row["source"],
"percent": row["percent"],
"tag": row["tag"],
"alias": row["alias"],
},
)

await db.execute("DROP TABLE splitpayments.splitpayments_old")
await db.execute("DROP TABLE splitpayments.splitpayments_m002")


async def m004_remove_tag(db):
async def m004_remove_tag(db: Connection):
"""
This removes tag
"""
keys = "id,wallet,source,percent,alias"
new_db = "splitpayments.targets"
old_db = "splitpayments.targets_old"
old_db = "splitpayments.targets_m003"

await db.execute(f"ALTER TABLE {new_db} RENAME TO targets_m003")

await db.execute(f"ALTER TABLE {new_db} RENAME TO targets_old")
await db.execute(
f"""
CREATE TABLE {new_db} (
12 changes: 4 additions & 8 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
from sqlite3 import Row
from typing import List, Optional
from typing import Optional

from fastapi import Query
from pydantic import BaseModel


class Target(BaseModel):
id: str
wallet: str
source: str
percent: float
alias: Optional[str]

@classmethod
def from_row(cls, row: Row):
return cls(**dict(row))
alias: Optional[str] = None


class TargetPut(BaseModel):
@@ -23,4 +19,4 @@ class TargetPut(BaseModel):


class TargetPutList(BaseModel):
targets: List[TargetPut]
targets: list[TargetPut]
Loading

0 comments on commit 5042d40

Please sign in to comment.