mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-10 20:42:32 +02:00
Merge pull request #1344 from lnbits/migrations/conv_warn_empty
conv.py: warn if table is empty
This commit is contained in:
@@ -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 argparse
|
||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
@@ -8,13 +13,6 @@ import psycopg2
|
|||||||
|
|
||||||
from lnbits.settings import settings
|
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
|
sqfolder = settings.lnbits_data_folder
|
||||||
db_url = settings.lnbits_database_url
|
db_url = settings.lnbits_database_url
|
||||||
|
|
||||||
@@ -31,7 +29,7 @@ else:
|
|||||||
pgschema = ""
|
pgschema = ""
|
||||||
|
|
||||||
|
|
||||||
def get_sqlite_cursor(sqdb) -> sqlite3:
|
def get_sqlite_cursor(sqdb):
|
||||||
consq = sqlite3.connect(sqdb)
|
consq = sqlite3.connect(sqdb)
|
||||||
return consq.cursor()
|
return consq.cursor()
|
||||||
|
|
||||||
@@ -112,12 +110,15 @@ def migrate_core(file: str, exclude_tables: List[str] = []):
|
|||||||
def migrate_ext(file: str):
|
def migrate_ext(file: str):
|
||||||
filename = os.path.basename(file)
|
filename = os.path.basename(file)
|
||||||
schema = filename.replace("ext_", "").split(".")[0]
|
schema = filename.replace("ext_", "").split(".")[0]
|
||||||
print(f"Migrating ext: {file}.{schema}")
|
print(f"Migrating ext: {schema} from file {file}")
|
||||||
migrate_db(file, schema)
|
migrate_db(file, schema)
|
||||||
print(f"✅ Migrated ext: {schema}")
|
print(f"✅ Migrated ext: {schema}")
|
||||||
|
|
||||||
|
|
||||||
def migrate_db(file: str, schema: str, exclude_tables: List[str] = []):
|
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)
|
sq = get_sqlite_cursor(file)
|
||||||
tables = sq.execute(
|
tables = sq.execute(
|
||||||
"""
|
"""
|
||||||
@@ -139,6 +140,10 @@ def migrate_db(file: str, schema: str, exclude_tables: List[str] = []):
|
|||||||
q = build_insert_query(schema, tableName, columns)
|
q = build_insert_query(schema, tableName, columns)
|
||||||
|
|
||||||
data = sq.execute(f"SELECT * FROM {tableName};").fetchall()
|
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)
|
insert_to_pg(q, data)
|
||||||
sq.close()
|
sq.close()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user