Shiladitya Bose
Shiladitya Bose

Reputation: 1203

How to configure pytest for alembic migrations

I am running a FastAPI application with PostgreSQL database and SQLAlchemy. I used sessionmaker to create and use database sessions, and I have a custom alembic migration script that needs to be performed on the database.

from app.core.config import DATABASE_URL
from app.models import User, Passwords
from app.db import Base

from alembic import context

config = context.config
config.set_main_option('sqlalchemy.url', DATABASE_URL)


fileConfig(config.config_file_name)


target_metadata = Base.metadata



def run_migrations_offline():
   
    url = config.get_main_option("sqlalchemy.url")
    context.configure(
        url=url,
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
    )

    with context.begin_transaction():
        context.run_migrations()

I want to be able to create a test database, apply the same migration, and teardown after the test. I have tried the following in the conftest.py

# Apply migrations at beginning and end of testing session
@pytest.fixture(scope="session")
def apply_migrations():
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    environ["TEST"] = "True"
    config = Config("alembic.ini")
    alembic.command.upgrade(config, "head")
    yield
    alembic.command.downgrade(config, "base")

@pytest.fixture(scope="session")
def create_test_db():
    url = f"{str(DATABASE_URL)}_test"

    if database_exists(url):
        drop_database(url)
    create_database(url)
    apply_migrations()
    engine = create_engine(url, echo=True)

    yield
    drop_database(url)

But it does not seem to do anything. I am new to testing and I can't find a decent guide on how to set this up properly. So any kind of help would be greatly appreciated.

Upvotes: 8

Views: 10355

Answers (1)

Ezra Epstein
Ezra Epstein

Reputation: 31

I'm not sure about putting apply_migrations() in the DB creation - as that is driven by Alembic and you're here trying to setup the environment for running Alembic (i.e., that looks circular to me), but for just setting up the DB:

@pytest.fixture(scope="session")
def alembic_engine():
    set_up_empty_test_db()
    ...

Where set_up_empty_test_db() is a function that does what its name implies. In my case that function runs a docker container that starts Postgres and then creates a blank/empty database.

See https://pytest-alembic.readthedocs.io/en/latest/setup.html#setting-up-fixtures

Upvotes: 0

Related Questions