Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit acc8ce7

Browse files
authored
Merge pull request #161 from jaredlewis/develop
Add ordering to select for updates
2 parents 9f4db1e + f5e5132 commit acc8ce7

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

entity/sync.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def upsert_entity_kinds(self, entity_kinds):
384384
# Select all our existing entity kinds for update so we can do proper locking
385385
# We have to select all here for some odd reason, if we only select the ones
386386
# we are syncing we still run into deadlock issues
387-
list(EntityKind.all_objects.all().select_for_update().values_list('id', flat=True))
387+
list(EntityKind.all_objects.all().order_by('id').select_for_update().values_list('id', flat=True))
388388

389389
# Upsert the entity kinds
390390
upserted_enitity_kinds = manager_utils.bulk_upsert(
@@ -412,7 +412,7 @@ def upsert_entities(self, entities, sync=False):
412412
if entities:
413413
# Default select for update query when syncing all
414414
select_for_update_query = (
415-
'SELECT FROM {table_name} FOR NO KEY UPDATE'
415+
'SELECT FROM {table_name} ORDER BY id ASC FOR NO KEY UPDATE'
416416
).format(
417417
table_name=Entity._meta.db_table
418418
)
@@ -421,7 +421,10 @@ def upsert_entities(self, entities, sync=False):
421421
# If we are not syncing all, only select those we are updating
422422
if not sync:
423423
select_for_update_query = (
424-
'SELECT FROM {table_name} WHERE (entity_type_id, entity_id) IN %s FOR NO KEY UPDATE'
424+
'SELECT FROM {table_name} '
425+
'WHERE (entity_type_id, entity_id) IN %s '
426+
'ORDER BY id ASC '
427+
'FOR NO KEY UPDATE'
425428
).format(
426429
table_name=Entity._meta.db_table
427430
)
@@ -506,7 +509,7 @@ def upsert_entity_relationships(self, queryset, entity_relationships):
506509

507510
# Select the relationships for update
508511
if entity_relationships:
509-
list(queryset.select_for_update().values_list(
512+
list(queryset.order_by('id').select_for_update().values_list(
510513
'id',
511514
flat=True
512515
))

0 commit comments

Comments
 (0)