didier/alembic/env.py

52 lines
1.3 KiB
Python
Raw Normal View History

import asyncio
from logging.config import fileConfig
2022-07-19 18:49:22 +02:00
from sqlalchemy.ext.asyncio import AsyncEngine
2022-07-19 18:49:22 +02:00
from alembic import context
from database.engine import engine
from database.models import Base
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# 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)
target_metadata = Base.metadata
2022-07-19 18:49:22 +02:00
def do_run_migrations(connection):
context.configure(connection=connection, target_metadata=target_metadata, render_as_batch=True)
with context.begin_transaction():
context.run_migrations()
2022-07-19 18:49:22 +02:00
async def run_async_migrations(connectable: AsyncEngine):
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
2022-07-19 18:49:22 +02:00
await connectable.dispose()
2022-07-19 18:49:22 +02:00
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.
"""
2022-07-19 18:49:22 +02:00
connectable = context.config.attributes.get("connection", None) or engine
2022-07-19 18:49:22 +02:00
if isinstance(connectable, AsyncEngine):
asyncio.run(run_async_migrations(connectable))
else:
do_run_migrations(connectable)
2022-07-19 18:49:22 +02:00
run_migrations_online()