From 189b7873835cd6cf4a0cf2efe5ef5d424c4fdc78 Mon Sep 17 00:00:00 2001 From: Chaolei Li Date: Fri, 2 Feb 2018 11:15:01 +0800 Subject: [PATCH] Set ondelete='CASCADE' on foreign key This commit actually do the samething as this patch: https://review.openstack.org/#/c/534225/ did. But this commit will update db after the patch "Some DB version is missing": https://review.openstack.org/#/c/536713/. In order to avoid the bug https://bugs.launchpad.net/zun/+bug/1744881. This commit changes the 'ForeignKey' attribute with ondelete='CASCADE' in ContainerAction and ContainerActionEvent model. Change-Id: Ib91cc31569a7a3ccc324f36626b4c186ba0b060c --- ...c965_add_ondelete_to_container_actions_.py | 47 +++++++++++++++++++ ...50f8_drop_container_actions_foreign_key.py | 46 ++++++++++++++++++ zun/db/sqlalchemy/models.py | 6 ++- 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 zun/db/sqlalchemy/alembic/versions/50829990c965_add_ondelete_to_container_actions_.py create mode 100644 zun/db/sqlalchemy/alembic/versions/fb9ad4a050f8_drop_container_actions_foreign_key.py diff --git a/zun/db/sqlalchemy/alembic/versions/50829990c965_add_ondelete_to_container_actions_.py b/zun/db/sqlalchemy/alembic/versions/50829990c965_add_ondelete_to_container_actions_.py new file mode 100644 index 000000000..21e566a64 --- /dev/null +++ b/zun/db/sqlalchemy/alembic/versions/50829990c965_add_ondelete_to_container_actions_.py @@ -0,0 +1,47 @@ +# 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. + +"""add ondelete to container_actions_events foreign key + +Revision ID: 50829990c965 +Revises: fb9ad4a050f8 +Create Date: 2018-02-02 03:58:14.384716 + +""" + +# revision identifiers, used by Alembic. +revision = '50829990c965' +down_revision = 'fb9ad4a050f8' +branch_labels = None +depends_on = None + +from alembic import op +from sqlalchemy.engine.reflection import Inspector as insp + +CONTAINER_ACTIONS_EVENTS = 'container_actions_events' +CONTAINER_ACTIONS = 'container_actions' + + +def upgrade(): + bind = op.get_bind() + + inspector = insp.from_engine(bind) + foreign_keys = inspector.get_foreign_keys(CONTAINER_ACTIONS_EVENTS) + + for foreign_key in foreign_keys: + if foreign_key.get('referred_table') == CONTAINER_ACTIONS: + op.drop_constraint(foreign_key.get('name'), + CONTAINER_ACTIONS_EVENTS, + type_="foreignkey") + op.create_foreign_key( + None, CONTAINER_ACTIONS_EVENTS, CONTAINER_ACTIONS, + ['action_id'], ['id'], ondelete='CASCADE') diff --git a/zun/db/sqlalchemy/alembic/versions/fb9ad4a050f8_drop_container_actions_foreign_key.py b/zun/db/sqlalchemy/alembic/versions/fb9ad4a050f8_drop_container_actions_foreign_key.py new file mode 100644 index 000000000..5af29cff9 --- /dev/null +++ b/zun/db/sqlalchemy/alembic/versions/fb9ad4a050f8_drop_container_actions_foreign_key.py @@ -0,0 +1,46 @@ +# 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. + +"""drop_container_actions_foreign_key + +Revision ID: fb9ad4a050f8 +Revises: 6ff4d35f4334 +Create Date: 2018-02-02 11:01:46.151429 + +""" + +# revision identifiers, used by Alembic. +revision = 'fb9ad4a050f8' +down_revision = '6ff4d35f4334' +branch_labels = None +depends_on = None + +from alembic import op +from sqlalchemy.engine.reflection import Inspector as insp + +CONTAINER_ACTIONS = 'container_actions' +CONTAINER = 'container' + + +def upgrade(): + bind = op.get_bind() + + inspector = insp.from_engine(bind) + foreign_keys = inspector.get_foreign_keys(CONTAINER_ACTIONS) + + for foreign_key in foreign_keys: + if foreign_key.get('referred_table') == CONTAINER: + op.drop_constraint(foreign_key.get('name'), CONTAINER_ACTIONS, + type_="foreignkey") + op.create_foreign_key( + None, CONTAINER_ACTIONS, CONTAINER, ['container_uuid'], + ['uuid'], ondelete='CASCADE') diff --git a/zun/db/sqlalchemy/models.py b/zun/db/sqlalchemy/models.py index ad6ddc491..d63d7abb9 100644 --- a/zun/db/sqlalchemy/models.py +++ b/zun/db/sqlalchemy/models.py @@ -422,7 +422,8 @@ class ContainerAction(Base): id = Column(Integer, primary_key=True, nullable=False) action = Column(String(255)) - container_uuid = Column(String(36), ForeignKey('container.uuid'), + container_uuid = Column(String(36), + ForeignKey('container.uuid', ondelete='CASCADE'), nullable=False) request_id = Column(String(255)) user_id = Column(String(255)) @@ -444,7 +445,8 @@ class ContainerActionEvent(Base): id = Column(Integer, primary_key=True, nullable=False) event = Column(String(255)) - action_id = Column(Integer, ForeignKey('container_actions.id'), + action_id = Column(Integer, + ForeignKey('container_actions.id', ondelete='CASCADE'), nullable=False) start_time = Column(DateTime, default=timeutils.utcnow) finish_time = Column(DateTime)