This commit is contained in:
J. Nick Koston
2023-03-10 10:05:51 -10:00
parent aaf1103652
commit 7161b15c91
3 changed files with 16 additions and 13 deletions

View File

@@ -1315,6 +1315,8 @@ def migrate_event_type_ids(instance: Recorder) -> bool:
session.add_all(missing_db_event_types)
session.flush() # Assign ids
for db_event_type in missing_db_event_types:
# We cannot add the assigned ids to the event_type_manager
# because the commit could get rolled back
assert db_event_type.event_type is not None
event_type_to_id[
db_event_type.event_type

View File

@@ -112,7 +112,8 @@ def purge_old_data(
return False
_purge_old_recorder_runs(instance, session, purge_before)
_purge_old_event_types(instance, session)
if instance.event_type_manager.active:
_purge_old_event_types(instance, session)
if repack:
repack_database(instance)
return True

View File

@@ -42,21 +42,21 @@ class EventTypeManager:
results: dict[str, int | None] = {}
missing: list[str] = []
for event_type in event_types:
if event_type_id := self._id_map.get(event_type):
results[event_type] = event_type_id
else:
if (event_type_id := self._id_map.get(event_type)) is None:
missing.append(event_type)
for event_type in missing:
with session.no_autoflush:
if event_type_row := session.execute(
find_event_type_ids(missing)
).first():
event_type_id = cast(int, event_type_row[0])
results[event_type] = self._id_map[event_type] = event_type_id
continue
results[event_type] = event_type_id
results[event_type] = None
if not missing:
return results
with session.no_autoflush:
for event_type_id, event_type in session.execute(
find_event_type_ids(missing)
):
results[event_type] = self._id_map[event_type] = cast(
int, event_type_id
)
return results