From b9f29c27ab80e963a6c2edcbb2f7aa37cf4a9ca5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 16 Feb 2023 22:12:11 -0600 Subject: [PATCH] Cleanup stale recorder code (#88275) - Removes dead commit function - Small cleanup to debug handling in execute --- homeassistant/components/recorder/util.py | 24 +++++------------------ tests/components/recorder/test_util.py | 22 --------------------- 2 files changed, 5 insertions(+), 41 deletions(-) diff --git a/homeassistant/components/recorder/util.py b/homeassistant/components/recorder/util.py index 3fc41fe22e5..f8a40972307 100644 --- a/homeassistant/components/recorder/util.py +++ b/homeassistant/components/recorder/util.py @@ -132,23 +132,6 @@ def session_scope( session.close() -def commit(session: Session, work: Any) -> bool: - """Commit & retry work: Either a model or in a function.""" - for _ in range(0, RETRIES): - try: - if callable(work): - work(session) - else: - session.add(work) - session.commit() - return True - except OperationalError as err: - _LOGGER.error("Error executing query: %s", err) - session.rollback() - time.sleep(QUERY_RETRY_WAIT) - return False - - def execute( qry: Query, to_native: bool = False, validate_entity_ids: bool = True ) -> list[Row]: @@ -156,9 +139,12 @@ def execute( This method also retries a few times in the case of stale connections. """ + debug = _LOGGER.isEnabledFor(logging.DEBUG) for tryno in range(0, RETRIES): try: - timer_start = time.perf_counter() + if debug: + timer_start = time.perf_counter() + if to_native: result = [ row @@ -171,7 +157,7 @@ def execute( else: result = qry.all() - if _LOGGER.isEnabledFor(logging.DEBUG): + if debug: elapsed = time.perf_counter() - timer_start if to_native: _LOGGER.debug( diff --git a/tests/components/recorder/test_util.py b/tests/components/recorder/test_util.py index b744feff8a9..31868b5f839 100644 --- a/tests/components/recorder/test_util.py +++ b/tests/components/recorder/test_util.py @@ -45,28 +45,6 @@ def test_session_scope_not_setup(hass_recorder): pass -def test_recorder_bad_commit(hass_recorder, recorder_db_url): - """Bad _commit should retry 3 times.""" - if recorder_db_url.startswith(("mysql://", "postgresql://")): - # This test is specific for SQLite: mysql/postgresql does not raise an OperationalError - # which triggers retries for the bad query below, it raises ProgrammingError - # on which we give up - return - - hass = hass_recorder() - - def work(session): - """Bad work.""" - session.execute(text("select * from notthere")) - - with patch( - "homeassistant.components.recorder.core.time.sleep" - ) as e_mock, util.session_scope(hass=hass) as session: - res = util.commit(session, work) - assert res is False - assert e_mock.call_count == 3 - - def test_recorder_bad_execute(hass_recorder): """Bad execute, retry 3 times.""" from sqlalchemy.exc import SQLAlchemyError