Skip to content

Commit

Permalink
Add backfill_changesets command (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
willemarcel authored Feb 27, 2024
1 parent e3a0ea4 commit 6c6c36c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
34 changes: 34 additions & 0 deletions osmchadjango/changeset/management/commands/backfill_changesets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from datetime import date

from django.core.management.base import BaseCommand

from ...models import Changeset
from ...tasks import create_changeset


class Command(BaseCommand):
help = """Backfill missing changesets in a date range.
Start and end dates should be in YYYY-MM-DD format."""

def add_arguments(self, parser):
parser.add_argument("start_date", nargs=1, type=str)
parser.add_argument("end_date", nargs=1, type=str)

def handle(self, *args, **options):
try:
end_date = date.fromisoformat(options["end_date"][0])
except (ValueError, TypeError):
end_date = date.today()

cl = Changeset.objects.filter(
date__gte=date.fromisoformat(options["start_date"][0]),
date__lte=end_date
).values_list('id')
cl = [c[0] for c in cl]

id = cl[len(cl) - 1]
final = cl[0]
while id < final:
if id not in cl:
create_changeset(id)
id = id + 1
14 changes: 14 additions & 0 deletions osmchadjango/changeset/tests/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,17 @@ def test_error(self):
)
self.assertIn('Verify the SuspicionReasons ids.', self.out.getvalue())
self.assertIn('One or both of them does not exist.', self.out.getvalue())


class TestBackfillChangesets(TestCase):
def setUp(self):
ChangesetFactory(id='1234', date=datetime(2021,1,2))
ChangesetFactory(id='1238', date=datetime(2021,1,3))

def test_backfill(self):
call_command("backfill_changesets", '2021-01-01', '2021-01-04')
cl = [i[0] for i in Changeset.objects.all().values_list('id')]
self.assertEqual(len(cl), 5)
self.assertIn(1235, cl)
self.assertIn(1236, cl)
self.assertIn(1237, cl)

0 comments on commit 6c6c36c

Please sign in to comment.