indexer: fix resource type update

Currently when we update a resource type the history table is broken
because a column is missing.

This change fixes that.

Closes-bug: #1649261
Change-Id: I938c7263824bae0a01634fa48fa784a91ae49499
(cherry picked from commit 178a349162)
This commit is contained in:
Mehdi Abaakouk 2017-01-06 11:10:56 +01:00 committed by gordon chung
parent 7bf0e3540a
commit e34779ad19
2 changed files with 37 additions and 10 deletions

View File

@ -390,6 +390,9 @@ class SQLAlchemyIndexer(indexer.IndexerDriver):
del_attributes=None):
if not add_attributes and not del_attributes:
return
add_attributes = add_attributes or []
del_attributes = del_attributes or []
self._set_resource_type_state(name, "updating", "active")
try:
@ -399,16 +402,17 @@ class SQLAlchemyIndexer(indexer.IndexerDriver):
with self.facade.writer_connection() as connection:
ctx = migration.MigrationContext.configure(connection)
op = operations.Operations(ctx)
with op.batch_alter_table(rt.tablename) as batch_op:
for attr in del_attributes:
batch_op.drop_column(attr)
for attr in add_attributes:
# TODO(sileht): When attr.required is True, we have
# to pass a default. rest layer current protect us,
# requied = True is not yet allowed
batch_op.add_column(sqlalchemy.Column(
attr.name, attr.satype,
nullable=not attr.required))
for table in [rt.tablename, '%s_history' % rt.tablename]:
with op.batch_alter_table(table) as batch_op:
for attr in del_attributes:
batch_op.drop_column(attr)
for attr in add_attributes:
# TODO(sileht): When attr.required is True, we
# have to pass a default. rest layer current
# protect us, requied = True is not yet allowed
batch_op.add_column(sqlalchemy.Column(
attr.name, attr.satype,
nullable=not attr.required))
rt.state = "active"
rt.updated_at = utils.utcnow()

View File

@ -1098,6 +1098,29 @@ class TestIndexerDriver(tests_base.TestCase):
self.assertEqual("indexer_test", r.type)
self.assertEqual("col1_value", r.col1)
# Update the resource type
add_attrs = mgr.resource_type_from_dict("indexer_test", {
"col2": {"type": "number", "required": False,
"max": 100, "min": 0}
}, "creating").attributes
self.index.update_resource_type("indexer_test",
add_attributes=add_attrs)
# Check the new attribute
r = self.index.get_resource("indexer_test", rid)
self.assertIsNone(r.col2)
self.index.update_resource("indexer_test", rid, col2=10)
rl = self.index.list_resources('indexer_test',
{"=": {"id": rid}},
history=True,
sorts=['revision_start:asc',
'started_at:asc'])
self.assertEqual(2, len(rl))
self.assertIsNone(rl[0].col2)
self.assertEqual(10, rl[1].col2)
# Deletion
self.assertRaises(indexer.ResourceTypeInUse,
self.index.delete_resource_type,