From ce1639792a2659ca9d1f53a15cb6940c5f95a5f0 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Wed, 18 Jan 2023 18:35:02 +0200 Subject: [PATCH] fix: order of migrations --- lnbits/app.py | 15 +++++++++------ lnbits/commands.py | 6 ++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lnbits/app.py b/lnbits/app.py index 0dbca2a67..9c20899ff 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -21,10 +21,11 @@ from fastapi.staticfiles import StaticFiles from loguru import logger from lnbits.core.crud import get_installed_extensions +from lnbits.core.helpers import migrate_extension_database from lnbits.core.tasks import register_task_listeners from lnbits.settings import get_wallet_class, set_wallet_class, settings -from .commands import migrate_databases +from .commands import db_versions, migrate_databases from .core import core_app, core_app_extra from .core.services import check_admin_settings from .core.views.generic import core_html_routes @@ -132,9 +133,11 @@ async def check_installed_extensions(app: FastAPI): for ext in installed_extensions: try: - is_installed = check_installed_extension(ext) - if not is_installed: + installed = check_installed_extension(ext) + if not installed: register_ext_routes(app, Extension(ext.id, True, False)) + current_version = (await db_versions()).get(ext.id, 0) + await migrate_extension_database(ext, current_version) except: logger.warning(f"Failed to re-install extension: {ext.id}") @@ -204,9 +207,6 @@ def register_startup(app: FastAPI): async def lnbits_startup(): try: - # check extensions after restart - await check_installed_extensions(app) - # wait till migration is done await migrate_databases() @@ -221,6 +221,9 @@ def register_startup(app: FastAPI): # initialize funding source await check_funding_source() + # check extensions after restart + await check_installed_extensions(app) + except Exception as e: logger.error(str(e)) raise ImportError("Failed to run 'startup' event.") diff --git a/lnbits/commands.py b/lnbits/commands.py index 0d7fd2c5c..a5634d29f 100644 --- a/lnbits/commands.py +++ b/lnbits/commands.py @@ -81,6 +81,12 @@ async def migrate_databases(): logger.info("✔️ All migrations done.") +async def db_versions(): + async with core_db.connect() as conn: + current_versions = await get_dbversions(conn) + return current_versions + + async def load_disabled_extension_list() -> None: """Update list of extensions that have been explicitly disabled""" inactive_extensions = await get_inactive_extensions()