Skip to content

Commit

Permalink
psycopg3 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
benthuffine committed Jan 16, 2024
1 parent a92a635 commit b832f79
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
60 changes: 45 additions & 15 deletions entity/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,25 @@ def upsert_entity_kinds(self, entity_kinds):
# Filter out unchanged entity kinds
unchanged_entity_kinds = {}
if entity_kinds:
sql_where = (
'(name, display_name) IN ('
'' + ','.join(['(%s, %s)' for _ in entity_kinds]) + ''
')'
)

sql_params = list(sum(
list(
(entity_kind.name, entity_kind.display_name)
for entity_kind in entity_kinds
),
()
))

unchanged_entity_kinds = {
(entity_kind.name, entity_kind.display_name): entity_kind
for entity_kind in EntityKind.all_objects.extra(
where=['(name, display_name) IN %s'],
params=[tuple(
(entity_kind.name, entity_kind.display_name)
for entity_kind in entity_kinds
)]
where=[sql_where],
params=sql_params
)
}

Expand Down Expand Up @@ -455,16 +466,24 @@ def upsert_entities(self, entities, sync=False):
if not sync:
select_for_update_query = (
'SELECT FROM {table_name} '
'WHERE (entity_type_id, entity_id) IN %s '
'WHERE (entity_type_id, entity_id) = ANY(ARRAY[{tuples}]) '
'ORDER BY id ASC '
'FOR NO KEY UPDATE'
).format(
table_name=Entity._meta.db_table
table_name=Entity._meta.db_table,
tuples=','.join(
['(%s, %s)' for _ in entities]
)
)
select_for_update_query_params = [tuple(
(entity.entity_type_id, entity.entity_id)
for entity in entities
)]

# Flatten a list of entity_type_id, entity_id tuples for our param replacements
select_for_update_query_params = list(sum(
list(
(entity.entity_type_id, entity.entity_id)
for entity in entities
),
()
))

# Select the items for update
with connection.cursor() as cursor:
Expand All @@ -475,12 +494,23 @@ def upsert_entities(self, entities, sync=False):
# entities that were activated or deactivated
initial_queryset = Entity.all_objects.all()
if not sync:
initial_queryset = Entity.all_objects.extra(
where=['(entity_type_id, entity_id) IN %s'],
params=[tuple(
sql_where = (
'(entity_type_id, entity_id) IN ('
'' + ','.join(['(%s, %s)' for _ in entities]) + ''
')'
)

sql_params = list(sum(
list(
(entity.entity_type_id, entity.entity_id)
for entity in entities
)]
),
()
))

initial_queryset = Entity.all_objects.extra(
where=[sql_where],
params=sql_params
)
initial_entity_activation_state = {
entity[0]: entity[1]
Expand Down
4 changes: 4 additions & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from optparse import OptionParser
from settings import configure_settings

# These lines allow nose tests to work in Python 3.10
import collections.abc
collections.Callable = collections.abc.Callable

# Configure the default settings and setup django
configure_settings()

Expand Down

0 comments on commit b832f79

Please sign in to comment.