Explictly cast entity IDs to strings when coalescing entity updates

Some plugins may represent entity IDs as integers, while the database
maps external IDs to strings. This may result in entities being
incorrectly mapped during merging. Casting to string prevents these
type-related ambiguities.
This commit is contained in:
Fabio Manganiello 2022-04-29 23:24:28 +02:00
parent 975991ba69
commit f45df5d4d3
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -172,7 +172,12 @@ class EntitiesEngine(Thread):
self, session: Session, entities: Iterable[Entity] self, session: Session, entities: Iterable[Entity]
) -> Iterable[Entity]: ) -> Iterable[Entity]:
existing_entities = { existing_entities = {
(entity.external_id or entity.name, entity.plugin): entity (
str(entity.external_id)
if entity.external_id is not None
else entity.name,
entity.plugin,
): entity
for entity in session.query(Entity) for entity in session.query(Entity)
.filter( .filter(
or_( or_(
@ -194,7 +199,13 @@ class EntitiesEngine(Thread):
return [ return [
existing_entities.get( existing_entities.get(
(entity.external_id or entity.name, entity.plugin), None (
str(entity.external_id)
if entity.external_id is not None
else entity.name,
entity.plugin,
),
None,
) )
for entity in entities for entity in entities
] ]
@ -207,8 +218,8 @@ class EntitiesEngine(Thread):
for col in columns: for col in columns:
if col == 'meta': if col == 'meta':
existing_entity.meta = { # type: ignore existing_entity.meta = { # type: ignore
**(existing_entity.meta or {}), **(existing_entity.meta or {}), # type: ignore
**(entity.meta or {}), **(entity.meta or {}), # type: ignore
} }
elif col not in ('id', 'created_at'): elif col not in ('id', 'created_at'):
setattr(existing_entity, col, getattr(entity, col)) setattr(existing_entity, col, getattr(entity, col))