Ensure that no records with duplicate key exist within an SQLAlchemy session before flushing

This commit is contained in:
Fabio Manganiello 2022-10-23 00:28:42 +02:00
parent 3e6ebdd23b
commit d7278857e5
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -228,13 +228,15 @@ class EntitiesEngine(Thread):
return existing_entity return existing_entity
new_entities = [] def entity_key(entity: Entity):
return ((entity.external_id or entity.name), entity.plugin)
new_entities = {}
entities_map = {} entities_map = {}
# Get the latest update for each ((id|name), plugin) record # Get the latest update for each ((id|name), plugin) record
for e in entities: for e in entities:
key = ((e.external_id or e.name), e.plugin) entities_map[entity_key(e)] = e
entities_map[key] = e
# Retrieve existing records and merge them # Retrieve existing records and merge them
for i, entity in enumerate(entities): for i, entity in enumerate(entities):
@ -242,9 +244,9 @@ class EntitiesEngine(Thread):
if existing_entity: if existing_entity:
entity = merge(entity, existing_entity) entity = merge(entity, existing_entity)
new_entities.append(entity) new_entities[entity_key(entity)] = entity
return new_entities return list(new_entities.values())
def _process_entities(self, *entities: Entity): def _process_entities(self, *entities: Entity):
with self._get_db().get_session() as session: with self._get_db().get_session() as session: