migration tracking.

This commit is contained in:
fiatjaf
2020-09-04 21:33:30 -03:00
committed by fiatjaf
parent f238b3d1ef
commit c965bca41d
17 changed files with 86 additions and 130 deletions
+34 -12
View File
@@ -1,4 +1,6 @@
import re
import importlib
import sqlite3
from flask import Flask
from flask_assets import Environment, Bundle # type: ignore
@@ -8,9 +10,10 @@ from flask_talisman import Talisman # type: ignore
from os import getenv
from werkzeug.middleware.proxy_fix import ProxyFix
from .core import core_app, migrations as core_migrations
from .core import core_app
from .helpers import ExtensionManager
from .settings import FORCE_HTTPS
from .db import open_db, open_ext_db
disabled_extensions = getenv("LNBITS_DISABLED_EXTENSIONS", "").split(",")
@@ -73,21 +76,40 @@ assets.register("base_css", Bundle("scss/base.scss", filters="pyscss", output="c
# --------
@app.cli.command("migrate")
def migrate_databases():
"""Creates the necessary databases if they don't exist already; or migrates them."""
core_migrations.migrate()
for ext in valid_extensions:
from .core import migrations as core_migrations
with open_db() as core_db:
try:
ext_migrations = importlib.import_module(f"lnbits.extensions.{ext.code}.migrations")
ext_migrations.migrate()
except Exception:
raise ImportError(f"Please make sure that the extension `{ext.code}` has a migrations file.")
rows = core_db.fetchall("SELECT * FROM dbversions")
except sqlite3.OperationalError:
# migration 3 wasn't ran
core_migrations.m000_create_migrations_table(core_db)
rows = core_db.fetchall("SELECT * FROM dbversions")
current_versions = {row["db"]: row["version"] for row in rows}
matcher = re.compile(r"^m(\d\d\d)_")
# init
# ----
def run_migration(db, migrations_module):
db_name = migrations_module.__name__.split(".")[-2]
for key, run_migration in migrations_module.__dict__.items():
if match := matcher.match(key):
version = int(match.group(1))
if version > current_versions.get(db_name, 0):
print(f"running migration {db_name}.{version}")
run_migration(db)
core_db.execute(
"INSERT OR REPLACE INTO dbversions (db, version) VALUES (?, ?)", (db_name, version)
)
if __name__ == "__main__":
app.run()
run_migration(core_db, core_migrations)
for ext in valid_extensions:
try:
ext_migrations = importlib.import_module(f"lnbits.extensions.{ext.code}.migrations")
with open_ext_db(ext.code) as db:
run_migration(db, ext_migrations)
except ImportError:
raise ImportError(f"Please make sure that the extension `{ext.code}` has a migrations file.")
+42 -30
View File
@@ -1,4 +1,15 @@
from lnbits.db import open_db
import sqlite3
def m000_create_migrations_table(db):
db.execute(
"""
CREATE TABLE dbversions (
db TEXT PRIMARY KEY,
version INT NOT NULL
)
"""
)
def m001_initial(db):
@@ -76,35 +87,36 @@ def m002_add_fields_to_apipayments(db):
Adding fields to apipayments for better accounting,
and renaming payhash to checking_id since that is what it really is.
"""
db.execute("ALTER TABLE apipayments RENAME COLUMN payhash TO checking_id")
db.execute("ALTER TABLE apipayments ADD COLUMN hash TEXT")
db.execute("CREATE INDEX by_hash ON apipayments (hash)")
db.execute("ALTER TABLE apipayments ADD COLUMN preimage TEXT")
db.execute("ALTER TABLE apipayments ADD COLUMN bolt11 TEXT")
db.execute("ALTER TABLE apipayments ADD COLUMN extra TEXT")
try:
db.execute("ALTER TABLE apipayments RENAME COLUMN payhash TO checking_id")
db.execute("ALTER TABLE apipayments ADD COLUMN hash TEXT")
db.execute("CREATE INDEX by_hash ON apipayments (hash)")
db.execute("ALTER TABLE apipayments ADD COLUMN preimage TEXT")
db.execute("ALTER TABLE apipayments ADD COLUMN bolt11 TEXT")
db.execute("ALTER TABLE apipayments ADD COLUMN extra TEXT")
import json
import json
rows = db.fetchall("SELECT * FROM apipayments")
for row in rows:
if not row["memo"] or not row["memo"].startswith("#"):
continue
rows = db.fetchall("SELECT * FROM apipayments")
for row in rows:
if not row["memo"] or not row["memo"].startswith("#"):
continue
for ext in ["withdraw", "events", "lnticket", "paywall", "tpos"]:
prefix = "#" + ext + " "
if row["memo"].startswith(prefix):
new = row["memo"][len(prefix) :]
db.execute(
"""
UPDATE apipayments SET extra = ?, memo = ?
WHERE checking_id = ? AND memo = ?
""",
(json.dumps({"tag": ext}), new, row["checking_id"], row["memo"]),
)
break
def migrate():
with open_db() as db:
m001_initial(db)
m002_add_fields_to_apipayments(db)
for ext in ["withdraw", "events", "lnticket", "paywall", "tpos"]:
prefix = "#" + ext + " "
if row["memo"].startswith(prefix):
new = row["memo"][len(prefix) :]
db.execute(
"""
UPDATE apipayments SET extra = ?, memo = ?
WHERE checking_id = ? AND memo = ?
""",
(json.dumps({"tag": ext}), new, row["checking_id"], row["memo"]),
)
break
except sqlite3.OperationalError:
# this is necessary now because it may be the case that this migration will
# run twice in some environments.
# catching errors like this won't be necessary in anymore now that we
# keep track of db versions so no migration ever runs twice.
pass
-8
View File
@@ -1,6 +1,3 @@
from lnbits.db import open_ext_db
def m001_initial(db):
"""
Initial amilks table.
@@ -16,8 +13,3 @@ def m001_initial(db):
);
"""
)
def migrate():
with open_ext_db("amilk") as db:
m001_initial(db)
@@ -1,6 +1,3 @@
from lnbits.db import open_ext_db
def m001_initial(db):
"""
Initial products table.
@@ -61,8 +58,3 @@ def m001_initial(db):
);
"""
)
def migrate():
with open_ext_db("diagonalley") as db:
m001_initial(db)
-9
View File
@@ -1,6 +1,3 @@
from lnbits.db import open_ext_db
def m001_initial(db):
db.execute(
@@ -86,9 +83,3 @@ def m002_changed(db):
),
)
db.execute("DROP TABLE tickets")
def migrate():
with open_ext_db("events") as db:
m001_initial(db)
m002_changed(db)
-5
View File
@@ -1,5 +0,0 @@
from lnbits.db import open_ext_db
def migrate():
print("pending")
-9
View File
@@ -1,6 +1,3 @@
from lnbits.db import open_ext_db
def m001_initial(db):
db.execute(
@@ -86,9 +83,3 @@ def m002_changed(db):
),
)
db.execute("DROP TABLE tickets")
def migrate():
with open_ext_db("lnticket") as db:
m001_initial(db)
m002_changed(db)
-8
View File
@@ -1,6 +1,3 @@
from lnbits.db import open_ext_db
def m001_initial(db):
"""
Initial pay table.
@@ -17,8 +14,3 @@ def m001_initial(db):
);
"""
)
def migrate():
with open_ext_db("lnurlp") as db:
m001_initial(db)
-8
View File
@@ -1,7 +1,5 @@
from sqlite3 import OperationalError
from lnbits.db import open_ext_db
def m001_initial(db):
"""
@@ -65,9 +63,3 @@ def m002_redux(db):
)
db.execute("DROP TABLE paywalls_old")
def migrate():
with open_ext_db("paywall") as db:
m001_initial(db)
m002_redux(db)
-8
View File
@@ -1,6 +1,3 @@
from lnbits.db import open_ext_db
def m001_initial(db):
"""
Initial tposs table.
@@ -15,8 +12,3 @@ def m001_initial(db):
);
"""
)
def migrate():
with open_ext_db("tpos") as db:
m001_initial(db)
@@ -1,6 +1,3 @@
from lnbits.db import open_ext_db
def m001_initial(db):
"""
Initial users table.
@@ -32,8 +29,3 @@ def m001_initial(db):
);
"""
)
def migrate():
with open_ext_db("usermanager") as db:
m001_initial(db)
-9
View File
@@ -1,6 +1,3 @@
from lnbits.db import open_ext_db
def m001_initial(db):
"""
Creates an improved withdraw table and migrates the existing data.
@@ -97,9 +94,3 @@ def m002_change_withdraw_table(db):
),
)
db.execute("DROP TABLE withdraw_links")
def migrate():
with open_ext_db("withdraw") as db:
m001_initial(db)
m002_change_withdraw_table(db)