From a8255f3621972df2e70945a833192e8c135d44ae Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 19 Aug 2023 13:23:20 +0200 Subject: [PATCH] Pass the configuration file used by the application to the Alembic process. The database settings could also be overridden in the configuration file besides the command line. We should therefore pass the path to the runtime configuration file, so the Alembic process can initialize its configuration from the same file and use the same settings. --- platypush/config/__init__.py | 7 +++++++ platypush/entities/_base.py | 3 +++ platypush/migrations/alembic/env.py | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/platypush/config/__init__.py b/platypush/config/__init__.py index d2de1fde..28c6beaa 100644 --- a/platypush/config/__init__.py +++ b/platypush/config/__init__.py @@ -530,5 +530,12 @@ class Config: # pylint: disable=protected-access cls._get_instance()._config[key] = value + @classmethod + def get_file(cls) -> str: + """ + :return: The main configuration file path. + """ + return cls._get_instance().config_file + # vim:sw=4:ts=4:et: diff --git a/platypush/entities/_base.py b/platypush/entities/_base.py index c3089dde..2a739124 100644 --- a/platypush/entities/_base.py +++ b/platypush/entities/_base.py @@ -29,6 +29,7 @@ from sqlalchemy.orm import ColumnProperty, backref, relationship from sqlalchemy.orm.exc import ObjectDeletedError import platypush +from platypush.config import Config from platypush.common.db import Base from platypush.message import JSONAble, Message @@ -355,6 +356,8 @@ def run_db_migrations(): '-c', alembic_ini, '-x', + f'CFGFILE={Config.get_file()}', + '-x', f'DBNAME={_get_db_engine().url}', 'upgrade', 'head', diff --git a/platypush/migrations/alembic/env.py b/platypush/migrations/alembic/env.py index 406b1f6e..d4aba707 100644 --- a/platypush/migrations/alembic/env.py +++ b/platypush/migrations/alembic/env.py @@ -74,6 +74,10 @@ def run_migrations_online() -> None: def set_db_engine(): + app_conf_file = context.get_x_argument(as_dictionary=True).get('CFGFILE') + if app_conf_file: + Config.init(app_conf_file) + engine_url = context.get_x_argument(as_dictionary=True).get('DBNAME') if not engine_url: db_conf = Config.get('db')