diff --git a/benefits/settings.py b/benefits/settings.py index 0d274c093..fbd9e7e01 100644 --- a/benefits/settings.py +++ b/benefits/settings.py @@ -2,6 +2,7 @@ Django settings for benefits project. """ import os + from benefits import sentry @@ -147,10 +148,11 @@ def _filter_empty(ls): WSGI_APPLICATION = "benefits.wsgi.application" +DATABASE_DIR = os.environ.get("DJANGO_DB_DIR", BASE_DIR) DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", - "NAME": "django.db", + "NAME": os.path.join(DATABASE_DIR, "django.db"), } } diff --git a/bin/init.sh b/bin/init.sh index 2fe3fdec9..5c802c5fa 100755 --- a/bin/init.sh +++ b/bin/init.sh @@ -2,9 +2,15 @@ set -eux # remove existing (old) database file -# -f forces the delete (and avoids an error when the file doesn't exist) -rm -f django.db +if [[ ${DJANGO_DB_RESET:-true} = true ]]; then + # construct the path to the database file from environment or default + DB_DIR="${DJANGO_DB_DIR:-.}" + DB_FILE="${DB_DIR}/django.db" + + # -f forces the delete (and avoids an error when the file doesn't exist) + rm -f "${DB_FILE}" +fi # run database migrations diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md index 80b7fac44..0392427d9 100644 --- a/docs/configuration/environment-variables.md +++ b/docs/configuration/environment-variables.md @@ -6,20 +6,6 @@ The sections below outline in more detail the application environment variables See other topic pages in this section for more specific environment variable configurations. -## Docker - -### `COMPOSE_PROJECT_NAME` - -!!! info "Local configuration" - - This setting only affects the app running on localhost - -!!! tldr "Docker docs" - - Read more at - -Name that Docker Compose prefixes to the project for container runs. - ## Amplitude !!! tldr "Amplitude API docs" @@ -57,6 +43,28 @@ Boolean: A list of strings representing the host/domain names that this Django site can serve. +### `DJANGO_DB_DIR` + +!!! warning "Deployment configuration" + + You may change this setting when deploying the app to a non-localhost domain + +The directory where Django creates its Sqlite database file. _Must exist and be +writable by the Django process._ + +By default, the base project directory (i.e. the root of the repository). + +### `DJANGO_DB_RESET` + +!!! warning "Deployment configuration" + + You may change this setting when deploying the app to a non-localhost domain + +Boolean: + +- `True` (default): deletes the existing database file and runs fresh Django migrations. +- `False`: Django uses the existing database file. + ### `DJANGO_DEBUG` !!! warning "Deployment configuration" diff --git a/terraform/app_service.tf b/terraform/app_service.tf index c1d775640..6441f1274 100644 --- a/terraform/app_service.tf +++ b/terraform/app_service.tf @@ -64,6 +64,8 @@ resource "azurerm_linux_web_app" "main" { # Django settings "DJANGO_ADMIN" = (local.is_prod || local.is_test) ? null : "${local.secret_prefix}django-admin)", "DJANGO_ALLOWED_HOSTS" = "${local.secret_prefix}django-allowed-hosts)", + "DJANGO_DB_DIR" = "${local.secret_prefix}django-db-dir)", + "DJANGO_DB_RESET" = "${local.secret_prefix}django-db-reset)", "DJANGO_DEBUG" = local.is_prod ? null : "${local.secret_prefix}django-debug)", "DJANGO_LOG_LEVEL" = "${local.secret_prefix}django-log-level)",