feat(backend): handle settings by environment (#13)
* build(backend): db provisioning tooling * feat(backend): split application configuration by env * docs(backend): update mention of local db setup
This commit is contained in:
parent
ee9a9d8dc3
commit
da2bf786d5
10 changed files with 61 additions and 21 deletions
3
.github/workflows/backend-pipeline.yml
vendored
3
.github/workflows/backend-pipeline.yml
vendored
|
@ -7,6 +7,9 @@ on:
|
|||
required: true
|
||||
type: string
|
||||
|
||||
env:
|
||||
ROTINI_CI: 1
|
||||
|
||||
jobs:
|
||||
setup:
|
||||
runs-on: ubuntu-latest
|
||||
|
|
|
@ -31,6 +31,12 @@ tasks:
|
|||
- "../.env"
|
||||
cmd: source {{ .VENV_ACTIVATE }} && python -m uvicorn main:app
|
||||
dir: backend/rotini
|
||||
start-db:
|
||||
desc: "Provisions a local Postgres database."
|
||||
dotenv:
|
||||
- ".env"
|
||||
cmd: . script/provision-db
|
||||
dir: backend
|
||||
migrate:
|
||||
desc: "Applies migrations. Usage: be:migrate -- <up|down>"
|
||||
deps: [bootstrap]
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
Before starting, make sure to run the [root bootstrap script](../README.md#Development) so the `task` commands are enabled.
|
||||
|
||||
Locally, a Postgres database that can be used for development can be started via `docker-compose up -d`.
|
||||
`task be:start-db` will provision a database for local usage.
|
||||
|
||||
An envfile should be present at `.env` and should define:
|
||||
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
version: '3.7'
|
||||
|
||||
services:
|
||||
database:
|
||||
image: postgres:15.3
|
||||
env_file:
|
||||
- .env
|
||||
ports:
|
||||
- 5432:5432
|
||||
environment:
|
||||
POSTGRES_PASSWORD: $DATABASE_PASSWORD
|
5
backend/rotini/envs/ci.py
Normal file
5
backend/rotini/envs/ci.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
DATABASE_USERNAME = "postgres"
|
||||
DATABASE_PASSWORD = "test"
|
||||
DATABASE_HOST = "localhost"
|
||||
DATABASE_PORT = 5432
|
||||
DATABASE_NAME = "postgres"
|
5
backend/rotini/envs/local.py
Normal file
5
backend/rotini/envs/local.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
DATABASE_USERNAME = "postgres"
|
||||
DATABASE_PASSWORD = "test"
|
||||
DATABASE_HOST = "localhost"
|
||||
DATABASE_PORT = 5432
|
||||
DATABASE_NAME = "postgres"
|
5
backend/rotini/envs/test.py
Normal file
5
backend/rotini/envs/test.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
DATABASE_USERNAME = "postgres"
|
||||
DATABASE_PASSWORD = "test"
|
||||
DATABASE_HOST = "localhost"
|
||||
DATABASE_PORT = 5431
|
||||
DATABASE_NAME = "postgres"
|
|
@ -1,5 +1,12 @@
|
|||
DATABASE_USERNAME = "postgres"
|
||||
DATABASE_PASSWORD = "test"
|
||||
DATABASE_HOST = "localhost"
|
||||
DATABASE_PORT = 5431
|
||||
DATABASE_NAME = "postgres"
|
||||
# pylint: disable=unused-import, wildcard-import, unused-wildcard-import
|
||||
import os
|
||||
|
||||
IS_CI = os.getenv("ROTINI_CI")
|
||||
IS_TEST = os.getenv("ROTINI_TEST")
|
||||
|
||||
if IS_CI is not None:
|
||||
from envs.ci import *
|
||||
elif IS_TEST is not None:
|
||||
from envs.test import *
|
||||
else:
|
||||
from envs.local import *
|
||||
|
|
20
backend/script/provision-db
Normal file
20
backend/script/provision-db
Normal file
|
@ -0,0 +1,20 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Provisions a Postgres database locally.
|
||||
#
|
||||
# This is useful if you are not hosting your database instance
|
||||
# elsewhere or want a simple setup for development purposes.
|
||||
|
||||
docker run \
|
||||
--name rotini_db \
|
||||
-e POSTGRES_PASSWORD=$DATABASE_PASSWORD \
|
||||
-e POSTGRES_USER=$DATABASE_USER \
|
||||
-e POSTGRES_DB=$DATABASE_NAME \
|
||||
-v $DATABASE_STORAGE_PATH:/var/lib/postgresql/data \
|
||||
-p 5432:5432 \
|
||||
-d \
|
||||
postgres:15.4
|
||||
|
||||
sleep 3
|
||||
|
||||
PYTHONPATH=rotini .venv/bin/python rotini/migrations/migrate.py up
|
|
@ -11,8 +11,8 @@ docker run \
|
|||
|
||||
sleep 2
|
||||
|
||||
PYTHONPATH=rotini .venv/bin/python rotini/migrations/migrate.py up || echo "Migrations failed."
|
||||
.venv/bin/pytest . -vv -s || echo "Test run failed."
|
||||
ROTINI_TEST=1 PYTHONPATH=rotini .venv/bin/python rotini/migrations/migrate.py up || echo "Migrations failed."
|
||||
ROTINI_TEST=1 .venv/bin/pytest . -vv -s || echo "Test run failed."
|
||||
|
||||
docker stop $TEST_DB_CONTAINER
|
||||
docker remove $TEST_DB_CONTAINER
|
||||
docker stop $TEST_DB_CONTAINER > /dev/null || echo "Failed to stop test database container."
|
||||
docker remove $TEST_DB_CONTAINER > /dev/null || echo "Failed to clean up test database container."
|
||||
|
|
Reference in a new issue