diff --git a/migrations.py b/migrations.py index 5af17e5..3befe21 100644 --- a/migrations.py +++ b/migrations.py @@ -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,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( """ @@ -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} (