Skip to content

Commit 165435b

Browse files
committed
change to using postgres for our DB [appendix_django_ends]
1 parent 813e3f9 commit 165435b

5 files changed

Lines changed: 35 additions & 12 deletions

File tree

‎Dockerfile‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ COPY tests/ /tests/
1212

1313
WORKDIR /src
1414
ENV DJANGO_SETTINGS_MODULE=djangoproject.django_project.settings
15-
RUN python /src/djangoproject/manage.py migrate
16-
CMD python /src/djangoproject/manage.py runserver 0.0.0.0:80
15+
CMD python /tests/wait_for_postgres.py && \
16+
python /src/djangoproject/manage.py migrate && \
17+
python /src/djangoproject/manage.py runserver 0.0.0.0:80

‎requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# app
22
psycopg2-binary
33
django
4+
dj-database-url
45

56
# tests
67
pytest

‎src/djangoproject/django_project/settings.py‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"""
1212

1313
import os
14+
import dj_database_url
15+
from allocation import config
1416

1517
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
1618
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -73,14 +75,7 @@
7375

7476
# Database
7577
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
76-
77-
DATABASES = {
78-
"default": {
79-
"ENGINE": "django.db.backends.sqlite3",
80-
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
81-
}
82-
}
83-
78+
DATABASES = {"default": dj_database_url.parse(config.get_postgres_uri())}
8479

8580
# Password validation
8681
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

‎tests/conftest.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# pylint: disable=redefined-outer-name
22
import time
33
from pathlib import Path
4+
45
import pytest
56
import requests
6-
from requests.exceptions import ConnectionError
7+
8+
from requests.exceptions import RequestException
9+
710
from allocation import config
11+
import wait_for_postgres
812

913

1014
def wait_for_webapp_to_come_up():
@@ -13,7 +17,7 @@ def wait_for_webapp_to_come_up():
1317
while time.time() < deadline:
1418
try:
1519
return requests.get(url)
16-
except ConnectionError:
20+
except RequestException:
1721
time.sleep(0.5)
1822
pytest.fail("API never came up")
1923

@@ -23,3 +27,8 @@ def restart_api():
2327
(Path(__file__).parent / "../src/djangoproject/manage.py").touch()
2428
time.sleep(0.5)
2529
wait_for_webapp_to_come_up()
30+
31+
32+
@pytest.fixture(autouse=True, scope="session")
33+
def wait_for_postgres_to_come_up():
34+
wait_for_postgres.wait_for_postgres_to_come_up()

‎tests/wait_for_postgres.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import time
2+
import psycopg2
3+
from allocation import config
4+
5+
6+
def wait_for_postgres_to_come_up():
7+
deadline = time.time() + 10
8+
while time.time() < deadline:
9+
try:
10+
return psycopg2.connect(config.get_postgres_uri())
11+
except psycopg2.OperationalError:
12+
time.sleep(0.5)
13+
raise Exception("Postgres never came up")
14+
15+
16+
if __name__ == "__main__":
17+
wait_for_postgres_to_come_up()

0 commit comments

Comments
 (0)