Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions homeassistant/components/recorder/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
def purge_old_data(instance, purge_days, repack):
"""Purge events and states older than purge_days ago."""
from .models import States, Events
from sqlalchemy import orm
from sqlalchemy.sql import exists
from sqlalchemy import func

purge_before = dt_util.utcnow() - timedelta(days=purge_days)
_LOGGER.debug("Purging events before %s", purge_before)
Expand All @@ -22,18 +21,10 @@ def purge_old_data(instance, purge_days, repack):
# For each entity, the most recent state is protected from deletion
# s.t. we can properly restore state even if the entity has not been
# updated in a long time
states_alias = orm.aliased(States, name='StatesAlias')
protected_states = session.query(States.state_id, States.event_id)\
.filter(~exists()
.where(States.entity_id ==
states_alias.entity_id)
.where(states_alias.last_updated >
States.last_updated))\
.all()
protected_states = session.query(func.max(States.state_id)) \
.group_by(States.entity_id).all()

protected_state_ids = tuple((state[0] for state in protected_states))
protected_event_ids = tuple((state[1] for state in protected_states
if state[1] is not None))

deleted_rows = session.query(States) \
.filter((States.last_updated < purge_before)) \
Expand All @@ -46,6 +37,13 @@ def purge_old_data(instance, purge_days, repack):
# Otherwise, if the SQL server has "ON DELETE CASCADE" as default, it
# will delete the protected state when deleting its associated
# event. Also, we would be producing NULLed foreign keys otherwise.
protected_events = session.query(States.event_id) \
.filter(States.state_id.in_(protected_state_ids)) \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a warning from pytest when running the purge tests locally. I think that it is from this line.

tests/components/recorder/test_purge.py::TestRecorderPurge::test_purge_old_events
  /Users/paulus/dev/python/py34-home-assistant/lib/python3.4/site-packages/sqlalchemy/sql/default_comparator.py:161: SAWarning: The IN-predicate on "states.state_id" was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate.  Consider alternative strategies for improved performance.
    'strategies for improved performance.' % expr)
  /Users/paulus/dev/python/py34-home-assistant/lib/python3.4/site-packages/sqlalchemy/sql/default_comparator.py:161: SAWarning: The IN-predicate on "events.event_id" was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate.  Consider alternative strategies for improved performance.
    'strategies for improved performance.' % expr)

.filter(States.event_id.isnot(None)) \
.all()

protected_event_ids = tuple((state[0] for state in protected_events))

deleted_rows = session.query(Events) \
.filter((Events.time_fired < purge_before)) \
.filter(~Events.event_id.in_(
Expand Down