Skip to content

Commit

Permalink
Make it easier to run the database (#15)
Browse files Browse the repository at this point in the history
- Pull a pre-built database container.
- Add variables for database image and port for easy overrides.
- Detect container runtime with different precedence based on OS.
- Change default DB password
- Update README
- Add make target to stop the database
- Run database for testing

Explicitly set SHELL

    Make defaults to /bin/sh, which is dash on Ubuntu and causes problems with
    using type.

Check the container runtime and show what was found

    This will be redundant in normal operations but will show helpful errors
    if something odd happens.
  • Loading branch information
samdoran authored Jan 16, 2025
1 parent 4d551b0 commit 0ff7831
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 38 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 .
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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.
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

0 comments on commit 0ff7831

Please sign in to comment.