Skip to content

Commit

Permalink
Optimise updates in Expense and Destination table (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwin1111 authored Jun 16, 2021
1 parent 104e172 commit 25a70da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
41 changes: 26 additions & 15 deletions fyle_accounting_mappings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,18 @@ def bulk_create_or_update_expense_attributes(

existing_attributes = ExpenseAttribute.objects.filter(
value__in=attribute_value_list, attribute_type=attribute_type,
workspace_id=workspace_id).all()
workspace_id=workspace_id).values('id', 'value', 'detail')

existing_attribute_values = []

primary_key_map = {}

for existing_attribute in existing_attributes:
existing_attribute_values.append(existing_attribute.value)
primary_key_map[existing_attribute.value] = existing_attribute.id
existing_attribute_values.append(existing_attribute['value'])
primary_key_map[existing_attribute['value']] = {
'id': existing_attribute['id'],
'detail': existing_attribute['detail']
}

attributes_to_be_created = []
attributes_to_be_updated = []
Expand All @@ -189,11 +192,12 @@ def bulk_create_or_update_expense_attributes(
)
)
else:
if update:
if update and 'detail' in attribute and \
attribute['detail'] != primary_key_map[attribute['value']]['detail']:
attributes_to_be_updated.append(
ExpenseAttribute(
id=primary_key_map[attribute['value']],
detail=attribute['detail'] if 'detail' in attribute else None,
id=primary_key_map[attribute['value']]['id'],
detail=attribute['detail'],
)
)
if attributes_to_be_created:
Expand Down Expand Up @@ -263,15 +267,19 @@ def bulk_create_or_update_destination_attributes(

existing_attributes = DestinationAttribute.objects.filter(
destination_id__in=attribute_destination_id_list, attribute_type=attribute_type,
workspace_id=workspace_id).all()
workspace_id=workspace_id).values('id', 'value', 'destination_id', 'detail')

existing_attribute_destination_ids = []

primary_key_map = {}

for existing_attribute in existing_attributes:
existing_attribute_destination_ids.append(existing_attribute.destination_id)
primary_key_map[existing_attribute.destination_id] = existing_attribute.id
existing_attribute_destination_ids.append(existing_attribute['destination_id'])
primary_key_map[existing_attribute['destination_id']] = {
'id': existing_attribute['id'],
'value': existing_attribute['value'],
'detail': existing_attribute['detail']
}

attributes_to_be_created = []
attributes_to_be_updated = []
Expand All @@ -293,13 +301,16 @@ def bulk_create_or_update_destination_attributes(
)
else:
if update:
attributes_to_be_updated.append(
DestinationAttribute(
id=primary_key_map[attribute['destination_id']],
value=attribute['value'],
detail=attribute['detail'] if 'detail' in attribute else None,
if (attribute['value'] != primary_key_map[attribute['destination_id']]['value']) or \
('detail' in attribute and \
attribute['detail'] != primary_key_map[attribute['destination_id']]['detail']):
attributes_to_be_updated.append(
DestinationAttribute(
id=primary_key_map[attribute['destination_id']]['id'],
value=attribute['value'],
detail=attribute['detail'] if 'detail' in attribute else None,
)
)
)
if attributes_to_be_created:
DestinationAttribute.objects.bulk_create(attributes_to_be_created, batch_size=50)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name='fyle-accounting-mappings',
version='0.16.7',
version='0.17.0',
author='Shwetabh Kumar',
author_email='[email protected]',
description='Django application to store the fyle accounting mappings in a generic manner',
Expand Down

0 comments on commit 25a70da

Please sign in to comment.