Skip to content

Commit

Permalink
✨(api) integrate postgresql database support
Browse files Browse the repository at this point in the history
We are now able to use a PostgreSQL database to persist data.
  • Loading branch information
jmaupetit committed Mar 28, 2024
1 parent 5e62436 commit 1a68a01
Show file tree
Hide file tree
Showing 23 changed files with 865 additions and 13 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ jobs:
test-api:
needs: build-api
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14
env:
POSTGRES_DB: test-qualicharge-api
POSTGRES_USER: qualicharge
POSTGRES_PASSWORD: pass
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
defaults:
run:
working-directory: ./src/api
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to

## [Unreleased]

## Added

- Integrate PostgreSQL database persistency in an asynchronous context

## [0.1.0] - 2024-03-26

### Added
Expand Down
36 changes: 33 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,27 @@ default: help
# -- Docker/compose
bootstrap: ## bootstrap the project for development
bootstrap: \
build
build \
migrate-api \
create-api-test-db
.PHONY: bootstrap

build: ## build the app container(s)
$(COMPOSE) build
.PHONY: build

logs: ## display OCPI server logs (follow mode)
down: ## stop and remove all containers
@$(COMPOSE) down
.PHONY: down

logs: ## display all services logs (follow mode)
@$(COMPOSE) logs -f
.PHONY: logs

logs-api: ## display API server logs (follow mode)
@$(COMPOSE) logs -f api
.PHONY: logs-api

run: ## run the whole stack
$(COMPOSE) up -d
.PHONY: run
Expand All @@ -38,7 +48,27 @@ stop: ## stop all servers
@$(COMPOSE) stop
.PHONY: stop

# -- OCPI
# -- Provisioning
create-api-test-db: ## create API test database
@echo "Creating api service test database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "create database \"$${QUALICHARGE_TEST_DB_NAME}\";"' || echo "Duly noted, skipping database creation."
.PHONY: create-api-test-db

drop-api-test-db: ## drop API test database
@echo "Droping api service test database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "drop database \"$${QUALICHARGE_TEST_DB_NAME}\";"' || echo "Duly noted, skipping database deletion."
.PHONY: drop-api-test-db

migrate-api: ## run alembic database migrations for the api service
@echo "Running api service database engine…"
@$(COMPOSE) up -d --wait postgresql
@echo "Creating api service database…"
@$(COMPOSE) exec postgresql bash -c 'psql "postgresql://$${POSTGRES_USER}:$${POSTGRES_PASSWORD}@$${QUALICHARGE_DB_HOST}:$${QUALICHARGE_DB_PORT}/postgres" -c "create database \"$${QUALICHARGE_DB_NAME}\";"' || echo "Duly noted, skipping database creation."
@echo "Running migrations for api service…"
@bin/alembic upgrade head
.PHONY: migrate-api

# -- API
lint: ## lint api python sources
lint: \
lint-black \
Expand Down
13 changes: 13 additions & 0 deletions bin/alembic
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -eo pipefail

declare DOCKER_USER
DOCKER_UID="$(id -u)"
DOCKER_GID="$(id -g)"
DOCKER_USER="${DOCKER_UID}:${DOCKER_GID}"

DOCKER_USER=${DOCKER_USER} \
DOCKER_UID=${DOCKER_UID} \
DOCKER_GID=${DOCKER_GID} \
docker compose run --rm api pipenv run alembic -c qualicharge/alembic.ini "$@"
2 changes: 1 addition & 1 deletion bin/pipenv
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

set -eo pipefail

docker compose run --rm -u "pipenv:pipenv" api pipenv "$@"
docker compose run --rm --no-deps -u "pipenv:pipenv" api pipenv "$@"
docker compose build api
18 changes: 17 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
version: '3.8'
version: "3.8"

services:
postgresql:
image: postgres:14
env_file:
- env.d/postgresql
- env.d/api
healthcheck:
test:
- "CMD-SHELL"
- "pg_isready"
- "-d"
- "$${QUALICHARGE_DB_NAME}"
interval: 10s
timeout: 5s
retries: 5

api:
build:
Expand All @@ -16,3 +30,5 @@ services:
- env.d/api
volumes:
- ./src/api:/app
depends_on:
- postgresql
9 changes: 9 additions & 0 deletions env.d/api
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
PORT=8000
QUALICHARGE_ALLOWED_HOSTS=["http://localhost:8010"]
QUALICHARGE_DB_ENGINE=postgresql
QUALICHARGE_DB_HOST=postgresql
QUALICHARGE_DB_NAME=qualicharge-api
QUALICHARGE_DB_PASSWORD=pass
QUALICHARGE_DB_PORT=5432
QUALICHARGE_DB_USER=qualicharge
QUALICHARGE_DEBUG=1
QUALICHARGE_TEST_DB_NAME=test-qualicharge-api
3 changes: 3 additions & 0 deletions env.d/postgresql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POSTGRES_DB=qualicharge-api
POSTGRES_USER=qualicharge
POSTGRES_PASSWORD=pass
8 changes: 6 additions & 2 deletions src/api/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ verify_ssl = true
name = "pypi"

[packages]
alembic = "==1.13.1"
fastapi = "==0.110.0"
psycopg2-binary = "==2.9.9"
pydantic-settings = "==2.2.1"
setuptools = "==69.2.0"
uvicorn = {extras = ["standard"], version = "==0.29.0"}
sqlmodel = "==0.0.16"
uvicorn = {extras = ["standard"] }

[dev-packages]
black = "==24.3.0"
honcho = "==1.1.0"
httpx = "==0.27.0"
mypy = "==1.9.0"
polyfactory = "==2.15.0"
pytest = "==8.1.1"
pytest-httpx = "==0.30.0"
ruff = "==0.3.4"

[requires]
Expand Down
Loading

0 comments on commit 1a68a01

Please sign in to comment.