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