Skip to content

Commit

Permalink
fix: types, postgres errors with cache
Browse files Browse the repository at this point in the history
  • Loading branch information
talvasconcelos authored and dni committed Nov 28, 2024
1 parent 6ac1177 commit 8a189dd
Showing 1 changed file with 17 additions and 40 deletions.
57 changes: 17 additions & 40 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.
"""
Expand All @@ -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 (
Expand All @@ -76,8 +52,8 @@ async def m003_add_id_and_tag(db):
);
"""
)
result = await db.execute("SELECT * FROM splitpayments.splitpayments_old")
rows = await result.mappings().all()
result = await db.execute("SELECT * FROM splitpayments.splitpayments_m002")
rows = result.mappings().all()
for row in rows:
await db.execute(
"""
Expand All @@ -101,18 +77,19 @@ async def m003_add_id_and_tag(db):
},
)

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} (
Expand Down

0 comments on commit 8a189dd

Please sign in to comment.