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

Notify on corrupt SQLite database #131367

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions homeassistant/components/recorder/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,9 +1236,31 @@ def _handle_sqlite_corruption(self, setup_run: bool) -> None:
move_away_broken_database(dburl_to_path(self.db_url))
self.recorder_runs_manager.reset()
self._setup_recorder()
self._notify_db_corruption()

if setup_run:
self._setup_run()

def _notify_db_corruption(self) -> None:
"""Notify users of SQLite database corruption.

Create a persistent notification and fire a 'recorder_database_corrupt' event to inform
users that their recorder database has been rebuilt due to database corruption.
"""
persistent_notification.create(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We consider persistent notifications to be user space nowadays, we should not be using that to send users system messages.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @frenck ,

Is there another mechanism we can use to display these sort of errors in the UI?

I can see value in there being some sort of proactive alert/notification as the database corruption is effectively handled silently, so users would not know to check the logs for the error.

Best,
Ryan

self.hass,
(
"Corruption was detected in the recorder SQLite database and a"
" new database has been created. See"
" [this page](https://www.home-assistant.io/integrations/recorder/#handling-disk-corruption-and-hardware-failures) for more information."
),
"Database corrupt",
"recorder_database_corrupt",
)

# Send an event to the bus
self.hass.bus.fire("recorder_database_corrupt")

def _close_event_session(self) -> None:
"""Close the event session."""
self.states_manager.reset()
Expand Down
11 changes: 11 additions & 0 deletions homeassistant/components/recorder/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
"sqlite_too_old": {
"title": "Update SQLite to {min_version} or later to continue using the recorder",
"description": "Support for version {server_version} of SQLite is ending; the minimum supported version is {min_version}. Please upgrade your database software."
},
"database_corrupt": {
"title": "SQLite database corruption detected",
"fix_flow": {
"step": {
"confirm": {
"title": "[%key:component::recorder::issues::database_corrupt::title%]",
"description": "Corruption was detected in the SQLite database file. The recorder database has been re-created, and no further action is required."
}
}
}
}
},
"services": {
Expand Down
33 changes: 29 additions & 4 deletions tests/components/recorder/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1687,8 +1687,18 @@ async def test_database_corruption_while_running(
recorder_mock: Recorder,
recorder_db_url: str,
caplog: pytest.LogCaptureFixture,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test we can recover from sqlite3 db corruption."""
# Setup listener for DB corruption event
corruption_events = 0

def handle_corruption_event(event):
nonlocal corruption_events
corruption_events += 1

hass.bus.async_listen("recorder_database_corrupt", handle_corruption_event)

await hass.async_block_till_done()
caplog.clear()

Expand All @@ -1703,10 +1713,15 @@ async def test_database_corruption_while_running(
)

await async_wait_recording_done(hass)
with patch.object(
get_instance(hass).event_session,
"close",
side_effect=OperationalError("statement", {}, []),
with (
patch.object(
get_instance(hass).event_session,
"close",
side_effect=OperationalError("statement", {}, []),
),
patch(
"homeassistant.components.recorder.core.persistent_notification.create"
) as notify_create_mock,
):
await async_wait_recording_done(hass)
test_db_file = recorder_db_url.removeprefix("sqlite:///")
Expand All @@ -1728,6 +1743,16 @@ async def test_database_corruption_while_running(
assert "The system will rename the corrupt database file" in caplog.text
assert "Connected to recorder database" in caplog.text

# Check that we tried to create a persistent notification
assert notify_create_mock.call_count == 1
assert (
"Corruption was detected in the recorder SQLite database and a new database has been create"
in notify_create_mock.call_args.args[1]
)

# Check the DB corruption event was fired
assert corruption_events == 1

# This state should go into the new database
hass.states.async_set("test.two", "on", {})
await async_wait_recording_done(hass)
Expand Down