From 64c330459b128e451d2c621325f1b2299af918d7 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 10 Jan 2023 16:16:17 +0100 Subject: [PATCH 1/3] conv.py: warn if table is empty --- tools/conv.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tools/conv.py b/tools/conv.py index 2ba723f25..a1c0699d0 100644 --- a/tools/conv.py +++ b/tools/conv.py @@ -1,3 +1,8 @@ +# Python script to migrate an LNbits SQLite DB to Postgres +# All credits to @Fritz446 for the awesome work + +# pip install psycopg2 OR psycopg2-binary + import argparse import os import sqlite3 @@ -8,13 +13,6 @@ import psycopg2 from lnbits.settings import settings -# Python script to migrate an LNbits SQLite DB to Postgres -# All credits to @Fritz446 for the awesome work - -# pip install psycopg2 OR psycopg2-binary - -# Change these values as needed - sqfolder = settings.lnbits_data_folder db_url = settings.lnbits_database_url @@ -31,7 +29,7 @@ else: pgschema = "" -def get_sqlite_cursor(sqdb) -> sqlite3: +def get_sqlite_cursor(sqdb): consq = sqlite3.connect(sqdb) return consq.cursor() @@ -112,12 +110,15 @@ def migrate_core(file: str, exclude_tables: List[str] = []): def migrate_ext(file: str): filename = os.path.basename(file) schema = filename.replace("ext_", "").split(".")[0] - print(f"Migrating ext: {file}.{schema}") + print(f"Migrating ext: {schema} from file {file}") migrate_db(file, schema) print(f"✅ Migrated ext: {schema}") def migrate_db(file: str, schema: str, exclude_tables: List[str] = []): + # first we check if this file exists: + assert os.path.isfile(file), f"{file} does not exist!" + sq = get_sqlite_cursor(file) tables = sq.execute( """ @@ -126,6 +127,9 @@ def migrate_db(file: str, schema: str, exclude_tables: List[str] = []): """ ).fetchall() + if len(tables) == 0: + print(f"⚠️ You sneaky dev! Schema {schema} is empty!") + for table in tables: tableName = table[0] print(f"Migrating table {tableName}") From 3f61286cadf95a7b3a0a8bc9c457d330039661e5 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 10 Jan 2023 16:19:56 +0100 Subject: [PATCH 2/3] correct placement of warning --- tools/conv.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/conv.py b/tools/conv.py index a1c0699d0..58cade5e5 100644 --- a/tools/conv.py +++ b/tools/conv.py @@ -127,9 +127,6 @@ def migrate_db(file: str, schema: str, exclude_tables: List[str] = []): """ ).fetchall() - if len(tables) == 0: - print(f"⚠️ You sneaky dev! Schema {schema} is empty!") - for table in tables: tableName = table[0] print(f"Migrating table {tableName}") @@ -143,6 +140,10 @@ def migrate_db(file: str, schema: str, exclude_tables: List[str] = []): q = build_insert_query(schema, tableName, columns) data = sq.execute(f"SELECT * FROM {tableName};").fetchall() + + if len(data) == 0: + print(f"⚠️ You sneaky dev! Table {tableName} is empty!") + insert_to_pg(q, data) sq.close() From a1a75ddf0bec55013b7d29f46b19d6bcb44379d0 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Tue, 10 Jan 2023 16:25:28 +0100 Subject: [PATCH 3/3] change sign --- tools/conv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/conv.py b/tools/conv.py index 58cade5e5..f01295fc7 100644 --- a/tools/conv.py +++ b/tools/conv.py @@ -142,7 +142,7 @@ def migrate_db(file: str, schema: str, exclude_tables: List[str] = []): data = sq.execute(f"SELECT * FROM {tableName};").fetchall() if len(data) == 0: - print(f"⚠️ You sneaky dev! Table {tableName} is empty!") + print(f"🛑 You sneaky dev! Table {tableName} is empty!") insert_to_pg(q, data) sq.close()