[db] Fix automatic migration upgrade for plugins and core

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
This commit is contained in:
Ferdinand Thiessen 2022-08-25 18:44:21 +02:00
parent 0698327ef5
commit 88a4dc24f2
3 changed files with 12 additions and 11 deletions

View File

@ -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:

View File

@ -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

View File

@ -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)