Deduplicate entity_id in the states table (#89557)

This commit is contained in:
J. Nick Koston 2023-03-12 10:01:58 -10:00 committed by GitHub
parent 8d88b02c2e
commit c41f91be89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 3715 additions and 1018 deletions

View file

@ -18,7 +18,7 @@ from homeassistant.components import recorder
from homeassistant.components.recorder import util
from homeassistant.components.recorder.const import DOMAIN, SQLITE_URL_PREFIX
from homeassistant.components.recorder.db_schema import RecorderRuns
from homeassistant.components.recorder.history.legacy import (
from homeassistant.components.recorder.history.modern import (
_get_single_entity_states_stmt,
)
from homeassistant.components.recorder.models import (
@ -908,26 +908,25 @@ def test_execute_stmt_lambda_element(
with session_scope(hass=hass) as session:
# No time window, we always get a list
stmt = _get_single_entity_states_stmt(
instance.schema_version, dt_util.utcnow(), "sensor.on", False
)
metadata_id = instance.states_meta_manager.get("sensor.on", session)
stmt = _get_single_entity_states_stmt(dt_util.utcnow(), metadata_id, False)
rows = util.execute_stmt_lambda_element(session, stmt)
assert isinstance(rows, list)
assert rows[0].state == new_state.state
assert rows[0].entity_id == new_state.entity_id
assert rows[0].metadata_id == metadata_id
# Time window >= 2 days, we get a ChunkedIteratorResult
rows = util.execute_stmt_lambda_element(session, stmt, now, one_week_from_now)
assert isinstance(rows, ChunkedIteratorResult)
row = next(rows)
assert row.state == new_state.state
assert row.entity_id == new_state.entity_id
assert row.metadata_id == metadata_id
# Time window < 2 days, we get a list
rows = util.execute_stmt_lambda_element(session, stmt, now, tomorrow)
assert isinstance(rows, list)
assert rows[0].state == new_state.state
assert rows[0].entity_id == new_state.entity_id
assert rows[0].metadata_id == metadata_id
with patch.object(session, "execute", MockExecutor):
rows = util.execute_stmt_lambda_element(session, stmt, now, tomorrow)