AgentEvalTool/migrations/env.py

73 lines
1.9 KiB
Python

"""Alembic env.py — wires Alembic up to the SQLModel metadata and the
project's SQLite URL so ``alembic upgrade head`` and ``alembic revision
--autogenerate`` work out of the box.
"""
from logging.config import fileConfig
from sqlalchemy import engine_from_config, pool
from alembic import context
# Import the DB module so its SQLModel table classes register in metadata.
from agenteval.storage.db import DATABASE_URL # noqa: F401
from agenteval.storage.db import ( # noqa: F401
EvalResultDB,
EvalRunDB,
EvalTargetDB,
FileCategoryDB,
FileRecordDB,
ModelConfigDB,
ScenarioDB,
ScenarioModelBindingDB,
TurnDB,
)
from sqlmodel import SQLModel
config = context.config
# Point Alembic at the same SQLite file the app uses.
config.set_main_option("sqlalchemy.url", DATABASE_URL)
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = SQLModel.metadata
def run_migrations_offline() -> None:
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
render_as_batch=True, # Required for SQLite ALTER TABLE support.
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
render_as_batch=True, # Required for SQLite ALTER TABLE support.
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()