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>
87 lines
2.6 KiB
Python
87 lines
2.6 KiB
Python
import os
|
|
import sys
|
|
from logging.config import fileConfig
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy import pool
|
|
from sqlmodel import SQLModel
|
|
|
|
from alembic import context
|
|
|
|
sys.path.append(os.getcwd()) # make `app` importable regardless of cwd
|
|
|
|
from app.core.config import get_settings # noqa: E402
|
|
from app import models # noqa: E402,F401 -- populates SQLModel.metadata via auto-import
|
|
|
|
# this is the Alembic Config object, which provides
|
|
# access to the values within the .ini file in use.
|
|
config = context.config
|
|
|
|
# Share one source of truth for the DB URL with the FastAPI app instead of
|
|
# 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.
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
# add your model's MetaData object here
|
|
# for 'autogenerate' support
|
|
target_metadata = SQLModel.metadata
|
|
|
|
# other values from the config, defined by the needs of env.py,
|
|
# can be acquired:
|
|
# my_important_option = config.get_main_option("my_important_option")
|
|
# ... etc.
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
"""Run migrations in 'offline' mode.
|
|
|
|
This configures the context with just a URL
|
|
and not an Engine, though an Engine is acceptable
|
|
here as well. By skipping the Engine creation
|
|
we don't even need a DBAPI to be available.
|
|
|
|
Calls to context.execute() here emit the given string to the
|
|
script output.
|
|
|
|
"""
|
|
url = config.attributes["sqlalchemy_url"]
|
|
context.configure(
|
|
url=url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def run_migrations_online() -> None:
|
|
"""Run migrations in 'online' mode.
|
|
|
|
In this scenario we need to create an Engine
|
|
and associate a connection with the context.
|
|
|
|
"""
|
|
connectable = create_engine(config.attributes["sqlalchemy_url"], poolclass=pool.NullPool)
|
|
|
|
with connectable.connect() as connection:
|
|
context.configure(
|
|
connection=connection, target_metadata=target_metadata
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|