Move async_migration_in_progress (#59087)

This commit is contained in:
Erik Montnemery 2021-11-05 10:40:56 +01:00 committed by GitHub
parent 8cc2f3b7a4
commit 470b01e4ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 43 additions and 46 deletions

View File

@ -563,9 +563,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa:
async def async_handle_core_service(call):
"""Service handler for handling core services."""
if (
call.service in SHUTDOWN_SERVICES
and await recorder.async_migration_in_progress(hass)
if call.service in SHUTDOWN_SERVICES and recorder.async_migration_in_progress(
hass
):
_LOGGER.error(
"The system cannot %s while a database upgrade is in progress",

View File

@ -138,9 +138,8 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
async def async_handle_core_service(call):
"""Service handler for handling core services."""
if (
call.service in SHUTDOWN_SERVICES
and await recorder.async_migration_in_progress(hass)
if call.service in SHUTDOWN_SERVICES and recorder.async_migration_in_progress(
hass
):
_LOGGER.error(
"The system cannot %s while a database upgrade is in progress",

View File

@ -176,18 +176,6 @@ CONFIG_SCHEMA = vol.Schema(
)
@bind_hass
async def async_migration_in_progress(hass: HomeAssistant) -> bool:
"""Determine is a migration is in progress.
This is a thin wrapper that allows us to change
out the implementation later.
"""
if DATA_INSTANCE not in hass.data:
return False
return hass.data[DATA_INSTANCE].migration_in_progress
@bind_hass
def is_entity_recorded(hass: HomeAssistant, entity_id: str) -> bool:
"""Check if an entity is being recorded.

View File

@ -455,3 +455,14 @@ def perodic_db_cleanups(instance: Recorder):
_LOGGER.debug("WAL checkpoint")
with instance.engine.connect() as connection:
connection.execute(text("PRAGMA wal_checkpoint(TRUNCATE);"))
def async_migration_in_progress(hass: HomeAssistant) -> bool:
"""Determine is a migration is in progress.
This is a thin wrapper that allows us to change
out the implementation later.
"""
if DATA_INSTANCE not in hass.data:
return False
return hass.data[DATA_INSTANCE].migration_in_progress

View File

@ -10,6 +10,7 @@ from homeassistant.core import HomeAssistant, callback
from .const import DATA_INSTANCE, MAX_QUEUE_BACKLOG
from .statistics import validate_statistics
from .util import async_migration_in_progress
if TYPE_CHECKING:
from . import Recorder
@ -93,7 +94,7 @@ def ws_info(
instance: Recorder = hass.data[DATA_INSTANCE]
backlog = instance.queue.qsize() if instance and instance.queue else None
migration_in_progress = instance.migration_in_progress if instance else False
migration_in_progress = async_migration_in_progress(hass)
recording = instance.recording if instance else False
thread_alive = instance.is_alive() if instance else False

View File

@ -4,12 +4,11 @@
from homeassistant.core import HomeAssistant
async def async_migration_in_progress(hass: HomeAssistant) -> bool:
def async_migration_in_progress(hass: HomeAssistant) -> bool:
"""Check to see if a recorder migration is in progress."""
if "recorder" not in hass.config.components:
return False
from homeassistant.components import ( # pylint: disable=import-outside-toplevel
recorder,
)
# pylint: disable-next=import-outside-toplevel
from homeassistant.components import recorder
return await recorder.async_migration_in_progress(hass)
return recorder.util.async_migration_in_progress(hass)

View File

@ -41,7 +41,7 @@ def _get_native_states(hass, entity_id):
async def test_schema_update_calls(hass):
"""Test that schema migrations occur in correct order."""
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
with patch(
"homeassistant.components.recorder.create_engine", new=create_engine_test
@ -54,7 +54,7 @@ async def test_schema_update_calls(hass):
)
await async_wait_recording_done_without_instance(hass)
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
update.assert_has_calls(
[
call(hass.data[DATA_INSTANCE], ANY, version + 1, 0)
@ -65,7 +65,7 @@ async def test_schema_update_calls(hass):
async def test_migration_in_progress(hass):
"""Test that we can check for migration in progress."""
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
with patch(
"homeassistant.components.recorder.create_engine", new=create_engine_test
@ -74,15 +74,15 @@ async def test_migration_in_progress(hass):
hass, "recorder", {"recorder": {"db_url": "sqlite://"}}
)
await hass.data[DATA_INSTANCE].async_migration_event.wait()
assert await recorder.async_migration_in_progress(hass) is True
assert recorder.util.async_migration_in_progress(hass) is True
await async_wait_recording_done_without_instance(hass)
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
async def test_database_migration_failed(hass):
"""Test we notify if the migration fails."""
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
with patch(
"homeassistant.components.recorder.create_engine", new=create_engine_test
@ -104,7 +104,7 @@ async def test_database_migration_failed(hass):
await hass.async_add_executor_job(hass.data[DATA_INSTANCE].join)
await hass.async_block_till_done()
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
assert len(mock_create.mock_calls) == 2
assert len(mock_dismiss.mock_calls) == 1
@ -112,7 +112,7 @@ async def test_database_migration_failed(hass):
async def test_database_migration_encounters_corruption(hass):
"""Test we move away the database if its corrupt."""
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
sqlite3_exception = DatabaseError("statement", {}, [])
sqlite3_exception.__cause__ = sqlite3.DatabaseError()
@ -133,13 +133,13 @@ async def test_database_migration_encounters_corruption(hass):
hass.states.async_set("my.entity", "off", {})
await async_wait_recording_done_without_instance(hass)
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
assert move_away.called
async def test_database_migration_encounters_corruption_not_sqlite(hass):
"""Test we fail on database error when we cannot recover."""
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
with patch(
"homeassistant.components.recorder.migration.schema_is_current",
@ -164,7 +164,7 @@ async def test_database_migration_encounters_corruption_not_sqlite(hass):
await hass.async_add_executor_job(hass.data[DATA_INSTANCE].join)
await hass.async_block_till_done()
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
assert not move_away.called
assert len(mock_create.mock_calls) == 2
assert len(mock_dismiss.mock_calls) == 1
@ -173,7 +173,7 @@ async def test_database_migration_encounters_corruption_not_sqlite(hass):
async def test_events_during_migration_are_queued(hass):
"""Test that events during migration are queued."""
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
with patch(
"homeassistant.components.recorder.create_engine", new=create_engine_test
@ -190,7 +190,7 @@ async def test_events_during_migration_are_queued(hass):
await hass.data[DATA_INSTANCE].async_recorder_ready.wait()
await async_wait_recording_done_without_instance(hass)
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
db_states = await hass.async_add_executor_job(_get_native_states, hass, "my.entity")
assert len(db_states) == 2
@ -198,7 +198,7 @@ async def test_events_during_migration_are_queued(hass):
async def test_events_during_migration_queue_exhausted(hass):
"""Test that events during migration takes so long the queue is exhausted."""
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
with patch(
"homeassistant.components.recorder.create_engine", new=create_engine_test
@ -216,7 +216,7 @@ async def test_events_during_migration_queue_exhausted(hass):
await hass.data[DATA_INSTANCE].async_recorder_ready.wait()
await async_wait_recording_done_without_instance(hass)
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
db_states = await hass.async_add_executor_job(_get_native_states, hass, "my.entity")
assert len(db_states) == 1
hass.states.async_set("my.entity", "on", {})

View File

@ -305,7 +305,7 @@ async def test_recorder_info_bad_recorder_config(hass, hass_ws_client):
async def test_recorder_info_migration_queue_exhausted(hass, hass_ws_client):
"""Test getting recorder status when recorder queue is exhausted."""
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.util.async_migration_in_progress(hass) is False
migration_done = threading.Event()

View File

@ -10,23 +10,23 @@ from tests.common import async_init_recorder_component
async def test_async_migration_in_progress(hass):
"""Test async_migration_in_progress wraps the recorder."""
with patch(
"homeassistant.components.recorder.async_migration_in_progress",
"homeassistant.components.recorder.util.async_migration_in_progress",
return_value=False,
):
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.async_migration_in_progress(hass) is False
# The recorder is not loaded
with patch(
"homeassistant.components.recorder.async_migration_in_progress",
"homeassistant.components.recorder.util.async_migration_in_progress",
return_value=True,
):
assert await recorder.async_migration_in_progress(hass) is False
assert recorder.async_migration_in_progress(hass) is False
await async_init_recorder_component(hass)
# The recorder is now loaded
with patch(
"homeassistant.components.recorder.async_migration_in_progress",
"homeassistant.components.recorder.util.async_migration_in_progress",
return_value=True,
):
assert await recorder.async_migration_in_progress(hass) is True
assert recorder.async_migration_in_progress(hass) is True