Skip to content

Commit

Permalink
tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Apr 28, 2022
1 parent ce2231d commit 6e64672
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
37 changes: 17 additions & 20 deletions homeassistant/components/recorder/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,6 @@
_LOGGER = logging.getLogger(__name__)


def _generate_find_attr_lambda() -> StatementLambdaElement:
"""Generate the find attributes select only once."""
return lambda_stmt(
lambda: union_all(
*[
select(func.min(States.attributes_id)).where(
States.attributes_id == bindparam(f"a{idx}", required=False)
)
for idx in range(
998
) # MAX_ROWS_TO_PURGE inlined to avoid TypeError: 'PyWrapper' object cannot be interpreted as an integer
]
)
)


FIND_ATTRS_LAMBDA = _generate_find_attr_lambda()


@retryable_database_job("purge")
def purge_old_data(
instance: Recorder, purge_before: datetime, repack: bool, apply_filter: bool = False
Expand Down Expand Up @@ -132,6 +113,22 @@ def _select_event_state_and_attributes_ids_to_purge(
return event_ids, state_ids, attributes_ids


def _generate_find_attr_lambda() -> StatementLambdaElement:
"""Generate the find attributes select only once."""
return lambda_stmt(
lambda: union_all(
*[
select(func.min(States.attributes_id)).where(
States.attributes_id == bindparam(f"a{idx}", required=False)
)
for idx in range(
998
) # MAX_ROWS_TO_PURGE inlined to avoid TypeError: 'PyWrapper' object cannot be interpreted as an integer
]
)
)


def _select_unused_attributes_ids(
session: Session, attributes_ids: set[int], using_sqlite: bool
) -> set[int]:
Expand Down Expand Up @@ -180,7 +177,7 @@ def _select_unused_attributes_ids(
# different queries in the cache.
#
id_query = session.execute(
FIND_ATTRS_LAMBDA.params(
_generate_find_attr_lambda().params(
{
f"a{idx}": attributes_id
for idx, attributes_id in enumerate(attributes_ids)
Expand Down
8 changes: 4 additions & 4 deletions tests/components/recorder/test_purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def mock_use_sqlite(request):
"homeassistant.components.recorder.Recorder.using_sqlite",
return_value=request.param,
), patch(
"homeassistant.components.recorder.purge.FIND_ATTRS_LAMBDA",
_generate_sqlite_compatible_find_attr_lambda(),
"homeassistant.components.recorder.purge._generate_find_attr_lambda",
_generate_sqlite_compatible_find_attr_lambda,
):
yield

Expand Down Expand Up @@ -190,8 +190,8 @@ async def test_purge_old_states_encounters_temporary_mysql_error(
), patch.object(
instance.engine.dialect, "name", "mysql"
), patch(
"homeassistant.components.recorder.purge.FIND_ATTRS_LAMBDA",
_generate_sqlite_compatible_find_attr_lambda(),
"homeassistant.components.recorder.purge._generate_find_attr_lambda",
_generate_sqlite_compatible_find_attr_lambda,
):
await hass.services.async_call(
recorder.DOMAIN, recorder.SERVICE_PURGE, {"keep_days": 0}
Expand Down

0 comments on commit 6e64672

Please sign in to comment.