Browse Source

Always use DATABASE_URL variable if set

This is useful in dev, not just production
Jake Howard 2 years ago
parent
commit
96d10e087e

+ 11 - 5
bakerydemo/settings/base.py

@@ -10,6 +10,7 @@ For the full list of settings and their values, see
 https://docs.djangoproject.com/en/3.2/ref/settings/
 """
 
+import dj_database_url
 import os
 
 # Build paths inside the project like this: os.path.join(PROJECT_DIR, ...)
@@ -115,12 +116,17 @@ WSGI_APPLICATION = "bakerydemo.wsgi.application"
 # Database
 # https://docs.djangoproject.com/en/3.2/ref/settings/#databases
 
-DATABASES = {
-    "default": {
-        "ENGINE": "django.db.backends.sqlite3",
-        "NAME": os.path.join(BASE_DIR, "bakerydemodb"),
+if "DATABASE_URL" in os.environ:
+    DATABASES = {
+        "default": dj_database_url.config(conn_max_age=500)
+    }
+else:
+    DATABASES = {
+        "default": {
+            "ENGINE": "django.db.backends.sqlite3",
+            "NAME": os.path.join(BASE_DIR, "bakerydemodb"),
+        }
     }
-}
 
 
 # Password validation

+ 0 - 5
bakerydemo/settings/production.py

@@ -2,8 +2,6 @@ import os
 import random
 import string
 
-import dj_database_url
-
 from .base import *  # noqa: F403
 
 DEBUG = False
@@ -35,9 +33,6 @@ EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
 if "PRIMARY_HOST" in os.environ:
     WAGTAILADMIN_BASE_URL = "https://{}".format(os.environ["PRIMARY_HOST"])
 
-db_from_env = dj_database_url.config(conn_max_age=500)
-DATABASES["default"].update(db_from_env)
-
 # AWS creds may be used for S3 and/or Elasticsearch
 AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID", "")
 AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY", "")

+ 1 - 0
requirements/base.txt

@@ -4,3 +4,4 @@ wagtail>=4.1,<5
 wagtail-font-awesome-svg>=0.0.3,<1
 django-debug-toolbar>=3.2,<4
 django-extensions==3.2.1
+dj-database-url==0.4.1

+ 0 - 1
requirements/production.txt

@@ -1,7 +1,6 @@
 -r base.txt
 elasticsearch==5.5.3
 # Additional dependencies for Heroku, AWS, and Google Cloud deployment
-dj-database-url==0.4.1
 uwsgi>=2.0.17,<2.1
 psycopg2>=2.7,<3.0
 whitenoise>=5.0,<5.1