diff --git a/homeassistant/components/recorder/core.py b/homeassistant/components/recorder/core.py index 92e10b47126..30ece9e98a5 100644 --- a/homeassistant/components/recorder/core.py +++ b/homeassistant/components/recorder/core.py @@ -436,7 +436,7 @@ class Recorder(threading.Thread): self.async_db_connected.set_result(True) @callback - def async_set_recorder_ready(self) -> None: + def async_set_db_ready(self) -> None: """Database live and ready for use. Called after non-live migration steps are finished. @@ -577,14 +577,19 @@ class Recorder(threading.Thread): self.hass.add_job(self.async_connection_success) - # If shutdown happened before Home Assistant finished starting - if self._wait_startup_or_shutdown() is SHUTDOWN_TASK: - self.migration_in_progress = False - # Make sure we cleanly close the run if - # we restart before startup finishes - self._shutdown() - self.hass.add_job(self.async_set_recorder_ready) - return + if self.migration_is_live or schema_is_current: + # If the migrate is live or the schema is current, we need to + # wait for startup to complete. If its not live, we need to continue + # on. + self.hass.add_job(self.async_set_db_ready) + # If shutdown happened before Home Assistant finished starting + if self._wait_startup_or_shutdown() is SHUTDOWN_TASK: + self.migration_in_progress = False + # Make sure we cleanly close the run if + # we restart before startup finishes + self._shutdown() + self.hass.add_job(self.async_set_db_ready) + return # We wait to start the migration until startup has finished # since it can be cpu intensive and we do not want it to compete @@ -604,11 +609,11 @@ class Recorder(threading.Thread): "Database Migration Failed", "recorder_database_migration", ) - self.hass.add_job(self.async_set_recorder_ready) + self.hass.add_job(self.async_set_db_ready) self._shutdown() return - self.hass.add_job(self.async_set_recorder_ready) + self.hass.add_job(self.async_set_db_ready) _LOGGER.debug("Recorder processing the queue") self.hass.add_job(self._async_set_recorder_ready_migration_done) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index de6fd8f01fe..6e4a67c9da5 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -99,7 +99,7 @@ def migrate_schema( if live_migration(version) and not db_ready: db_ready = True instance.migration_is_live = True - hass.add_job(instance.async_set_recorder_ready) + hass.add_job(instance.async_set_db_ready) new_version = version + 1 _LOGGER.info("Upgrading recorder db schema to version %s", new_version) _apply_update(hass, engine, session_maker, new_version, current_version)