39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
"""Alembic environment: points migrations at the app's models and database."""
|
|
|
|
import logging
|
|
|
|
from alembic import context
|
|
from sqlalchemy import create_engine, pool
|
|
|
|
from app.config import get_settings
|
|
from app.models import Base, alembic_compare_type, alembic_render_item
|
|
|
|
if not logging.getLogger().handlers:
|
|
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s: %(message)s")
|
|
|
|
target_metadata = Base.metadata
|
|
url = get_settings().database_url
|
|
|
|
|
|
def configure(**kw) -> None: # noqa: ANN003
|
|
context.configure(
|
|
target_metadata=target_metadata,
|
|
render_item=alembic_render_item,
|
|
compare_type=alembic_compare_type,
|
|
render_as_batch=url.startswith("sqlite"), # SQLite can't ALTER most things in place
|
|
**kw,
|
|
)
|
|
|
|
|
|
if context.is_offline_mode():
|
|
configure(url=url, literal_binds=True)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
else:
|
|
engine = create_engine(url, poolclass=pool.NullPool)
|
|
with engine.connect() as connection:
|
|
configure(connection=connection)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
engine.dispose()
|