Merge "Remove remaining methods from db objects"

This commit is contained in:
Jenkins 2016-07-19 09:05:53 +00:00 committed by Gerrit Code Review
commit f9b47c61c3
4 changed files with 28 additions and 40 deletions

View File

@ -114,6 +114,10 @@ def resource_update(context, resource_id, values, atomic_key,
expected_engine_id) 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): def resource_create(context, values):
return IMPL.resource_create(context, values) return IMPL.resource_create(context, values)

View File

@ -80,6 +80,17 @@ def get_backend():
return sys.modules[__name__] 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): def soft_delete_aware_query(context, *args, **kwargs):
"""Stack query helper that accounts for context's `show_deleted` field. """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 getattr(raw_template_ref, k) != v)
if values: if values:
raw_template_ref.update_and_save(values) update_and_save(context, raw_template_ref, values)
return raw_template_ref return raw_template_ref
@ -224,6 +235,11 @@ def resource_update(context, resource_id, values, atomic_key,
return bool(rows_updated) 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): def resource_delete(context, resource_id):
session = context.session session = context.session
with session.begin(subtransactions=True): with session.begin(subtransactions=True):
@ -628,11 +644,11 @@ def stack_delete(context, stack_id):
'%(id)s %(msg)s') % { '%(id)s %(msg)s') % {
'id': stack_id, 'id': stack_id,
'msg': 'that does not exist'}) 'msg': 'that does not exist'})
session = orm_session.Session.object_session(s) session = context.session
with session.begin(): with session.begin():
for r in s.resources: for r in s.resources:
session.delete(r) 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, @oslo_db_api.wrap_db_retry(max_retries=3, retry_on_deadlock=True,
@ -1039,7 +1055,7 @@ def software_deployment_get_all(context, server_id=None):
def software_deployment_update(context, deployment_id, values): def software_deployment_update(context, deployment_id, values):
deployment = software_deployment_get(context, deployment_id) deployment = software_deployment_get(context, deployment_id)
deployment.update_and_save(values) update_and_save(context, deployment, values)
return deployment return deployment
@ -1114,10 +1130,10 @@ def service_update(context, service_id, values):
def service_delete(context, service_id, soft_delete=True): def service_delete(context, service_id, soft_delete=True):
service = service_get(context, service_id) service = service_get(context, service_id)
session = orm_session.Session.object_session(service) session = context.session
with session.begin(): with session.begin():
if soft_delete: if soft_delete:
service.soft_delete(session=session) delete_softly(context, service)
else: else:
session.delete(service) session.delete(service)

View File

@ -17,54 +17,24 @@ import uuid
from oslo_db.sqlalchemy import models from oslo_db.sqlalchemy import models
from oslo_utils import timeutils from oslo_utils import timeutils
import six
import sqlalchemy import sqlalchemy
from sqlalchemy.ext import declarative from sqlalchemy.ext import declarative
from sqlalchemy.orm import backref from sqlalchemy.orm import backref
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.orm import session as orm_session
from heat.db.sqlalchemy import types from heat.db.sqlalchemy import types
BASE = declarative.declarative_base() 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): class HeatBase(models.ModelBase, models.TimestampMixin):
"""Base class for Heat Models.""" """Base class for Heat Models."""
__table_args__ = {'mysql_engine': 'InnoDB'} __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): class SoftDelete(object):
deleted_at = sqlalchemy.Column(sqlalchemy.DateTime) 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): class StateAware(object):
action = sqlalchemy.Column('action', sqlalchemy.String(255)) action = sqlalchemy.Column('action', sqlalchemy.String(255))

View File

@ -193,12 +193,10 @@ class Resource(
@classmethod @classmethod
def update_by_id(cls, context, resource_id, values): def update_by_id(cls, context, resource_id, values):
resource_db = db_api.resource_get(context, resource_id) db_api.resource_update_and_save(context, resource_id, values)
resource_db.update_and_save(values)
def update_and_save(self, values): def update_and_save(self, values):
resource_db = db_api.resource_get(self._context, self.id) db_api.resource_update_and_save(self._context, self.id, values)
resource_db.update_and_save(values)
def select_and_update(self, values, expected_engine_id=None, def select_and_update(self, values, expected_engine_id=None,
atomic_key=0): atomic_key=0):