Create initial data scripts¶
Overview¶
The goal is to convert our initial data into scripts that can be loaded into the database when the backend is set up for the first time.
These are the steps:
- Export the data into a csv file
- Generate a python script from the csv data
Prerequisites¶
You must be able to run the project locally via docker or otherwise. This guide assumes you're running it from docker-compose via the buildrun.sh
script.
The initial data exists in a Google spreadsheet, such as this one for People Depot. There should be individual sheets named after the model names the data correspond to, such as ProgramArea - Data
. The sheet name is useful for us to identify the model it corresponds to.
The sheet should be formatted like so:
- the first row contains the names of the field names in the model. The names must be exactly the same
- rows 2 to n are the initial data for the model we want to turn into a script.
It is required that there be data in the first column of the sheet.
Gather data for preparation¶
-
Export the data from the Google spreadsheet
- Find the sheet in the document containing the data to export. Let's use the
ProgramArea - Data
data as our example. - Go to File -> Download -> Comma Separated Values (.csv). This will download the sheet as a .csv file.
- Copy the file to the app/core/initial_data directory.
- Find the sheet in the document containing the data to export. Let's use the
Convert data into Python script¶
-
Start Docker
-
From project root, run
-
Go to the project root and run this command
-
Check that there's a new file called
app/core/scripts/programarea_seed.py
and that it looks correct-
You can run it to verify, but will need to remove that data if you care about restoring your local database state
-
Run this command to run the script
- To remove the data, go into the database and delete all rows from
core_programarea
-
Combine Script in Migration¶
-
Look for name of the last migration file in
app/data/migrations
directory -
Create a script in the same directory named
<number>_<modelnameinlowercase>_seed.py
with the following contents and replaceModelNameInPascalCase
and<name of last script>
with appropriate values:from django.db import migrations from core.models import ModelNameInPascalCase def run(__code__, __reverse_code__): # paste everything in seed script's run function here # remove pass below pass class Migration(migrations.Migration): initial = True dependencies = [("data", "<name of last script>")] operations = [migrations.RunPython(run, migrations.RunPython.noop)]
For example:
from django.db import migrations from core.models import BookType def run(__code__, __reverse_code__): items = [ (1, "Hard Cover"), (2, "Soft Cover"), ] for uuid, name in items: BookType.objects.create(uuid=uuid, name=name) class Migration(migrations.Migration): initial = True dependencies = [("data", "0011_author_seed")] operations = [migrations.RunPython(run, migrations.RunPython.noop)]