diff --git a/migrations/env.py b/migrations/env.py index f788062..8b48389 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -2,7 +2,7 @@ import os import sys from logging.config import fileConfig -from sqlalchemy import engine_from_config +from sqlalchemy import create_engine from sqlalchemy import pool from sqlmodel import SQLModel @@ -18,8 +18,11 @@ from app import models # noqa: E402,F401 -- populates SQLModel.metadata via au config = context.config # Share one source of truth for the DB URL with the FastAPI app instead of -# whatever placeholder sits in alembic.ini. -config.set_main_option("sqlalchemy.url", get_settings().database_url) +# whatever placeholder sits in alembic.ini. Stored in `config.attributes` +# (a plain dict) rather than via set_main_option — that goes through +# configparser's interpolation, which crashes on a literal "%" in the URL +# (eg. from a percent-encoded special character in the password). +config.attributes["sqlalchemy_url"] = get_settings().database_url # Interpret the config file for Python logging. # This line sets up loggers basically. @@ -48,7 +51,7 @@ def run_migrations_offline() -> None: script output. """ - url = config.get_main_option("sqlalchemy.url") + url = config.attributes["sqlalchemy_url"] context.configure( url=url, target_metadata=target_metadata, @@ -67,11 +70,7 @@ def run_migrations_online() -> None: and associate a connection with the context. """ - connectable = engine_from_config( - config.get_section(config.config_ini_section, {}), - prefix="sqlalchemy.", - poolclass=pool.NullPool, - ) + connectable = create_engine(config.attributes["sqlalchemy_url"], poolclass=pool.NullPool) with connectable.connect() as connection: context.configure(