on-delete cascade rule for resource_data
During deleting of resource, the resource_data is also loaded in memory and then deleted. This is not performant. The passive_delete option in relationship deletes the relation without loading the children. This patch configures the resource to have a passive-delete relationship with resource_data and configures ondelete CASCADE so that the DB takes care of deleting the resource_data when a resource is deleted. This patch proposes the following changes: - When deleting a resource do not load resource_data. - When a resource is deleted, DB takes care of deleting resource_data. The resource data remains intact when the FK constraints are deleted and added to reflect on-delete cascade. Change-Id: I4162e6f9ad9582fcb304887e5d3718de220231ec Closes-Bug: #1573585
This commit is contained in:
parent
3b7731ddce
commit
ed8a086556
|
@ -0,0 +1,44 @@
|
|||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
# This is a placeholder for Liberty backports.
|
||||
# Do not use this number for new Mitaka work. New Mitaka work starts after
|
||||
# all the placeholders.
|
||||
|
||||
|
||||
import sqlalchemy
|
||||
|
||||
from migrate import ForeignKeyConstraint
|
||||
|
||||
|
||||
def upgrade(migrate_engine):
|
||||
meta = sqlalchemy.MetaData()
|
||||
meta.bind = migrate_engine
|
||||
|
||||
resource_data = sqlalchemy.Table('resource_data', meta, autoload=True)
|
||||
resource = sqlalchemy.Table('resource', meta, autoload=True)
|
||||
|
||||
for fk in resource_data.foreign_keys:
|
||||
if fk.column == resource.c.id:
|
||||
# delete the existing fk
|
||||
# and create with ondelete cascade and a proper name
|
||||
existing_fkey = ForeignKeyConstraint(
|
||||
columns=[resource_data.c.resource_id],
|
||||
refcolumns=[resource.c.id], name=fk.name)
|
||||
existing_fkey.drop()
|
||||
fkey = ForeignKeyConstraint(
|
||||
columns=[resource_data.c.resource_id],
|
||||
refcolumns=[resource.c.id],
|
||||
name="fk_resource_id", ondelete='CASCADE')
|
||||
fkey.create()
|
||||
break
|
|
@ -276,10 +276,11 @@ class ResourceData(BASE, HeatBase):
|
|||
value = sqlalchemy.Column('value', sqlalchemy.Text)
|
||||
redact = sqlalchemy.Column('redact', sqlalchemy.Boolean)
|
||||
decrypt_method = sqlalchemy.Column(sqlalchemy.String(64))
|
||||
resource_id = sqlalchemy.Column('resource_id',
|
||||
sqlalchemy.Integer,
|
||||
sqlalchemy.ForeignKey('resource.id'),
|
||||
nullable=False)
|
||||
resource_id = sqlalchemy.Column(
|
||||
'resource_id', sqlalchemy.Integer,
|
||||
sqlalchemy.ForeignKey(column='resource.id', name='fk_resource_id',
|
||||
ondelete='CASCADE'),
|
||||
nullable=False)
|
||||
|
||||
|
||||
class Resource(BASE, HeatBase, StateAware):
|
||||
|
@ -303,7 +304,8 @@ class Resource(BASE, HeatBase, StateAware):
|
|||
stack = relationship(Stack, backref=backref('resources'))
|
||||
root_stack_id = sqlalchemy.Column(sqlalchemy.String(36), index=True)
|
||||
data = relationship(ResourceData,
|
||||
cascade="all,delete",
|
||||
cascade="all",
|
||||
passive_deletes=True,
|
||||
backref=backref('resource'))
|
||||
|
||||
# Override timestamp column to store the correct value: it should be the
|
||||
|
|
|
@ -747,6 +747,19 @@ class HeatMigrationsCheckers(test_migrations.WalkVersionsMixin,
|
|||
self.assertIndexMembers(engine, 'stack', 'ix_stack_owner_id',
|
||||
['owner_id'])
|
||||
|
||||
def _check_073(self, engine, data):
|
||||
# check if column still exists and is not nullable.
|
||||
self.assertColumnIsNotNullable(engine, 'resource_data', 'resource_id')
|
||||
# Ensure that only one foreign key exists and is created as expected.
|
||||
inspector = sqlalchemy.engine.reflection.Inspector.from_engine(engine)
|
||||
resource_data_fkeys = inspector.get_foreign_keys('resource_data')
|
||||
self.assertEqual(1, len(resource_data_fkeys))
|
||||
fk = resource_data_fkeys[0]
|
||||
self.assertEqual('fk_resource_id', fk['name'])
|
||||
self.assertEqual(['resource_id'], fk['constrained_columns'])
|
||||
self.assertEqual('resource', fk['referred_table'])
|
||||
self.assertEqual(['id'], fk['referred_columns'])
|
||||
|
||||
|
||||
class TestHeatMigrationsMySQL(HeatMigrationsCheckers,
|
||||
test_base.MySQLOpportunisticTestCase):
|
||||
|
|
Loading…
Reference in New Issue