Don't start recorder if a database from the future is used (#134467)

This commit is contained in:
Erik Montnemery 2025-01-02 18:56:23 +01:00 committed by GitHub
parent ee46edffa3
commit cf238cd8f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 0 deletions

View File

@ -719,6 +719,16 @@ class Recorder(threading.Thread):
if schema_status is None:
# Give up if we could not validate the schema
return
if schema_status.current_version > SCHEMA_VERSION:
_LOGGER.error(
"The database schema version %s is newer than %s which is the maximum "
"database schema version supported by the installed version of "
"Home Assistant Core, either upgrade Home Assistant Core or restore "
"the database from a backup compatible with this version",
schema_status.current_version,
SCHEMA_VERSION,
)
return
self.schema_version = schema_status.current_version
if not schema_status.migration_needed and not schema_status.schema_errors:

View File

@ -2615,6 +2615,46 @@ async def test_clean_shutdown_when_schema_migration_fails(
assert instance.engine is None
async def test_setup_fails_after_downgrade(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test we fail to setup after a downgrade.
Also test we shutdown cleanly.
"""
with (
patch.object(
migration,
"_get_current_schema_version",
side_effect=[None, SCHEMA_VERSION + 1],
),
patch("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB", True),
):
if recorder.DOMAIN not in hass.data:
recorder_helper.async_initialize_recorder(hass)
assert not await async_setup_component(
hass,
recorder.DOMAIN,
{
recorder.DOMAIN: {
CONF_DB_URL: "sqlite://",
CONF_DB_RETRY_WAIT: 0,
CONF_DB_MAX_RETRIES: 1,
}
},
)
await hass.async_block_till_done()
instance = recorder.get_instance(hass)
await hass.async_stop()
assert instance.engine is None
assert (
f"The database schema version {SCHEMA_VERSION+1} is newer than {SCHEMA_VERSION}"
" which is the maximum database schema version supported by the installed "
"version of Home Assistant Core"
) in caplog.text
async def test_events_are_recorded_until_final_write(
hass: HomeAssistant,
async_setup_recorder_instance: RecorderInstanceGenerator,