Only remove duplicated statistics on error (#65653)
parent
fe05d6680c
commit
1f8e8926fe
|
@ -6,6 +6,7 @@ import logging
|
|||
import sqlalchemy
|
||||
from sqlalchemy import ForeignKeyConstraint, MetaData, Table, func, text
|
||||
from sqlalchemy.exc import (
|
||||
DatabaseError,
|
||||
InternalError,
|
||||
OperationalError,
|
||||
ProgrammingError,
|
||||
|
@ -612,22 +613,31 @@ def _apply_update(instance, new_version, old_version): # noqa: C901
|
|||
# Add name column to StatisticsMeta
|
||||
_add_columns(instance, "statistics_meta", ["name VARCHAR(255)"])
|
||||
elif new_version == 24:
|
||||
# Delete duplicated statistics
|
||||
with session_scope(session=instance.get_session()) as session:
|
||||
delete_duplicates(instance, session)
|
||||
# Recreate statistics indices to block duplicated statistics
|
||||
_drop_index(instance, "statistics", "ix_statistics_statistic_id_start")
|
||||
_create_index(instance, "statistics", "ix_statistics_statistic_id_start")
|
||||
_drop_index(
|
||||
instance,
|
||||
"statistics_short_term",
|
||||
"ix_statistics_short_term_statistic_id_start",
|
||||
)
|
||||
_create_index(
|
||||
instance,
|
||||
"statistics_short_term",
|
||||
"ix_statistics_short_term_statistic_id_start",
|
||||
)
|
||||
try:
|
||||
_create_index(instance, "statistics", "ix_statistics_statistic_id_start")
|
||||
_create_index(
|
||||
instance,
|
||||
"statistics_short_term",
|
||||
"ix_statistics_short_term_statistic_id_start",
|
||||
)
|
||||
except DatabaseError:
|
||||
# There may be duplicated statistics entries, delete duplicated statistics
|
||||
# and try again
|
||||
with session_scope(session=instance.get_session()) as session:
|
||||
delete_duplicates(instance, session)
|
||||
_create_index(instance, "statistics", "ix_statistics_statistic_id_start")
|
||||
_create_index(
|
||||
instance,
|
||||
"statistics_short_term",
|
||||
"ix_statistics_short_term_statistic_id_start",
|
||||
)
|
||||
|
||||
else:
|
||||
raise ValueError(f"No schema migration defined for version {new_version}")
|
||||
|
|
Loading…
Reference in New Issue