Ret alembic-crash når DATABASE_URL indeholder et bogstaveligt "%"

migrations/env.py brugte config.set_main_option() til at dele
databasens URL med alembic.ini — det kalder internt Pythons
configparser.set(), som fortolker "%" som sin egen
interpolations-syntaks og fejler på ethvert kodeord der indeholder
et procenttegn (fx fra en almindelig password-generator, eller
percent-encoded specialtegn i en URL).

Fundet under produktionsopsætning: brugerens Postgres-kodeord
indeholdt "%", hvilket crashede `alembic upgrade head` med
"ValueError: invalid interpolation syntax".

URL'en gemmes nu i config.attributes (en almindelig dict, ingen
configparser-fortolkning involveret) i stedet, og engine'en i
run_migrations_online bygges direkte med create_engine() i stedet
for engine_from_config() (som ellers ville læse URL'en tilbage via
configparser igen). Verificeret lokalt med både et normalt
DATABASE_URL og et med et bogstaveligt "%" i kodeordet.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Carsten Gram 2026-09-29 12:15:36 +02:00
parent d3e6b92d9f
commit 2f52d2c846

View file

@ -2,7 +2,7 @@ import os
import sys import sys
from logging.config import fileConfig from logging.config import fileConfig
from sqlalchemy import engine_from_config from sqlalchemy import create_engine
from sqlalchemy import pool from sqlalchemy import pool
from sqlmodel import SQLModel from sqlmodel import SQLModel
@ -18,8 +18,11 @@ from app import models # noqa: E402,F401 -- populates SQLModel.metadata via au
config = context.config config = context.config
# Share one source of truth for the DB URL with the FastAPI app instead of # Share one source of truth for the DB URL with the FastAPI app instead of
# whatever placeholder sits in alembic.ini. # whatever placeholder sits in alembic.ini. Stored in `config.attributes`
config.set_main_option("sqlalchemy.url", get_settings().database_url) # (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. # Interpret the config file for Python logging.
# This line sets up loggers basically. # This line sets up loggers basically.
@ -48,7 +51,7 @@ def run_migrations_offline() -> None:
script output. script output.
""" """
url = config.get_main_option("sqlalchemy.url") url = config.attributes["sqlalchemy_url"]
context.configure( context.configure(
url=url, url=url,
target_metadata=target_metadata, target_metadata=target_metadata,
@ -67,11 +70,7 @@ def run_migrations_online() -> None:
and associate a connection with the context. and associate a connection with the context.
""" """
connectable = engine_from_config( connectable = create_engine(config.attributes["sqlalchemy_url"], poolclass=pool.NullPool)
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection: with connectable.connect() as connection:
context.configure( context.configure(