mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Correct migration to recorder schema 22 (#59048)
This commit is contained in:
parent
4345432d14
commit
2f3dea30e2
@ -12,6 +12,7 @@ from sqlalchemy.exc import (
|
|||||||
SQLAlchemyError,
|
SQLAlchemyError,
|
||||||
)
|
)
|
||||||
from sqlalchemy.schema import AddConstraint, DropConstraint
|
from sqlalchemy.schema import AddConstraint, DropConstraint
|
||||||
|
from sqlalchemy.sql.expression import true
|
||||||
|
|
||||||
from .models import (
|
from .models import (
|
||||||
SCHEMA_VERSION,
|
SCHEMA_VERSION,
|
||||||
@ -24,7 +25,7 @@ from .models import (
|
|||||||
StatisticsShortTerm,
|
StatisticsShortTerm,
|
||||||
process_timestamp,
|
process_timestamp,
|
||||||
)
|
)
|
||||||
from .statistics import get_metadata_with_session, get_start_time
|
from .statistics import get_start_time
|
||||||
from .util import session_scope
|
from .util import session_scope
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -558,21 +559,25 @@ def _apply_update(instance, session, new_version, old_version): # noqa: C901
|
|||||||
session.add(StatisticsRuns(start=fake_start_time))
|
session.add(StatisticsRuns(start=fake_start_time))
|
||||||
fake_start_time += timedelta(minutes=5)
|
fake_start_time += timedelta(minutes=5)
|
||||||
|
|
||||||
# Copy last hourly statistic to the newly created 5-minute statistics table
|
# When querying the database, be careful to only explicitly query for columns
|
||||||
sum_statistics = get_metadata_with_session(
|
# which were present in schema version 21. If querying the table, SQLAlchemy
|
||||||
instance.hass, session, statistic_type="sum"
|
# will refer to future columns.
|
||||||
)
|
for sum_statistic in session.query(StatisticsMeta.id).filter_by(has_sum=true()):
|
||||||
for metadata_id, _ in sum_statistics.values():
|
|
||||||
last_statistic = (
|
last_statistic = (
|
||||||
session.query(Statistics)
|
session.query(
|
||||||
.filter_by(metadata_id=metadata_id)
|
Statistics.start,
|
||||||
|
Statistics.last_reset,
|
||||||
|
Statistics.state,
|
||||||
|
Statistics.sum,
|
||||||
|
)
|
||||||
|
.filter_by(metadata_id=sum_statistic.id)
|
||||||
.order_by(Statistics.start.desc())
|
.order_by(Statistics.start.desc())
|
||||||
.first()
|
.first()
|
||||||
)
|
)
|
||||||
if last_statistic:
|
if last_statistic:
|
||||||
session.add(
|
session.add(
|
||||||
StatisticsShortTerm(
|
StatisticsShortTerm(
|
||||||
metadata_id=last_statistic.metadata_id,
|
metadata_id=sum_statistic.id,
|
||||||
start=last_statistic.start,
|
start=last_statistic.start,
|
||||||
last_reset=last_statistic.last_reset,
|
last_reset=last_statistic.last_reset,
|
||||||
state=last_statistic.state,
|
state=last_statistic.state,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user