Skip to content

Commit

Permalink
create repeat register if Instance has no repeats
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Jan 8, 2025
1 parent 6d6fae2 commit b763543
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
4 changes: 3 additions & 1 deletion onadata/libs/tests/utils/test_logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,9 @@ def test_no_repeats(self):
register_instance_export_repeats(instance)

exists = MetaData.objects.filter(data_type="export_repeat_columns").exists()
self.assertFalse(exists)
self.assertTrue(exists)
metadata = MetaData.objects.get(data_type="export_repeat_columns")
self.assertEqual(metadata.extra_data, {})

def test_create_register_previous_candidates(self):
"""Previous submissions are considered when creating repeat register"""
Expand Down
25 changes: 9 additions & 16 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,10 +1550,13 @@ def _get_repeat_max(data):
return repeat_max


def _update_export_repeats(
incoming_repeats: dict[str, int], metadata: MetaData
) -> None:
for repeat, incoming_max in incoming_repeats.items():
def _update_export_repeats(instance: Instance, metadata: MetaData) -> None:
repeat_counts = _get_instance_repeat_max(instance)

if not repeat_counts:
return

for repeat, incoming_max in repeat_counts.items():
# Get the maximum between incoming max and the current max
# Done at database level to gurantee atomicity and
# consistency. Avoids race conditions if it were done at the
Expand Down Expand Up @@ -1602,18 +1605,13 @@ def register_instance_export_repeats(instance: Instance) -> None:
:param instance: Instance object
"""
repeat_counts = _get_instance_repeat_max(instance)

if not repeat_counts:
return

metadata, created = _get_repeat_register(instance.xform)

if created:
register_xform_export_repeats(instance.xform)

else:
_update_export_repeats(repeat_counts, metadata)
_update_export_repeats(instance, metadata)


@transaction.atomic()
Expand All @@ -1625,10 +1623,5 @@ def register_xform_export_repeats(xform: XForm) -> None:
instance_qs = xform.instances.filter(deleted_at__isnull=True)

for instance in queryset_iterator(instance_qs):
repeat_counts = _get_instance_repeat_max(instance)

if not repeat_counts:
continue

metadata, _ = _get_repeat_register(xform)
_update_export_repeats(repeat_counts, metadata)
_update_export_repeats(instance, metadata)

0 comments on commit b763543

Please sign in to comment.