From 88a4dc24f297b2f1fafed765bf433e5ca028e350 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 25 Aug 2022 18:44:21 +0200 Subject: [PATCH] [db] Fix automatic migration upgrade for plugins and core Signed-off-by: Ferdinand Thiessen --- flaschengeist/cli/install_cmd.py | 3 +-- flaschengeist/controller/pluginController.py | 12 +++++++++--- flaschengeist/database/__init__.py | 8 ++------ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/flaschengeist/cli/install_cmd.py b/flaschengeist/cli/install_cmd.py index e1e9244..367a1bc 100644 --- a/flaschengeist/cli/install_cmd.py +++ b/flaschengeist/cli/install_cmd.py @@ -3,7 +3,6 @@ from click.decorators import pass_context from flask.cli import with_appcontext from flask_migrate import upgrade -from flaschengeist.alembic import alembic_migrations_path from flaschengeist.controller import pluginController from flaschengeist.utils.hook import Hook @@ -16,7 +15,7 @@ def install(ctx: click.Context): plugins = pluginController.get_enabled_plugins() # Install database - upgrade(alembic_migrations_path, revision="heads") + upgrade(revision="flaschengeist@head") # Install plugins for plugin in plugins: diff --git a/flaschengeist/controller/pluginController.py b/flaschengeist/controller/pluginController.py index 7cccc4e..4a84d3c 100644 --- a/flaschengeist/controller/pluginController.py +++ b/flaschengeist/controller/pluginController.py @@ -7,6 +7,7 @@ from typing import Union from flask import current_app from werkzeug.exceptions import NotFound, BadRequest from sqlalchemy.exc import OperationalError +from flask_migrate import upgrade as database_upgrade from importlib.metadata import entry_points from flaschengeist import version as flaschengeist_version @@ -83,14 +84,19 @@ def install_plugin(plugin_name: str): raise NotFound cls = entry_point[0].load() - plugin = cls.query.filter(Plugin.name == plugin_name).one_or_none() + plugin: Plugin = cls.query.filter(Plugin.name == plugin_name).one_or_none() if plugin is None: plugin = cls(name=plugin_name, installed_version=entry_point[0].dist.version) db.session.add(plugin) - db.session.flush() + db.session.commit() # Custom installation steps plugin.install() - db.session.commit() + # Check migrations + directory = entry_point[0].dist.locate_file("") + for loc in entry_point[0].module.split(".") + ["migrations"]: + directory /= loc + if directory.exists(): + database_upgrade(revision=f"{plugin_name}@head") return plugin diff --git a/flaschengeist/database/__init__.py b/flaschengeist/database/__init__.py index 21301d7..66428d0 100644 --- a/flaschengeist/database/__init__.py +++ b/flaschengeist/database/__init__.py @@ -39,11 +39,7 @@ def configure_alembic(config: Config): migrations = [config.get_main_option("script_location") + "/migrations"] # Gather all migration paths - all_plugins = entry_points(group="flaschengeist.plugins") - for plugin in pluginController.get_enabled_plugins(): - entry_point = all_plugins.select(name=plugin.name) - if not entry_point: - continue + for entry_point in entry_points(group="flaschengeist.plugins"): try: directory = entry_point.dist.locate_file("") for loc in entry_point.module.split(".") + ["migrations"]: @@ -52,7 +48,7 @@ def configure_alembic(config: Config): logger.debug(f"Adding migration version path {directory}") migrations.append(str(directory.resolve())) except: - logger.warning(f"Could not load migrations of plugin {plugin.name} for database migration.") + logger.warning(f"Could not load migrations of plugin {entry_point.name} for database migration.") logger.debug("Plugin loading failed", exc_info=True) # write back seperator (we changed it if neither seperator nor locations were specified)