Running migrations in pytest #1450
Answered
by
CaselIT
ZeN220
asked this question in
Usage Questions
-
Im tried run alembic migrations like this code: from alembic.command import downgrade, upgrade
from alembic.config import Config as AlembicConfig
from pytest_asyncio import fixture
from sqlalchemy.ext.asyncio import (
AsyncEngine,
AsyncSession,
async_sessionmaker,
create_async_engine,
)
from app.config import Config
@fixture(scope="function")
async def engine(config: Config) -> AsyncEngine:
engine = create_async_engine(config.postgres.dsn)
yield engine
await engine.dispose()
@fixture(scope="function")
async def session_maker(
engine: AsyncEngine,
) -> async_sessionmaker[AsyncSession]:
return async_sessionmaker(engine, expire_on_commit=False)
@fixture(scope="function")
async def session(
session_maker: async_sessionmaker,
engine: AsyncEngine,
alembic_config: AlembicConfig,
) -> AsyncSession:
upgrade(alembic_config, "head")
async with session_maker() as session:
yield session
await session.rollback()
downgrade(alembic_config, "base") but because under hood
how i can effective runalembic migrations in my tests? |
Beta Was this translation helpful? Give feedback.
Answered by
CaselIT
Apr 2, 2024
Replies: 1 comment 7 replies
-
Hi, Asycio makes everything complex... |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
change the
run_migrations_online
to do the following