
Synced from commit 5fb343faee1442921e2d610b1a5222e67418c4cd. Due to changes in oslo.db API, this sync requires a bit more work on Neutron side. Sync includes the following commits: 5b7e61c Dispose db connections pool on disconnect d1988b9 Set sql_mode callback on connect instead of checkout a1a8280 Fix excessive logging from db.sqlalchemy.session 9933bdd Get mysql_sql_mode parameter from config 96a2217 Prevent incorrect usage of _wrap_db_error() 20a7510 Add from_config() method to EngineFacade fea119e Drop special case for MySQL traditional mode, update unit tests dda24eb Introduce mysql_sql_mode option, remove old warning 0b5af67 Introduce a method to set any MySQL session SQL mode 8dccc7b Handle ibm_db_sa DBDuplicateEntry integrity errors 5b9e9f4 Fix doc build errors in db.sqlalchemy ac84a40 Update log translation domains 86707cd Remove None for dict.get() 0545121 Fix duplicating of SQL queries in logs fcf517d Update oslo log messages with translation domains 630d395 Don't use cfg.CONF in oslo.db ce69e7f Don't store engine instances in oslo.db Change-Id: I0e1d86878d3eb924b01e04dced0f90b4e57757d8
120 lines
3.9 KiB
Python
120 lines
3.9 KiB
Python
# Copyright (c) 2011 X.commerce, a business unit of eBay Inc.
|
|
# Copyright 2010 United States Government as represented by the
|
|
# Administrator of the National Aeronautics and Space Administration.
|
|
# Copyright 2011 Piston Cloud Computing, Inc.
|
|
# Copyright 2012 Cloudscaling Group, Inc.
|
|
# All Rights Reserved.
|
|
#
|
|
# 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.
|
|
"""
|
|
SQLAlchemy models.
|
|
"""
|
|
|
|
import six
|
|
|
|
from sqlalchemy import Column, Integer
|
|
from sqlalchemy import DateTime
|
|
from sqlalchemy.orm import object_mapper
|
|
|
|
from neutron.openstack.common import timeutils
|
|
|
|
|
|
class ModelBase(six.Iterator):
|
|
"""Base class for models."""
|
|
__table_initialized__ = False
|
|
|
|
def save(self, session):
|
|
"""Save this object."""
|
|
|
|
# NOTE(boris-42): This part of code should be look like:
|
|
# session.add(self)
|
|
# session.flush()
|
|
# But there is a bug in sqlalchemy and eventlet that
|
|
# raises NoneType exception if there is no running
|
|
# transaction and rollback is called. As long as
|
|
# sqlalchemy has this bug we have to create transaction
|
|
# explicitly.
|
|
with session.begin(subtransactions=True):
|
|
session.add(self)
|
|
session.flush()
|
|
|
|
def __setitem__(self, key, value):
|
|
setattr(self, key, value)
|
|
|
|
def __getitem__(self, key):
|
|
return getattr(self, key)
|
|
|
|
def get(self, key, default=None):
|
|
return getattr(self, key, default)
|
|
|
|
@property
|
|
def _extra_keys(self):
|
|
"""Specifies custom fields
|
|
|
|
Subclasses can override this property to return a list
|
|
of custom fields that should be included in their dict
|
|
representation.
|
|
|
|
For reference check tests/db/sqlalchemy/test_models.py
|
|
"""
|
|
return []
|
|
|
|
def __iter__(self):
|
|
columns = dict(object_mapper(self).columns).keys()
|
|
# NOTE(russellb): Allow models to specify other keys that can be looked
|
|
# up, beyond the actual db columns. An example would be the 'name'
|
|
# property for an Instance.
|
|
columns.extend(self._extra_keys)
|
|
self._i = iter(columns)
|
|
return self
|
|
|
|
# In Python 3, __next__() has replaced next().
|
|
def __next__(self):
|
|
n = six.advance_iterator(self._i)
|
|
return n, getattr(self, n)
|
|
|
|
def next(self):
|
|
return self.__next__()
|
|
|
|
def update(self, values):
|
|
"""Make the model object behave like a dict."""
|
|
for k, v in six.iteritems(values):
|
|
setattr(self, k, v)
|
|
|
|
def iteritems(self):
|
|
"""Make the model object behave like a dict.
|
|
|
|
Includes attributes from joins.
|
|
"""
|
|
local = dict(self)
|
|
joined = dict([(k, v) for k, v in six.iteritems(self.__dict__)
|
|
if not k[0] == '_'])
|
|
local.update(joined)
|
|
return six.iteritems(local)
|
|
|
|
|
|
class TimestampMixin(object):
|
|
created_at = Column(DateTime, default=lambda: timeutils.utcnow())
|
|
updated_at = Column(DateTime, onupdate=lambda: timeutils.utcnow())
|
|
|
|
|
|
class SoftDeleteMixin(object):
|
|
deleted_at = Column(DateTime)
|
|
deleted = Column(Integer, default=0)
|
|
|
|
def soft_delete(self, session):
|
|
"""Mark this object as deleted."""
|
|
self.deleted = self.id
|
|
self.deleted_at = timeutils.utcnow()
|
|
self.save(session=session)
|