mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Forgiving add index in migration (#16092)
This commit is contained in:
parent
ef07460792
commit
68cd65567d
@ -57,6 +57,7 @@ def _create_index(engine, table_name, index_name):
|
|||||||
within the table definition described in the models
|
within the table definition described in the models
|
||||||
"""
|
"""
|
||||||
from sqlalchemy import Table
|
from sqlalchemy import Table
|
||||||
|
from sqlalchemy.exc import OperationalError
|
||||||
from . import models
|
from . import models
|
||||||
|
|
||||||
table = Table(table_name, models.Base.metadata)
|
table = Table(table_name, models.Base.metadata)
|
||||||
@ -67,7 +68,15 @@ def _create_index(engine, table_name, index_name):
|
|||||||
_LOGGER.info("Adding index `%s` to database. Note: this can take several "
|
_LOGGER.info("Adding index `%s` to database. Note: this can take several "
|
||||||
"minutes on large databases and slow computers. Please "
|
"minutes on large databases and slow computers. Please "
|
||||||
"be patient!", index_name)
|
"be patient!", index_name)
|
||||||
index.create(engine)
|
try:
|
||||||
|
index.create(engine)
|
||||||
|
except OperationalError as err:
|
||||||
|
if 'already exists' not in str(err).lower():
|
||||||
|
raise
|
||||||
|
|
||||||
|
_LOGGER.warning('Index %s already exists on %s, continueing',
|
||||||
|
index_name, table_name)
|
||||||
|
|
||||||
_LOGGER.debug("Finished creating %s", index_name)
|
_LOGGER.debug("Finished creating %s", index_name)
|
||||||
|
|
||||||
|
|
||||||
|
@ -80,3 +80,13 @@ def test_forgiving_add_column():
|
|||||||
migration._add_columns(engine, 'hello', [
|
migration._add_columns(engine, 'hello', [
|
||||||
'context_id CHARACTER(36)',
|
'context_id CHARACTER(36)',
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
|
def test_forgiving_add_index():
|
||||||
|
"""Test that add index will continue if index exists."""
|
||||||
|
engine = create_engine(
|
||||||
|
'sqlite://',
|
||||||
|
poolclass=StaticPool
|
||||||
|
)
|
||||||
|
models.Base.metadata.create_all(engine)
|
||||||
|
migration._create_index(engine, "states", "ix_states_context_id")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user