Fix migration with negative event type cache (#91910)

* Fix migration with negative event type cache

fixes a regression with #91770

* Update homeassistant/components/recorder/table_managers/event_types.py
This commit is contained in:
J. Nick Koston 2023-04-23 19:56:17 -05:00 committed by GitHub
parent c38839d72f
commit 0b0c94ee52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 1 deletions

View File

@ -1481,6 +1481,7 @@ def migrate_event_type_ids(instance: Recorder) -> bool:
event_type_to_id[ event_type_to_id[
db_event_type.event_type db_event_type.event_type
] = db_event_type.event_type_id ] = db_event_type.event_type_id
event_type_manager.clear_non_existent(db_event_type.event_type)
session.execute( session.execute(
update(Events), update(Events),

View File

@ -120,9 +120,17 @@ class EventTypeManager(BaseLRUTableManager[EventTypes]):
""" """
for event_type, db_event_types in self._pending.items(): for event_type, db_event_types in self._pending.items():
self._id_map[event_type] = db_event_types.event_type_id self._id_map[event_type] = db_event_types.event_type_id
self._non_existent_event_types.pop(event_type, None) self.clear_non_existent(event_type)
self._pending.clear() self._pending.clear()
def clear_non_existent(self, event_type: str) -> None:
"""Clear a non-existent event type from the cache.
This call is not thread-safe and must be called from the
recorder thread.
"""
self._non_existent_event_types.pop(event_type, None)
def evict_purged(self, event_types: Iterable[str]) -> None: def evict_purged(self, event_types: Iterable[str]) -> None:
"""Evict purged event_types from the cache when they are no longer used. """Evict purged event_types from the cache when they are no longer used.

View File

@ -553,6 +553,16 @@ async def test_migrate_event_type_ids(
assert len(events_by_type["event_type_one"]) == 2 assert len(events_by_type["event_type_one"]) == 2
assert len(events_by_type["event_type_two"]) == 1 assert len(events_by_type["event_type_two"]) == 1
def _get_many():
with session_scope(hass=hass, read_only=True) as session:
return instance.event_type_manager.get_many(
("event_type_one", "event_type_two"), session
)
mapped = await instance.async_add_executor_job(_get_many)
assert mapped["event_type_one"] is not None
assert mapped["event_type_two"] is not None
@pytest.mark.parametrize("enable_migrate_entity_ids", [True]) @pytest.mark.parametrize("enable_migrate_entity_ids", [True])
async def test_migrate_entity_ids( async def test_migrate_entity_ids(