Skip to content

Commit

Permalink
feat: Add a --sync flag to the loaddivisions management command
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed May 14, 2024
1 parent b008370 commit 3e6dc00
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

* Add a --sync flag to the loaddivisions management command, to delete divisions that are in the DB but not the CSV, even if the DB contains the CSV. This flag is relevant if you synchronize with a single CSV.

## 3.3.0 (2023-05-08)

* Add last_seen field to database objects
Expand Down
14 changes: 10 additions & 4 deletions opencivicdata/core/management/commands/loaddivisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def to_db(fd):
return Division(id=fd.id, name=fd.name, **args)


def load_divisions(country, bulk=False):
def load_divisions(country, bulk=False, sync=False):
existing_divisions = Division.objects.filter(country=country)

country_division = FileDivision.get("ocd-division/country:{}".format(country))
Expand All @@ -35,7 +35,7 @@ def load_divisions(country, bulk=False):

if objects_set == existing_divisions_set:
print("The CSV and the DB contents are exactly the same; no work to be done!")
elif objects_set.issubset(existing_divisions_set):
elif not sync and objects_set.issubset(existing_divisions_set):
print("The DB contains all CSV contents; no work to be done!")
else:
if bulk:
Expand Down Expand Up @@ -65,9 +65,15 @@ def add_arguments(self, parser):
parser.add_argument(
"--bulk",
action="store_true",
help="Use bulk_create to add divisions. *Warning* This deletes any existing divisions",
help="Use bulk_create to add divisions. *Warning* This deletes any existing divisions.",
)
parser.add_argument(
"--sync",
action="store_true",
help="Add divisions from a CSV file, and delete existing divisions that are not in the "
"CSV file. This option only makes sense with a single country.",
)

def handle(self, *args, **options):
for country in options["countries"]:
load_divisions(country, options["bulk"])
load_divisions(country, options["bulk"], options["sync"])

0 comments on commit 3e6dc00

Please sign in to comment.