Skip to content

Commit 61c9c77

Browse files
committed
feat: add support for copying over additional data migrations
add docs for new env var and for how to load new data for different environments.
1 parent fd83391 commit 61c9c77

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

bin/init.sh

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ rm -f django.db
88

99
# run database migrations
1010

11+
if [[ ${DJANGO_LOAD_SAMPLE_DATA:-true} = false ]]; then
12+
if [[ -d ${DJANGO_MIGRATIONS_DIR} ]]; then
13+
cp ${DJANGO_MIGRATIONS_DIR}/0002_*.py ./benefits/core/migrations/
14+
else
15+
echo "Warning: DJANGO_MIGRATION_DIR needs to be a directory... not loading any data"
16+
fi
17+
fi
18+
1119
python manage.py migrate
1220

1321
# create a superuser account for backend admin access

docs/configuration/data.md

+29
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,35 @@ Run these commands from within the repository root, inside the devcontainer:
6868
bin/init.sh
6969
```
7070

71+
## Loading new data for different environment
72+
73+
Django will run all migration files found in an app's `migrations` module.
74+
75+
To load new data for a different environment:
76+
77+
1. (Optional) Set an environment variable `DJANGO_LOAD_SAMPLE_DATA` to `false` if you don't want the `core` app's sample data to be loaded.
78+
1. Create a data migration file, and make sure the name is prefixed with `0002`. (The migration process for Benefits expects data migration files to named as such). The basic structure for the contents of this file is:
79+
```python
80+
from django.db import migrations
81+
82+
83+
def load_data(app, *args, **kwargs):
84+
pass
85+
86+
87+
class Migration(migrations.Migration):
88+
dependencies = [
89+
("core", "0001_initial"),
90+
]
91+
92+
operations = [
93+
migrations.RunPython(load_data),
94+
]
95+
```
96+
1. Put this file in the core app's `migrations` module
97+
1. If `DJANGO_LOAD_SAMPLE_DATA` is `false`, you can also set [`DJANGO_MIGRATIONS_DIR`](../environment-variables/#django_migrations_dir) to a directory path, and put your data migration there.
98+
99+
71100
[core-models]: https://github.com/cal-itp/benefits/blob/dev/benefits/core/models.py
72101
[django-load-initial-data]: https://docs.djangoproject.com/en/4.0/howto/initial-data/
73102
[eligibility-server]: https://docs.calitp.org/eligibility-server

docs/configuration/environment-variables.md

+9
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ The log level used in the application's logging configuration.
113113

114114
By default the application sends logs to `stdout`.
115115

116+
117+
### `DJANGO_MIGRATIONS_DIR`
118+
119+
!!! warning "Deployment configuration"
120+
121+
You may change this setting when deploying the app to a non-localhost domain
122+
123+
If [`DJANGO_LOAD_SAMPLE_DATA`](#django_load_sample_data) is `false`, then you can set `DJANGO_MIGRATIONS_DIR` to the path of a directory containing data migrations that you want to be run. Those data migration files need to be prefixed with `0002` so that the [helper migration file](data.md)) can find it. See [Configuration data](./data.md) for more on loading data for different environments.
124+
116125
### `DJANGO_SECRET_KEY`
117126

118127
!!! warning "Deployment configuration"

0 commit comments

Comments
 (0)