This is a tutorial app build for my blog post
docker-compose up
docker-compose run app pytest
The database is managed via sqlalchemy and alembic. The Base Model is set up in tables/base_class and imported all underlying models. Base.py imports all models to feed it to alembic
In config.settings you pass localhost (or 127.0.0.1) as db_host. In the .env file it is the name of the DB so docker will define the hostname.
import Base models, import configs to load all metadata to alembic
from app.core.config import settings # noqa
from app.db.base import Base # noqa
config = context.config
config.set_main_option("sqlalchemy.url", settings.DATABASE_URL)
target_metadata = Base.metadata
fileConfig(config.config_file_name)`
alembic init app/db/migrations/
- run alembic stamp to move the migration scripts to the latest version. If the DB is behind,
run
alembic history
to find the ID of the last migration file.docker-compose run app alembic revision --autogenerate
- When the file is created execute the migration
docker-compose run app alembic upgrade head