diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index ef273b388ca..e05491b5ae1 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -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", diff --git a/homeassistant/components/homeassistant/__init__.py b/homeassistant/components/homeassistant/__init__.py index 8c31859b8e0..d75358fe62e 100644 --- a/homeassistant/components/homeassistant/__init__.py +++ b/homeassistant/components/homeassistant/__init__.py @@ -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", diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index 6a9c53b96ad..da3955cb9b8 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -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. diff --git a/homeassistant/components/recorder/util.py b/homeassistant/components/recorder/util.py index 6c53347a536..c63f6abee3a 100644 --- a/homeassistant/components/recorder/util.py +++ b/homeassistant/components/recorder/util.py @@ -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 diff --git a/homeassistant/components/recorder/websocket_api.py b/homeassistant/components/recorder/websocket_api.py index b06d2d07f1e..5a4f0425919 100644 --- a/homeassistant/components/recorder/websocket_api.py +++ b/homeassistant/components/recorder/websocket_api.py @@ -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 diff --git a/homeassistant/helpers/recorder.py b/homeassistant/helpers/recorder.py index e3ed3428a2a..a51d9de59e2 100644 --- a/homeassistant/helpers/recorder.py +++ b/homeassistant/helpers/recorder.py @@ -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) diff --git a/tests/components/recorder/test_migrate.py b/tests/components/recorder/test_migrate.py index e3cf8fc3527..7cdf69f1282 100644 --- a/tests/components/recorder/test_migrate.py +++ b/tests/components/recorder/test_migrate.py @@ -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", {}) diff --git a/tests/components/recorder/test_websocket_api.py b/tests/components/recorder/test_websocket_api.py index 52a9424b4ac..7a45dea0379 100644 --- a/tests/components/recorder/test_websocket_api.py +++ b/tests/components/recorder/test_websocket_api.py @@ -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() diff --git a/tests/helpers/test_recorder.py b/tests/helpers/test_recorder.py index 60d60a2335e..c0663cba165 100644 --- a/tests/helpers/test_recorder.py +++ b/tests/helpers/test_recorder.py @@ -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