Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: parameterize Django database path/deletion #1770

Merged
merged 6 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion benefits/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Django settings for benefits project.
"""
import os

from benefits import sentry


Expand Down Expand Up @@ -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"),
}
}

Expand Down
10 changes: 8 additions & 2 deletions bin/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 22 additions & 14 deletions docs/configuration/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://docs.docker.com/compose/reference/envvars/#compose_project_name>

Name that Docker Compose prefixes to the project for container runs.

## Amplitude

!!! tldr "Amplitude API docs"
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions terraform/app_service.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)",

Expand Down
Loading