|
4 | 4 |
|
5 | 5 | [`benefits/core/migrations/0002_sample_data.py`][data-sample]
|
6 | 6 |
|
| 7 | +!!! example "Helper migration file to define data migration order" |
| 8 | + |
| 9 | + [`benefits/core/migrations/0003_data_migration_order.py`][helper-migration] |
| 10 | + |
| 11 | + Since our initial data migration files are assumed to start with `0002` and depend only on `0001_initial`, this migration file ensures the migration graph has a defined order (which is required for the migration to run). It looks in the `benefits/core/migrations` directory for migration files starting with `0002` and declares them in its dependencies list. |
| 12 | + |
7 | 13 | !!! tldr "Django docs"
|
8 | 14 |
|
9 | 15 | [How to provide initial data for models][django-load-initial-data]
|
@@ -62,8 +68,38 @@ Run these commands from within the repository root, inside the devcontainer:
|
62 | 68 | bin/init.sh
|
63 | 69 | ```
|
64 | 70 |
|
| 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 under `benefits/core/migrations` |
| 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 | + |
65 | 100 | [core-models]: https://github.com/cal-itp/benefits/blob/dev/benefits/core/models.py
|
66 | 101 | [django-load-initial-data]: https://docs.djangoproject.com/en/4.0/howto/initial-data/
|
67 | 102 | [eligibility-server]: https://docs.calitp.org/eligibility-server
|
68 | 103 | [data-sample]: https://github.com/cal-itp/benefits/tree/dev/benefits/core/migrations/0002_sample_data.py
|
| 104 | +[helper-migration]: https://github.com/cal-itp/benefits/tree/dev/benefits/core/migrations/0003_data_migration_order.py |
69 | 105 | [init]: https://github.com/cal-itp/benefits/blob/dev/bin/init.sh
|
0 commit comments