From b750aa140fdc6fdd0ef9494fcc185251ee7e6fd9 Mon Sep 17 00:00:00 2001 From: alemidev Date: Thu, 25 Aug 2022 20:03:33 +0200 Subject: [PATCH] fix: i actually sorta finished it and added a migration script --- ...e-old-documents-to-namespaced-documents.py | 21 +++++++++++++++++++ src/treepuncher/storage.py | 3 ++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 scripts/migrate-old-documents-to-namespaced-documents.py diff --git a/scripts/migrate-old-documents-to-namespaced-documents.py b/scripts/migrate-old-documents-to-namespaced-documents.py new file mode 100644 index 0000000..0cde00e --- /dev/null +++ b/scripts/migrate-old-documents-to-namespaced-documents.py @@ -0,0 +1,21 @@ +import sqlite3 + +def migrate_old_documents_to_namespaced_documents(db:str): + db = sqlite3.connect(db) + + values = db.cursor().execute("SELECT * FROM documents", ()).fetchall(); + + for k,v in values: + if "_" in k: + addon, key = k.split("_", 1) + db.cursor().execute("CREATE TABLE IF NOT EXISTS documents_{addon} (name TEXT PRIMARY KEY, value TEXT)", ()) + db.cursor().execute("INSERT INTO documents_{addon} VALUES (?, ?)", (key, v)) + db.cursor().execute("DELETE FROM documents WHERE name = ?", k) + +if __name__ == "__main__": + import sys + if len(sys.argv) < 2: + print("[!] No argument given") + exit(-1) + migrate_old_documents_to_namespaced_documents(sys.argv[1]) + diff --git a/src/treepuncher/storage.py b/src/treepuncher/storage.py index 855fe53..12c1200 100644 --- a/src/treepuncher/storage.py +++ b/src/treepuncher/storage.py @@ -21,13 +21,14 @@ class AuthenticatorState: legacy : bool = False class AddonStorage: + # TODO this uses py formatting in SQL queries, can we avoid it? db: sqlite3.Connection name: str def __init__(self, db:sqlite3.Connection, name:str): self.db = db self.name = name - self.db.cursor().execute('CREATE TABLE IF NOT EXISTS documents (name TEXT PRIMARY KEY, value TEXT)') + self.db.cursor().execute(f'CREATE TABLE IF NOT EXISTS documents_{self.name} (name TEXT PRIMARY KEY, value TEXT)') self.db.commit() # fstrings in queries are evil but if you go to this length to fuck up you kinda deserve it :)