Skip to content

Commit

Permalink
fix case of alembic in case of specific passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
alex75 committed Dec 12, 2023
1 parent 863502b commit 982df79
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
9 changes: 7 additions & 2 deletions alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion cads_broker/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 982df79

Please sign in to comment.