fastapi_cicd

Start a project with:

docker-compose -f deploy/docker-compose.yml --project-directory . up

If you want to develop in docker with autoreload, use this command:

docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . up

This command exposes application on port 8000, mounts current directory and enables autoreload.

But you have to rebuild image every time you modify poetry.lock or pyproject.toml with this command:

docker-compose -f deploy/docker-compose.yml --project-directory . build

Pre-commit

To install pre-commit simply run inside the shell:

pre-commit install

Migrations

If you want to migrate your database, you should run following commands:

# To run all migrations untill the migration with revision_id.
alembic upgrade "<revision_id>"

# To perform all pending migrations.
alembic upgrade "head"

Reverting migrations

If you want to revert migrations, you should run:

# revert all migrations up to: revision_id.
alembic downgrade <revision_id>

# Revert everything.
 alembic downgrade base

Migration generation

To generate migrations you should run:

# For automatic change detection.
alembic revision --autogenerate

# For empty file generation.
alembic revision

Running tests

If you want to run it in docker, simply run:

docker-compose -f deploy/docker-compose.yml --project-directory . run --rm api pytest -vv .
docker-compose -f deploy/docker-compose.yml --project-directory . down

For running tests on your local machine.

  1. you need to start a database.

I prefer doing it with docker:

docker run -p "5432:5432" -e "POSTGRES_PASSWORD=fastapi_cicd" -e "POSTGRES_USER=fastapi_cicd" -e "POSTGRES_DB=fastapi_cicd" postgres:13.6-bullseye
  1. Run the pytest.
pytest -vv .