import asyncio from logging.config import fileConfig from alembic import context from sqlalchemy import engine_from_config from sqlalchemy import pool from sqlalchemy.ext.asyncio import AsyncEngine from dotenv import dotenv_values from db import Base config = context.config if config.config_file_name is not None: fileConfig(config.config_file_name) env = dotenv_values(".env") assert env['POSTGRES_URL'] config.set_main_option('sqlalchemy.url', env['POSTGRES_URL']) target_metadata = Base.metadata def run_migrations_online(): connectable = context.config.attributes.get("connection", None) if connectable is None: connectable = AsyncEngine( engine_from_config( context.config.get_section(context.config.config_ini_section), prefix="sqlalchemy.", poolclass=pool.NullPool, future=True ) ) if isinstance(connectable, AsyncEngine): asyncio.run(run_async_migrations(connectable)) else: do_run_migrations(connectable) async def run_async_migrations(connectable): async with connectable.connect() as connection: await connection.run_sync(do_run_migrations) await connectable.dispose() def do_run_migrations(connection): context.configure( connection=connection, target_metadata=target_metadata, compare_type=True, ) with context.begin_transaction(): context.run_migrations() run_migrations_online()