diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 4c12317..53d3ad5 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -25,6 +25,9 @@ jobs: with: python-version: ${{ matrix.python-version }} + - name: Run PostgreSQL + run: make start-db + - name: Install dependencies run: | python -m pip install --upgrade pip diff --git a/Makefile b/Makefile index 201c78f..7c01fc4 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,23 @@ PROJECT_DIR=$(shell pwd) PYTHON_VERSION = $(shell python -V | cut -d ' ' -f 2 | cut -d '.' -f 1,2) export PIP_DISABLE_PIP_VERSION_CHECK = 1 +DB_IMAGE ?= quay.io/samdoran/digital-roadmap-data +DB_PORT ?= 5432 + +# Set the shell because otherwise this defaults to /bin/sh, +# which is dash on Ubuntu. The type builtin for dash does not accept flags. +SHELL = /bin/bash + +# Determine container runtime, preferring Docker on macOS +OS = $(shell uname) +CONTAINER_RUNTIMES = podman docker +ifeq ($(OS), Darwin) + CONTAINER_RUNTIMES = docker podman +endif + +CONTAINER_RUNTIME ?= $(shell type -P $(CONTAINER_RUNTIMES) | head -n 1) + + default: install .PHONY: venv @@ -23,6 +40,25 @@ install: venv install-dev: venv $(PIP) install -r requirements/requirements-dev-$(PYTHON_VERSION).txt +.PHONY: check-container-runtime +check-container-runtime: +ifeq ($(strip $(CONTAINER_RUNTIME)),) + @echo "Missing container runtime. Could not find '$(CONTAINER_RUNTIMES)' in PATH." + @exit 1 +else + @echo Found container runtime \'$(CONTAINER_RUNTIME)\' +endif + + +.PHONY: start-db +start-db: stop-db + $(CONTAINER_RUNTIME) run --rm -d -p $(DB_PORT):5432 --name digital-roadmap-data $(DB_IMAGE) + +.PHONY: stop-db +stop-db: check-container-runtime + @$(CONTAINER_RUNTIME) stop digital-roadmap-data > /dev/null 2>&1 || true + @sleep 0.1 + .PHONY: run run: $(VENV_DIR)/bin/fastapi run app/main.py --reload --host 127.0.0.1 --port 8081 @@ -44,5 +80,5 @@ test: @$(PYTEST) .PHONY: build -build: - docker build -t digital_roadmap:latest -f Containerfile . +build: check-container-runtime + $(CONTAINER_RUNTIME) build -t digital-roadmap:latest -f Containerfile . diff --git a/README.md b/README.md index 9df2433..cc8e118 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Create a virtual environment, install the requirements, and run the server. ```shell make install -make run +make start-db run ``` This runs a server using the default virtual environment. Documentation can be found at `http://127.0.0.1:8081/docs`. @@ -26,7 +26,7 @@ Install the developer tools and run the server. ```shell make install-dev -make run +make start-db run ``` Alternatively you may create your own virtual environment, install the requirements, and run the server manually. @@ -36,6 +36,16 @@ pip install -r requirements/requirements-dev-{Python version}.txt fastapi run app/main.py --reload --host 127.0.0.1 --port 8081 ``` +The database runs in a container and contains data already. To specify a different container image, set `DB_IMAGE`. + +```shell +export DB_IMAGE=digital-roadmap:latest +make start-db +``` + +To restart the database container, run `make start-db`. + +To stop the database, run `make stop-db`. ### Testing diff --git a/app/config.py b/app/config.py index d3d529c..e84f19d 100644 --- a/app/config.py +++ b/app/config.py @@ -4,7 +4,7 @@ DB_NAME = os.getenv("DB_NAME", "digital_roadmap") DB_USER = os.getenv("DB_USER", "postgres") -DB_PASSWORD = os.getenv("DB_PASSWORD", "password") +DB_PASSWORD = os.getenv("DB_PASSWORD", "postgres") DB_HOST = os.getenv("DB_HOST", "localhost") DB_PORT = os.getenv("DB_PORT", 5432) SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"