From 2d580a0d0ed8e5f1d18d29b92983c788f6357ace Mon Sep 17 00:00:00 2001 From: arcbtc Date: Thu, 13 Aug 2020 21:49:50 +0100 Subject: [PATCH] Fixed lntickets migration --- lnbits/extensions/lnticket/crud.py | 12 ++--- lnbits/extensions/lnticket/migrations.py | 62 +++++++++++++++++++++++- 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/lnbits/extensions/lnticket/crud.py b/lnbits/extensions/lnticket/crud.py index b389bce..28b2abb 100644 --- a/lnbits/extensions/lnticket/crud.py +++ b/lnbits/extensions/lnticket/crud.py @@ -14,7 +14,7 @@ def create_ticket(checking_id: str, wallet: str, form: str, name: str, email: with open_ext_db("lnticket") as db: db.execute( """ - INSERT INTO tickets (id, paid, form, email, ltext, name, wallet, sats) + INSERT INTO ticket (id, paid, form, email, ltext, name, wallet, sats) VALUES (?, ?, ?, ?, ?, ?, ?, ?) """, (checking_id, False, form, email, ltext, name, wallet, sats), @@ -24,12 +24,12 @@ def create_ticket(checking_id: str, wallet: str, form: str, name: str, email: def update_ticket(paid: bool, checking_id: str) -> Tickets: with open_ext_db("lnticket") as db: - row = db.fetchone("SELECT * FROM tickets WHERE id = ?", (checking_id,)) + row = db.fetchone("SELECT * FROM ticket WHERE id = ?", (checking_id,)) if row[1] == True: return get_ticket(checking_id) db.execute( """ - UPDATE tickets + UPDATE ticket SET paid = ? WHERE id = ? """, @@ -50,7 +50,7 @@ def update_ticket(paid: bool, checking_id: str) -> Tickets: def get_ticket(ticket_id: str) -> Optional[Tickets]: with open_ext_db("lnticket") as db: - row = db.fetchone("SELECT * FROM tickets WHERE id = ?", (ticket_id,)) + row = db.fetchone("SELECT * FROM ticket WHERE id = ?", (ticket_id,)) return Tickets(**row) if row else None @@ -61,14 +61,14 @@ def get_tickets(wallet_ids: Union[str, List[str]]) -> List[Tickets]: with open_ext_db("lnticket") as db: q = ",".join(["?"] * len(wallet_ids)) - rows = db.fetchall(f"SELECT * FROM tickets WHERE wallet IN ({q})", (*wallet_ids,)) + rows = db.fetchall(f"SELECT * FROM ticket WHERE wallet IN ({q})", (*wallet_ids,)) return [Tickets(**row) for row in rows] def delete_ticket(ticket_id: str) -> None: with open_ext_db("lnticket") as db: - db.execute("DELETE FROM tickets WHERE id = ?", (ticket_id,)) + db.execute("DELETE FROM ticket WHERE id = ?", (ticket_id,)) diff --git a/lnbits/extensions/lnticket/migrations.py b/lnbits/extensions/lnticket/migrations.py index 471f78c..6434f13 100644 --- a/lnbits/extensions/lnticket/migrations.py +++ b/lnbits/extensions/lnticket/migrations.py @@ -32,6 +32,66 @@ def m001_initial(db): """ ) + +def m002_changed(db): + + db.execute( + """ + CREATE TABLE IF NOT EXISTS ticket ( + id TEXT PRIMARY KEY, + paid BOOLEAN NOT NULL, + form TEXT NOT NULL, + email TEXT NOT NULL, + ltext TEXT NOT NULL, + name TEXT NOT NULL, + wallet TEXT NOT NULL, + sats INTEGER NOT NULL, + time TIMESTAMP NOT NULL DEFAULT (strftime('%s', 'now')) + ); + """ + ) + + for row in [list(row) for row in db.fetchall("SELECT * FROM tickets")]: + usescsv = "" + + for i in range(row[5]): + if row[7]: + usescsv += "," + str(i + 1) + else: + usescsv += "," + str(1) + usescsv = usescsv[1:] + db.execute( + """ + INSERT INTO ticket ( + id, + paid, + title, + form, + email, + ltext, + name, + wallet, + sats + ) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + """, + ( + row[0], + True, + row[1], + row[2], + row[3], + row[4], + row[5], + row[6], + row[7], + row[8], + ), + ) + db.execute("DROP TABLE tickets") + + def migrate(): with open_ext_db("lnticket") as db: - m001_initial(db) \ No newline at end of file + m001_initial(db) + m002_changed(db)