Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix recorder run history during schema migration and startup #87492

Merged
merged 1 commit into from Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions homeassistant/components/recorder/run_history.py
Expand Up @@ -64,8 +64,13 @@ def first(self) -> RecorderRuns:
@property
def current(self) -> RecorderRuns:
"""Get the current run."""
assert self._current_run_info is not None
return self._current_run_info
# If start has not been called yet because the recorder is
# still starting up we want history to use the current time
# as the created time to ensure we can still return results
# and we do not try to pull data from the previous run.
return self._current_run_info or RecorderRuns(
start=self.recording_start, created=dt_util.utcnow()
)

def get(self, start: datetime) -> RecorderRuns | None:
"""Return the recorder run that started before or at start.
Expand Down
11 changes: 11 additions & 0 deletions tests/components/recorder/test_run_history.py
Expand Up @@ -45,3 +45,14 @@ async def test_run_history(recorder_mock, hass):
process_timestamp(instance.run_history.get(now).start)
== instance.run_history.recording_start
)


async def test_run_history_during_schema_migration(recorder_mock, hass):
"""Test the run history during schema migration."""
instance = recorder.get_instance(hass)
run_history = instance.run_history
assert run_history.current.start == run_history.recording_start
with instance.get_session() as session:
run_history.start(session)
assert run_history.current.start == run_history.recording_start
assert run_history.current.created >= run_history.recording_start