diff --git a/conv.py b/conv.py index bf408cfbe..159c7dc0e 100644 --- a/conv.py +++ b/conv.py @@ -1,12 +1,12 @@ -## 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 os -import sqlite3 - import psycopg2 +import sqlite3 +import os +# 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 @@ -34,7 +34,8 @@ def get_postgres_cursor(): def check_db_versions(sqdb): sqlite = get_sqlite_cursor(sqdb) dblite = dict(sqlite.execute("SELECT * FROM dbversions;").fetchall()) - del dblite["lnurlpos"] # wrongly added? + if "lnurlpos" in dblite: + del dblite["lnurlpos"] sqlite.close() postgres = get_postgres_cursor() @@ -42,7 +43,7 @@ def check_db_versions(sqdb): dbpost = dict(postgres.fetchall()) for key in dblite.keys(): - if dblite[key] != dbpost[key]: + if key in dblite and key in dbpost and dblite[key] != dbpost[key]: raise Exception( f"sqlite database version ({dblite[key]}) of {key} doesn't match postgres database version {dbpost[key]}" ) @@ -76,7 +77,10 @@ def insert_to_pg(query, data): connection = cursor.connection for d in data: - cursor.execute(query, d) + try: + cursor.execute(query, d) + except: + raise ValueError(f"Failed to insert {d}") connection.commit() cursor.close() diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index f69ca95b1..0214ac091 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -30,7 +30,8 @@ async def get_account( user_id: str, conn: Optional[Connection] = None ) -> Optional[User]: row = await (conn or db).fetchone( - "SELECT id, email, pass as password FROM accounts WHERE id = ?", (user_id,) + "SELECT id, email, pass as password FROM accounts WHERE id = ?", ( + user_id,) ) return User(**row) if row else None @@ -184,7 +185,7 @@ async def get_standalone_payment( """ SELECT * FROM apipayments - WHERE checking_id = ? OR hash = ? + WHERE (checking_id = ? OR hash = ?) AND amount > 0 -- only the incoming payment LIMIT 1 """, (checking_id_or_hash, checking_id_or_hash), @@ -304,7 +305,8 @@ async def delete_expired_invoices(conn: Optional[Connection] = None,) -> None: except: continue - expiration_date = datetime.datetime.fromtimestamp(invoice.date + invoice.expiry) + expiration_date = datetime.datetime.fromtimestamp( + invoice.date + invoice.expiry) if expiration_date > datetime.datetime.utcnow(): continue