From 982df792ded271cf11d6188ff33962fe515fc67e Mon Sep 17 00:00:00 2001 From: Alessio Siniscalchi Date: Tue, 12 Dec 2023 15:23:46 +0100 Subject: [PATCH] fix case of alembic in case of specific passwords --- alembic.ini | 9 +++++++-- alembic/env.py | 24 +++++++++++++++++------- cads_broker/database.py | 3 ++- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/alembic.ini b/alembic.ini index d9f94e0c..88dd779c 100644 --- a/alembic.ini +++ b/alembic.ini @@ -60,8 +60,13 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = driver://user:pass@localhost/dbname - +drivername = drivername +username = username +password = password +host = host +port = port +database = database +sqlalchemy.url = %(drivername)s://%(username)s:%(password)s@%(host)s:%(port)s/%(database)s [post_write_hooks] # post_write_hooks defines scripts or Python functions that are run diff --git a/alembic/env.py b/alembic/env.py index c7c0ddd7..90dea6e6 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -33,7 +33,14 @@ def run_migrations_offline() -> None: Calls to alembic.context.execute() here emit the given string to the script output. """ - url = config.get_main_option("sqlalchemy.url") + url = sa.engine.URL.create( + drivername=config.get_main_option("drivername"), # type: ignore + username=config.get_main_option("username"), + password=config.get_main_option("password"), + host=config.get_main_option("host"), + port=config.get_main_option("port"), # type: ignore + database=config.get_main_option("database"), + ) alembic.context.configure( url=url, target_metadata=cads_broker.database.BaseModel.metadata, @@ -50,13 +57,16 @@ def run_migrations_online() -> None: In this scenario we need to create an Engine and associate a connection with the alembic.context. """ - connectable = sa.engine_from_config( - config.get_section(config.config_ini_section, {}), - prefix="sqlalchemy.", - poolclass=sa.pool.NullPool, + url = sa.engine.URL.create( + drivername=config.get_main_option("drivername"), # type: ignore + username=config.get_main_option("username"), + password=config.get_main_option("password"), + host=config.get_main_option("host"), + port=config.get_main_option("port"), # type: ignore + database=config.get_main_option("database"), ) - - with connectable.connect() as connection: + engine = sa.create_engine(url, poolclass=sa.pool.NullPool) + with engine.connect() as connection: alembic.context.configure( connection=connection, target_metadata=cads_broker.database.BaseModel.metadata, diff --git a/cads_broker/database.py b/cads_broker/database.py index 7761efd5..ac1e0a96 100644 --- a/cads_broker/database.py +++ b/cads_broker/database.py @@ -584,7 +584,8 @@ def init_database(connection_string: str, force: bool = False) -> sa.engine.Engi os.chdir(migration_directory) alembic_config_path = os.path.join(migration_directory, "alembic.ini") alembic_cfg = alembic.config.Config(alembic_config_path) - alembic_cfg.set_main_option("sqlalchemy.url", connection_string) + for option in ["drivername", "username", "password", "host", "port", "database"]: + alembic_cfg.set_main_option(option, str(getattr(engine.url, option))) if not sqlalchemy_utils.database_exists(engine.url): sqlalchemy_utils.create_database(engine.url) # cleanup and create the schema