Skip to content

Commit

Permalink
fix(docs): create initial data scripts page
Browse files Browse the repository at this point in the history
  • Loading branch information
fyliu authored and ethanstrominger committed Jul 1, 2024
1 parent f628d27 commit f991143
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
9 changes: 6 additions & 3 deletions app/data/migrations/0001_programarea_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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)]
9 changes: 6 additions & 3 deletions app/data/migrations/0002_practicearea_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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)]
9 changes: 6 additions & 3 deletions app/data/migrations/0003_sdg_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
(
Expand Down Expand Up @@ -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)]
20 changes: 12 additions & 8 deletions docs/how-to/create-initial-data-migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<name of last script, or contents of max_migration.txt>")]


operations = [migrations.RunPython(run, migrations.RunPython.noop)]
operations = [migrations.RunPython(forward, reverse)]
```

For example:
Expand All @@ -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"),
Expand All @@ -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/

0 comments on commit f991143

Please sign in to comment.