feature/migrations, closes #19 #20

Merged
crimsen merged 28 commits from feature/migrations into develop 2023-03-02 05:37:11 +00:00
3 changed files with 12 additions and 11 deletions
Showing only changes of commit 88a4dc24f2 - Show all commits

View File

@ -3,7 +3,6 @@ from click.decorators import pass_context
from flask.cli import with_appcontext from flask.cli import with_appcontext
from flask_migrate import upgrade from flask_migrate import upgrade
from flaschengeist.alembic import alembic_migrations_path
from flaschengeist.controller import pluginController from flaschengeist.controller import pluginController
from flaschengeist.utils.hook import Hook from flaschengeist.utils.hook import Hook
@ -16,7 +15,7 @@ def install(ctx: click.Context):
plugins = pluginController.get_enabled_plugins() plugins = pluginController.get_enabled_plugins()
# Install database # Install database
upgrade(alembic_migrations_path, revision="heads") upgrade(revision="flaschengeist@head")
# Install plugins # Install plugins
for plugin in plugins: for plugin in plugins:

View File

@ -7,6 +7,7 @@ from typing import Union
from flask import current_app from flask import current_app
from werkzeug.exceptions import NotFound, BadRequest from werkzeug.exceptions import NotFound, BadRequest
from sqlalchemy.exc import OperationalError from sqlalchemy.exc import OperationalError
from flask_migrate import upgrade as database_upgrade
from importlib.metadata import entry_points from importlib.metadata import entry_points
from flaschengeist import version as flaschengeist_version from flaschengeist import version as flaschengeist_version
@ -83,14 +84,19 @@ def install_plugin(plugin_name: str):
raise NotFound raise NotFound
cls = entry_point[0].load() 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: if plugin is None:
plugin = cls(name=plugin_name, installed_version=entry_point[0].dist.version) plugin = cls(name=plugin_name, installed_version=entry_point[0].dist.version)
db.session.add(plugin) db.session.add(plugin)
db.session.flush() db.session.commit()
# Custom installation steps # Custom installation steps
plugin.install() 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 return plugin

View File

@ -39,11 +39,7 @@ def configure_alembic(config: Config):
migrations = [config.get_main_option("script_location") + "/migrations"] migrations = [config.get_main_option("script_location") + "/migrations"]
# Gather all migration paths # Gather all migration paths
all_plugins = entry_points(group="flaschengeist.plugins") for entry_point in 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
try: try:
directory = entry_point.dist.locate_file("") directory = entry_point.dist.locate_file("")
for loc in entry_point.module.split(".") + ["migrations"]: 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}") logger.debug(f"Adding migration version path {directory}")
migrations.append(str(directory.resolve())) migrations.append(str(directory.resolve()))
except: 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) logger.debug("Plugin loading failed", exc_info=True)
# write back seperator (we changed it if neither seperator nor locations were specified) # write back seperator (we changed it if neither seperator nor locations were specified)