From 845075031a6a6fc720f4b5d587556aec65519f3c Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Tue, 21 Jun 2016 14:11:39 +1200 Subject: [PATCH] Remove the db object delete method The HeatBase.delete method starts its own (sub) transaction with one of three possible sessions. This change moves all delete calls to a sqlalchemy.api function with the current context session. This will help with bug #1479723 to always do deletes with a session provided by the context manager. Change-Id: I8dfd3bc6fdb44b0e3b06fab5d7dc8e06fa3d80a8 --- heat/db/api.py | 4 ++++ heat/db/sqlalchemy/api.py | 37 +++++++++++++++++++++++++----------- heat/db/sqlalchemy/models.py | 10 ---------- heat/objects/resource.py | 3 +-- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/heat/db/api.py b/heat/db/api.py index 899d83c3ec..9fbb9c7cb8 100644 --- a/heat/db/api.py +++ b/heat/db/api.py @@ -118,6 +118,10 @@ def resource_create(context, values): return IMPL.resource_create(context, values) +def resource_delete(context, resource_id): + return IMPL.resource_delete(context, resource_id) + + def resource_exchange_stacks(context, resource_id1, resource_id2): return IMPL.resource_exchange_stacks(context, resource_id1, resource_id2) diff --git a/heat/db/sqlalchemy/api.py b/heat/db/sqlalchemy/api.py index 0c27409107..55a5f1ada7 100644 --- a/heat/db/sqlalchemy/api.py +++ b/heat/db/sqlalchemy/api.py @@ -125,14 +125,17 @@ def raw_template_update(context, template_id, values): def raw_template_delete(context, template_id): raw_template = raw_template_get(context, template_id) raw_tmpl_files_id = raw_template.files_id - raw_template.delete() - if raw_tmpl_files_id is None: - return - # If no other raw_template is referencing the same raw_template_files, - # delete that too - if context.session.query(models.RawTemplate).filter_by( - files_id=raw_tmpl_files_id).first() is None: - raw_template_files_get(context, raw_tmpl_files_id).delete() + session = context.session + with session.begin(subtransactions=True): + session.delete(raw_template) + if raw_tmpl_files_id is None: + return + # If no other raw_template is referencing the same raw_template_files, + # delete that too + if session.query(models.RawTemplate).filter_by( + files_id=raw_tmpl_files_id).first() is None: + raw_tmpl_files = raw_template_files_get(context, raw_tmpl_files_id) + session.delete(raw_tmpl_files) def raw_template_files_create(context, values): @@ -221,6 +224,14 @@ def resource_update(context, resource_id, values, atomic_key, return bool(rows_updated) +def resource_delete(context, resource_id): + session = context.session + with session.begin(subtransactions=True): + resource = session.query(models.Resource).get(resource_id) + if resource: + session.delete(resource) + + def resource_data_get_all(context, resource_id, data=None): """Looks up resource_data by resource.id. @@ -276,7 +287,7 @@ def stack_tags_delete(context, stack_id): result = stack_tags_get(context, stack_id) if result: for tag in result: - tag.delete() + session.delete(tag) def stack_tags_get(context, stack_id): @@ -334,7 +345,9 @@ def resource_exchange_stacks(context, resource_id1, resource_id2): def resource_data_delete(context, resource_id, key): result = resource_data_get_by_key(context, resource_id, key) - result.delete() + session = context.session + with session.begin(subtransactions=True): + session.delete(result) def resource_create(context, values): @@ -1039,7 +1052,9 @@ def software_deployment_update(context, deployment_id, values): def software_deployment_delete(context, deployment_id): deployment = software_deployment_get(context, deployment_id) - deployment.delete() + session = context.session + with session.begin(subtransactions=True): + session.delete(deployment) def snapshot_create(context, values): diff --git a/heat/db/sqlalchemy/models.py b/heat/db/sqlalchemy/models.py index dd6d38ff66..3b360f7df1 100644 --- a/heat/db/sqlalchemy/models.py +++ b/heat/db/sqlalchemy/models.py @@ -46,16 +46,6 @@ class HeatBase(models.ModelBase, models.TimestampMixin): session = get_session() session.expire(self, attrs) - def delete(self, session=None): - """Delete this object.""" - if not session: - session = orm_session.Session.object_session(self) - if not session: - session = get_session() - session.begin(subtransactions=True) - session.delete(self) - session.commit() - def update_and_save(self, values, session=None): if not session: session = orm_session.Session.object_session(self) diff --git a/heat/objects/resource.py b/heat/objects/resource.py index 656e221242..9b632a4828 100644 --- a/heat/objects/resource.py +++ b/heat/objects/resource.py @@ -125,8 +125,7 @@ class Resource( @classmethod def delete(cls, context, resource_id): - resource_db = db_api.resource_get(context, resource_id) - resource_db.delete() + db_api.resource_delete(context, resource_id) @classmethod def exchange_stacks(cls, context, resource_id1, resource_id2):