New indexes for states and recording_runs tables (#6688)

* New indexes for states table

* Added recorder_runs indexes

* Created a new function for compound indexes.

A new function was created because it makes it a little cleaner when creating
a single-field index since one doesn't have to create a list. This is mostly
when creating the name of the index so with a bit more logic it's possible
to combine it into one function. Given how often migration changes are run,
I thought that code bloat was probably a worthy trade-off for now.

* Adjusted indexes, POC for ref indexes by name.

* Corrected lint errors

* Fixed pydocstyle error

* Moved create_index function outside apply_update

* Moved to single line (just barely)
This commit is contained in:
Tim Soderstrom authored and Paulus Schoutsen committed 2017-03-23 20:48:31 -07:00
1 parent 6c5989895a
commit 5dfdb9e481
3 files changed
+33 -18

No files matched your search

+8 -3
View File
@@ -16,7 +16,7 @@ from homeassistant.remote import JSONEncoder
# pylint: disable=invalid-name
Base = declarative_base()
SCHEMA_VERSION = 1
SCHEMA_VERSION = 2
_LOGGER = logging.getLogger(__name__)
@@ -66,13 +66,16 @@ class States(Base): # type: ignore
attributes = Column(Text)
event_id = Column(Integer, ForeignKey('events.event_id'))
last_changed = Column(DateTime(timezone=True), default=datetime.utcnow)
last_updated = Column(DateTime(timezone=True), default=datetime.utcnow)
last_updated = Column(DateTime(timezone=True), default=datetime.utcnow,
index=True)
created = Column(DateTime(timezone=True), default=datetime.utcnow)
__table_args__ = (Index('states__state_changes',
'last_changed', 'last_updated', 'entity_id'),
Index('states__significant_changes',
'domain', 'last_updated', 'entity_id'), )
'domain', 'last_updated', 'entity_id'),
Index('ix_states_entity_id_created',
'entity_id', 'created'),)
@staticmethod
def from_event(event):
@@ -124,6 +127,8 @@ class RecorderRuns(Base): # type: ignore
closed_incorrect = Column(Boolean, default=False)
created = Column(DateTime(timezone=True), default=datetime.utcnow)
__table_args__ = (Index('ix_recorder_runs_start_end', 'start', 'end'),)
def entity_ids(self, point_in_time=None):
"""Return the entity ids that existed in this run.