Last active
May 3, 2023 04:45
-
-
Save daaniam/02591e242bf5455041049bba0802ba2d to your computer and use it in GitHub Desktop.
Alembic .env config AsyncEngine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy.engine import Connection | |
from sqlalchemy.ext.asyncio import AsyncEngine | |
from app.core.config import get_config | |
app_config = get_config() | |
config.set_main_option("sqlalchemy.url", app_config.postgres.dsn) | |
def do_run_migrations(connection: Connection) -> None: | |
context.configure(connection=connection, target_metadata=target_metadata) | |
with context.begin_transaction(): | |
context.run_migrations() | |
async 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 = AsyncEngine( | |
engine_from_config( | |
config.get_section(config.config_ini_section), | |
prefix="sqlalchemy.", | |
poolclass=pool.NullPool, | |
future=True, | |
) | |
) | |
async with connectable.connect() as connection: | |
await connection.run_sync(do_run_migrations) | |
await connectable.dispose() | |
if context.is_offline_mode(): | |
run_migrations_offline() | |
else: | |
asyncio.run(run_migrations_online()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment