From f9911434d2c60d063940854278a57cb3141402dc Mon Sep 17 00:00:00 2001 From: Fang Yi Liu Date: Mon, 17 Jun 2024 10:57:19 -0700 Subject: [PATCH] fix(docs): create initial data scripts page --- app/data/migrations/0001_programarea_seed.py | 9 ++++++--- app/data/migrations/0002_practicearea_seed.py | 9 ++++++--- app/data/migrations/0003_sdg_seed.py | 9 ++++++--- docs/how-to/create-initial-data-migrations.md | 20 +++++++++++-------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/app/data/migrations/0001_programarea_seed.py b/app/data/migrations/0001_programarea_seed.py index a240c835..06ef76ff 100644 --- a/app/data/migrations/0001_programarea_seed.py +++ b/app/data/migrations/0001_programarea_seed.py @@ -3,7 +3,7 @@ from core.models import ProgramArea -def run(__code__, __reverse_code__): +def forward(__code__, __reverse_code__): items = [ (1, "Citizen Engagement"), (2, "Civic Tech Infrastructure"), @@ -19,8 +19,11 @@ def run(__code__, __reverse_code__): ProgramArea.objects.create(uuid=uuid, name=name) +def reverse(__code__, __reverse_code__): + ProgramArea.objects.all().delete() + + class Migration(migrations.Migration): - initial = True dependencies = [("core", "0018_rename_recurringevent_event")] - operations = [migrations.RunPython(run, migrations.RunPython.noop)] + operations = [migrations.RunPython(forward, reverse)] diff --git a/app/data/migrations/0002_practicearea_seed.py b/app/data/migrations/0002_practicearea_seed.py index 7439da02..00e65465 100644 --- a/app/data/migrations/0002_practicearea_seed.py +++ b/app/data/migrations/0002_practicearea_seed.py @@ -2,7 +2,7 @@ from core.models import PracticeArea -def run(__code__, __reverse_code__): +def forward(__code__, __reverse_code__): items = [ (1, "Development"), (2, "Project Management"), @@ -13,8 +13,11 @@ def run(__code__, __reverse_code__): PracticeArea.objects.create(uuid=uuid, name=name) +def reverse(__code__, __reverse_code__): + PracticeArea.objects.all().delete() + + class Migration(migrations.Migration): - initial = True dependencies = [("data", "0001_programarea_seed")] - operations = [migrations.RunPython(run, migrations.RunPython.noop)] + operations = [migrations.RunPython(forward, reverse)] diff --git a/app/data/migrations/0003_sdg_seed.py b/app/data/migrations/0003_sdg_seed.py index 856d05f7..c3cf9b1c 100644 --- a/app/data/migrations/0003_sdg_seed.py +++ b/app/data/migrations/0003_sdg_seed.py @@ -3,7 +3,7 @@ from core.models import Sdg -def run(__code__, __reverse_code__): +def forward(__code__, __reverse_code__): items = [ (1, "No Poverty", "End poverty in all its forms everywhere", "sdg01.png"), ( @@ -107,8 +107,11 @@ def run(__code__, __reverse_code__): Sdg.objects.create(uuid=uuid, name=name, description=description, image=image) +def reverse(__code__, __reverse_code__): + Sdg.objects.all().delete() + + class Migration(migrations.Migration): - initial = True dependencies = [("data", "0002_practicearea_seed")] - operations = [migrations.RunPython(run, migrations.RunPython.noop)] + operations = [migrations.RunPython(forward, reverse)] diff --git a/docs/how-to/create-initial-data-migrations.md b/docs/how-to/create-initial-data-migrations.md index dcbccb15..91957d5c 100644 --- a/docs/how-to/create-initial-data-migrations.md +++ b/docs/how-to/create-initial-data-migrations.md @@ -89,18 +89,20 @@ It is required that there be data in the first column of the sheet. from core.models import ModelNameInPascalCase - def run(__code__, __reverse_code__): + def forward(__code__, __reverse_code__): # paste everything in seed script's run function here # remove pass below pass + def reverse(__code__, __reverse_code__): + ModelNameInPascalCase.objects.all().delete() + + class Migration(migrations.Migration): - initial = True dependencies = [("data", "")] - - operations = [migrations.RunPython(run, migrations.RunPython.noop)] + operations = [migrations.RunPython(forward, reverse)] ``` For example: @@ -111,7 +113,7 @@ It is required that there be data in the first column of the sheet. from core.models import BookType - def run(__code__, __reverse_code__): + def forward(__code__, __reverse_code__): items = [ (1, "Hard Cover"), (2, "Soft Cover"), @@ -120,12 +122,14 @@ It is required that there be data in the first column of the sheet. BookType.objects.create(uuid=uuid, name=name) + def reverse(__code__, __reverse_code__): + BookType.objects.all().delete() + + class Migration(migrations.Migration): - initial = True dependencies = [("data", "0011_author_seed")] - - operations = [migrations.RunPython(run, migrations.RunPython.noop)] + operations = [migrations.RunPython(forward, reverse)] ``` [pd-data-spreadsheet]: https://docs.google.com/spreadsheets/d/1x_zZ8JLS2hO-zG0jUocOJmX16jh-DF5dccrd_OEGNZ0/