feature/migrations, closes #19 #20

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

View File

@ -1,4 +1,5 @@
from pathlib import Path from pathlib import Path
alembic_migrations = str(Path(__file__).resolve().parent / "migrations") alembic_migrations_path = str(Path(__file__).resolve().parent / "migrations")
alembic_script_path = str(Path(__file__).resolve().parent)

View File

@ -5,11 +5,10 @@ from flask_cors import CORS
from datetime import datetime, date from datetime import datetime, date
from flask.json import JSONEncoder, jsonify from flask.json import JSONEncoder, jsonify
from importlib.metadata import entry_points from importlib.metadata import entry_points
from sqlalchemy.exc import OperationalError
from werkzeug.exceptions import HTTPException from werkzeug.exceptions import HTTPException
from flaschengeist import logger from flaschengeist import logger
from flaschengeist.models import Plugin from flaschengeist.controller import pluginController
from flaschengeist.utils.hook import Hook from flaschengeist.utils.hook import Hook
from flaschengeist.config import configure_app from flaschengeist.config import configure_app
@ -38,11 +37,9 @@ class CustomJSONEncoder(JSONEncoder):
@Hook("plugins.loaded") @Hook("plugins.loaded")
def load_plugins(app: Flask): def load_plugins(app: Flask):
app.config["FG_PLUGINS"] = {} app.config["FG_PLUGINS"] = {}
enabled_plugins = Plugin.query.filter(Plugin.enabled == True).all()
all_plugins = entry_points(group="flaschengeist.plugins") all_plugins = entry_points(group="flaschengeist.plugins")
for plugin in enabled_plugins: for plugin in pluginController.get_enabled_plugins():
logger.debug(f"Searching for enabled plugin {plugin.name}") logger.debug(f"Searching for enabled plugin {plugin.name}")
entry_point = all_plugins.select(name=plugin.name) entry_point = all_plugins.select(name=plugin.name)
if not entry_point: if not entry_point:

View File

@ -3,7 +3,7 @@ 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 from flaschengeist.alembic import alembic_migrations_path
from flaschengeist.cli.plugin_cmd import install_plugin_command from flaschengeist.cli.plugin_cmd import install_plugin_command
from flaschengeist.utils.hook import Hook from flaschengeist.utils.hook import Hook
@ -14,7 +14,7 @@ from flaschengeist.utils.hook import Hook
@Hook("plugins.installed") @Hook("plugins.installed")
def install(ctx): def install(ctx):
# Install database # Install database
upgrade(alembic_migrations, revision="heads") upgrade(alembic_migrations_path, revision="heads")
# Install plugins # Install plugins
install_plugin_command(ctx, [], True) install_plugin_command(ctx, [], True)

View File

@ -8,14 +8,35 @@ import sqlalchemy
from typing import Union from typing import Union
from flask import current_app from flask import current_app
from werkzeug.exceptions import NotFound from werkzeug.exceptions import NotFound
from sqlalchemy.exc import OperationalError
from importlib.metadata import entry_points from importlib.metadata import entry_points
from .. import logger from .. import logger
from ..database import db from ..database import db
from ..utils import Hook from ..utils.hook import Hook
from ..models import Plugin, PluginSetting, Notification from ..models import Plugin, PluginSetting, Notification
def get_enabled_plugins():
try:
enabled_plugins = Plugin.query.filter(Plugin.enabled == True).all()
except OperationalError as e:
class PluginStub:
def __init__(self, name) -> None:
self.name = name
logger.error("Could not connect to database or database not initialized! No plugins enabled!")
logger.debug("Can not query enabled plugins", exc_info=True)
enabled_plugins = [
PluginStub("auth"),
PluginStub("roles"),
PluginStub("users"),
PluginStub("scheduler"),
]
return enabled_plugins
def get_setting(plugin_id: str, name: str, **kwargs): def get_setting(plugin_id: str, name: str, **kwargs):
"""Get plugin setting from database """Get plugin setting from database

View File

@ -4,7 +4,9 @@ from flask_sqlalchemy import SQLAlchemy
from importlib.metadata import EntryPoint, entry_points, distribution from importlib.metadata import EntryPoint, entry_points, distribution
from sqlalchemy import MetaData from sqlalchemy import MetaData
from flaschengeist.alembic import alembic_script_path
from flaschengeist import logger from flaschengeist import logger
from flaschengeist.controller import pluginController
# https://alembic.sqlalchemy.org/en/latest/naming.html # https://alembic.sqlalchemy.org/en/latest/naming.html
metadata = MetaData( metadata = MetaData(
@ -31,25 +33,26 @@ def configure_alembic(config: Config):
uninstall can break the alembic version management. uninstall can break the alembic version management.
""" """
# Set main script location # Set main script location
config.set_main_option( config.set_main_option("script_location", alembic_script_path)
"script_location", str(distribution("flaschengeist").locate_file("") / "flaschengeist" / "alembic")
)
# Set Flaschengeist's migrations # Set Flaschengeist's migrations
migrations = [config.get_main_option("script_location") + "/migrations"] migrations = [config.get_main_option("script_location") + "/migrations"]
# Gather all migration paths # Gather all migration paths
ep: EntryPoint all_plugins = entry_points(group="flaschengeist.plugins")
for ep 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 = ep.dist.locate_file("") directory = entry_point.dist.locate_file("")
for loc in ep.module.split(".") + ["migrations"]: for loc in entry_point.module.split(".") + ["migrations"]:
directory /= loc directory /= loc
if directory.exists(): if directory.exists():
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 {ep.name} for database migration.") logger.warning(f"Could not load migrations of plugin {plugin.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)