Skip to content

Commit ea36375

Browse files
authored
Feat: use PostgreSQL for the database backend (#131)
2 parents a707370 + 721edc3 commit ea36375

File tree

25 files changed

+4094
-89
lines changed

25 files changed

+4094
-89
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"name": "caltrans/pems",
33
"dockerComposeFile": ["../compose.yml"],
44
"service": "dev",
5+
"runServices": ["dev", "pgweb"],
56
"forwardPorts": ["docs:8000"],
67
"workspaceFolder": "/caltrans/app",
7-
"postStartCommand": ["/bin/bash", "bin/reset_db.sh"],
8+
"postStartCommand": ["/bin/bash", "bin/setup.sh"],
89
"postAttachCommand": ["/bin/bash", ".devcontainer/postAttach.sh"],
910
"customizations": {
1011
"vscode": {

.env.sample

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@ DJANGO_SUPERUSER_USERNAME=pems-admin
33
DJANGO_SUPERUSER_EMAIL=pems-admin@compiler.la
44
DJANGO_SUPERUSER_PASSWORD=superuser12345!
55

6-
# Django storage
6+
# Django Database settings
77
DJANGO_DB_RESET=true
8-
DJANGO_STORAGE_DIR=.
9-
DJANGO_DB_FILE=django.db
8+
DJANGO_DB_NAME=django
9+
DJANGO_DB_USER=django
10+
DJANGO_DB_PASSWORD=django_password
1011
DJANGO_DB_FIXTURES="pems/local_fixtures.json"
1112

13+
# PostgreSQL settings
14+
POSTGRES_HOSTNAME=postgres
15+
POSTGRES_DB=postgres
16+
POSTGRES_USER=postgres
17+
POSTGRES_PASSWORD=postgres_password
18+
POSTGRES_PORT=5432
19+
POSTGRES_SSLMODE=disable
20+
21+
PGWEB_PORT=8081
22+
1223
# Streamlit
1324
STREAMLIT_LOCAL_PORT=8501
1425
# options: hidden, sidebar

.github/workflows/tests-pytest.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ jobs:
1717
- name: Check out code
1818
uses: actions/checkout@v4
1919

20-
- name: Install system packages
21-
run: |
22-
sudo apt-get update -y
23-
sudo apt-get install -y gettext
24-
2520
- uses: actions/setup-python@v5
2621
with:
2722
python-version-file: .github/workflows/.python-version
@@ -33,9 +28,6 @@ jobs:
3328
pip install -e .[test]
3429
pip install -r streamlit_app/requirements.txt
3530
36-
- name: Run setup
37-
run: ./bin/init.sh
38-
3931
- name: Run tests
4032
run: ./tests/pytest/run.sh
4133

appcontainer/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ ENV GUNICORN_CONF "/$USER/run/gunicorn.conf.py"
9191
# overwrite default nginx.conf
9292
COPY appcontainer/nginx.conf /etc/nginx/nginx.conf
9393

94+
# copy certs for PostgreSQL verify-full
95+
COPY appcontainer/certs/aws_global_postgres_ca_bundle.pem app/certs/aws_global_postgres_ca_bundle.pem
96+
9497
WORKDIR /$USER/app
9598

9699
# copy runtime files

appcontainer/certs/aws_global_postgres_ca_bundle.pem

Lines changed: 2660 additions & 0 deletions
Large diffs are not rendered by default.

bin/init.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.

bin/reset_db.sh

Lines changed: 0 additions & 33 deletions
This file was deleted.

bin/setup.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
set -ex
3+
4+
# Ensure databases, users, migrations, and superuser are set up
5+
should_reset=${REMOTE_CONTAINERS:-false}
6+
if [[ $should_reset == "true" ]]; then
7+
# running in a devcontainer, reset the DB
8+
python manage.py ensure_db --reset
9+
else
10+
python manage.py ensure_db
11+
fi
12+
13+
# Load data fixtures (if any)
14+
valid_fixtures=$(echo "$DJANGO_DB_FIXTURES" | grep -e fixtures\.json$ || test $? = 1)
15+
16+
if [[ -n "$valid_fixtures" ]]; then
17+
python manage.py loaddata $DJANGO_DB_FIXTURES
18+
else
19+
echo "No JSON fixtures to load"
20+
fi

bin/start.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env bash
22
set -eu
33

4-
# initialize Django
4+
# prepare static files
55

6-
bin/init.sh
6+
python manage.py collectstatic --no-input
77

88
# start the web server
99

bin/start_aws.sh

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
11
#!/usr/bin/env bash
22
set -eu
33

4-
#
54
# S3 bucket name is injected by Copilot as an environment variable
65
# since it was created via copilot storage init --name pems-db, the variable is 'PEMSDB_NAME'
76
S3_BUCKET_NAME="$PEMSDB_NAME"
87
S3_FIXTURE_PATH="fixtures.json"
9-
LOCAL_FIXTURE_PATH="fixtures.json"
108

119
echo "Downloading $S3_FIXTURE_PATH from bucket $S3_BUCKET_NAME"
12-
aws s3 cp "s3://${S3_BUCKET_NAME}/${S3_FIXTURE_PATH}" "${LOCAL_FIXTURE_PATH}"
10+
aws s3 cp "s3://${S3_BUCKET_NAME}/${S3_FIXTURE_PATH}" "${DJANGO_DB_FIXTURES}"
1311
echo "Download complete"
1412

15-
# initialize Django
16-
17-
bin/init.sh
18-
19-
# effectively reset database by loading downloaded fixtures into the database
20-
echo "Loading data from ${LOCAL_FIXTURE_PATH}"
21-
python manage.py loaddata "${LOCAL_FIXTURE_PATH}"
22-
echo "Data loading complete"
23-
24-
# start the web server
25-
26-
nginx
27-
28-
# start the application server
29-
30-
python -m gunicorn -c $GUNICORN_CONF pems.wsgi
13+
bin/setup.sh
14+
bin/start.sh

0 commit comments

Comments
 (0)