Remove remaining methods from db objects
- HeatBase.expire, not used anywhere - HeatBase.save_and_update, moved to sqlalchemy.api - SoftDelete.soft_delete moved to sqlalchemy.api update_and_save creates a transaction, so it needs to be in sqlalchemy.api so that a context manager can eventually manage the transaction. Change-Id: I84749f4fd0781ed9a2d62327b39ce6eee0f07b35
This commit is contained in:
parent
845075031a
commit
b125b81298
@ -114,6 +114,10 @@ def resource_update(context, resource_id, values, atomic_key,
|
||||
expected_engine_id)
|
||||
|
||||
|
||||
def resource_update_and_save(context, resource_id, values):
|
||||
return IMPL.resource_update_and_save(context, resource_id, values)
|
||||
|
||||
|
||||
def resource_create(context, values):
|
||||
return IMPL.resource_create(context, values)
|
||||
|
||||
|
@ -80,6 +80,17 @@ def get_backend():
|
||||
return sys.modules[__name__]
|
||||
|
||||
|
||||
def update_and_save(context, obj, values):
|
||||
with context.session.begin(subtransactions=True):
|
||||
for k, v in six.iteritems(values):
|
||||
setattr(obj, k, v)
|
||||
|
||||
|
||||
def delete_softly(context, obj):
|
||||
"""Mark this object as deleted."""
|
||||
update_and_save(context, obj, {'deleted_at': timeutils.utcnow()})
|
||||
|
||||
|
||||
def soft_delete_aware_query(context, *args, **kwargs):
|
||||
"""Stack query helper that accounts for context's `show_deleted` field.
|
||||
|
||||
@ -117,7 +128,7 @@ def raw_template_update(context, template_id, values):
|
||||
if getattr(raw_template_ref, k) != v)
|
||||
|
||||
if values:
|
||||
raw_template_ref.update_and_save(values)
|
||||
update_and_save(context, raw_template_ref, values)
|
||||
|
||||
return raw_template_ref
|
||||
|
||||
@ -224,6 +235,11 @@ def resource_update(context, resource_id, values, atomic_key,
|
||||
return bool(rows_updated)
|
||||
|
||||
|
||||
def resource_update_and_save(context, resource_id, values):
|
||||
resource = context.session.query(models.Resource).get(resource_id)
|
||||
update_and_save(context, resource, values)
|
||||
|
||||
|
||||
def resource_delete(context, resource_id):
|
||||
session = context.session
|
||||
with session.begin(subtransactions=True):
|
||||
@ -635,11 +651,11 @@ def stack_delete(context, stack_id):
|
||||
'%(id)s %(msg)s') % {
|
||||
'id': stack_id,
|
||||
'msg': 'that does not exist'})
|
||||
session = orm_session.Session.object_session(s)
|
||||
session = context.session
|
||||
with session.begin():
|
||||
for r in s.resources:
|
||||
session.delete(r)
|
||||
s.soft_delete(session=session)
|
||||
delete_softly(context, s)
|
||||
|
||||
|
||||
@oslo_db_api.wrap_db_retry(max_retries=3, retry_on_deadlock=True,
|
||||
@ -1046,7 +1062,7 @@ def software_deployment_get_all(context, server_id=None):
|
||||
|
||||
def software_deployment_update(context, deployment_id, values):
|
||||
deployment = software_deployment_get(context, deployment_id)
|
||||
deployment.update_and_save(values)
|
||||
update_and_save(context, deployment, values)
|
||||
return deployment
|
||||
|
||||
|
||||
@ -1121,10 +1137,10 @@ def service_update(context, service_id, values):
|
||||
|
||||
def service_delete(context, service_id, soft_delete=True):
|
||||
service = service_get(context, service_id)
|
||||
session = orm_session.Session.object_session(service)
|
||||
session = context.session
|
||||
with session.begin():
|
||||
if soft_delete:
|
||||
service.soft_delete(session=session)
|
||||
delete_softly(context, service)
|
||||
else:
|
||||
session.delete(service)
|
||||
|
||||
|
@ -17,54 +17,24 @@ import uuid
|
||||
|
||||
from oslo_db.sqlalchemy import models
|
||||
from oslo_utils import timeutils
|
||||
import six
|
||||
import sqlalchemy
|
||||
from sqlalchemy.ext import declarative
|
||||
from sqlalchemy.orm import backref
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.orm import session as orm_session
|
||||
|
||||
from heat.db.sqlalchemy import types
|
||||
|
||||
BASE = declarative.declarative_base()
|
||||
|
||||
|
||||
def get_session():
|
||||
from heat.db.sqlalchemy import api as db_api
|
||||
return db_api.get_session()
|
||||
|
||||
|
||||
class HeatBase(models.ModelBase, models.TimestampMixin):
|
||||
"""Base class for Heat Models."""
|
||||
__table_args__ = {'mysql_engine': 'InnoDB'}
|
||||
|
||||
def expire(self, session=None, attrs=None):
|
||||
"""Expire this object ()."""
|
||||
if not session:
|
||||
session = orm_session.Session.object_session(self)
|
||||
if not session:
|
||||
session = get_session()
|
||||
session.expire(self, attrs)
|
||||
|
||||
def update_and_save(self, values, session=None):
|
||||
if not session:
|
||||
session = orm_session.Session.object_session(self)
|
||||
if not session:
|
||||
session = get_session()
|
||||
session.begin(subtransactions=True)
|
||||
for k, v in six.iteritems(values):
|
||||
setattr(self, k, v)
|
||||
session.commit()
|
||||
|
||||
|
||||
class SoftDelete(object):
|
||||
deleted_at = sqlalchemy.Column(sqlalchemy.DateTime)
|
||||
|
||||
def soft_delete(self, session=None):
|
||||
"""Mark this object as deleted."""
|
||||
self.update_and_save({'deleted_at': timeutils.utcnow()},
|
||||
session=session)
|
||||
|
||||
|
||||
class StateAware(object):
|
||||
action = sqlalchemy.Column('action', sqlalchemy.String(255))
|
||||
|
@ -193,12 +193,10 @@ class Resource(
|
||||
|
||||
@classmethod
|
||||
def update_by_id(cls, context, resource_id, values):
|
||||
resource_db = db_api.resource_get(context, resource_id)
|
||||
resource_db.update_and_save(values)
|
||||
db_api.resource_update_and_save(context, resource_id, values)
|
||||
|
||||
def update_and_save(self, values):
|
||||
resource_db = db_api.resource_get(self._context, self.id)
|
||||
resource_db.update_and_save(values)
|
||||
db_api.resource_update_and_save(self._context, self.id, values)
|
||||
|
||||
def select_and_update(self, values, expected_engine_id=None,
|
||||
atomic_key=0):
|
||||
|
Loading…
Reference in New Issue
Block a user