Skip to content

Running migrations in pytest #1450

Answered by CaselIT
ZeN220 asked this question in Usage Questions
Apr 1, 2024 · 1 comments · 7 replies
Discussion options

You must be logged in to vote

right python really don't want to make you use asyncio from sync code...

You can probably use the same functionality used by sqlalchemy.
Try something like this (not 100% sure it will work)
In the test:

    async with engine.connect() as c:
        await c.run_sync(lambda _: upgrade(alembic_config, "head"))
    async with session_maker() as session:
        yield session
        await session.rollback()
    async with engine.connect() as c:
        await c.run_sync(lambda _: downgrade(alembic_config, "base"))

change the run_migrations_online to do the following

from sqlalchemy.util.concurrency import await_only, in_greenlet

if in_greenlet():
    await_only(run_async_migrations())
else:
    

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@ZeN220
Comment options

@CaselIT
Comment options

@ZeN220
Comment options

@CaselIT
Comment options

Answer selected by ZeN220
@Anton-Kot
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants