5a5be875a0
Changes that were ported from oslo: b4f72b2 Don't raise MySQL 2013 'Lost connection' errors 271adfb Format sql in db.sqlalchemy.session docstring 0334cb3 Handle exception messages with six.text_type eff69ce Drop dependency on log from oslo db code 7a11a04 Automatic retry db.api query if db connection lost 11f2add Clean up docstring in db.sqlalchemy.session 1b5147f Only enable MySQL TRADITIONAL mode if we're running against MySQL 39e1c5c Move db tests base.py to common code 986dafd Fix parsing of UC errors in sqlite 3.7.16+/3.8.2+ 9a203e6 Use dialect rather than a particular DB API driver 1779029 Move helper DB functions to db.sqlalchemy.utils bcf6d5e Small edits on help strings ae01e9a Transition from migrate to alembic 70ebb19 Fix mocking of utcnow() for model datetime cols 7aa94df Add a db check for CHARSET=utf8 aff0171 Remove "vim: tabstop=4 shiftwidth=4 softtabstop=4" from headers fa0f36f Fix database connection string is secret 8575d87 Removed copyright from empty files d08d27f Fix the obsolete exception message 8b2b0b7 Use hacking import_exceptions for gettextutils._ 9bc593e Add docstring for exception handlers of session 855644a Removal of _REPOSITORY global variable. ea6caf9 Remove string.lowercase usage a33989e Remove eventlet tpool from common db.api e40903b Database hook enabling traditional mode at MySQL f2115a0 Replace xrange in for loop with range c802fa6 SQLAlchemy error patterns improved 1c1f199 Remove unused import 6d0a6c3 Correct invalid docstrings 135dd00 Remove start index 0 in range() 28f8fd5 Make _extra_keys a property of ModelBase 45658e2 Fix violations of H302:import only modules bb4d7a2 Enables db2 server disconnects to be handled pessimistically 915f8ab db.sqlalchemy.session add [sql].idle_timeout e6494c2 Use six.iteritems to make dict work on Python2/3 48cfb7b Drop dependency on processutils from oslo db code 4c47d3e Fix locking in migration tests c2ee282 Incorporating MIT licensed code c5a1088 Typos fix in db and periodic_task module fb0e86a Use six.moves.configparser instead of ConfigParser 1dd4971 fix typo in db session docstring 8a01dd8 The ability to run tests at various backend 0fe4e28 Use log.warning() instead of log.warn() in oslo.db 12bcdb7 Remove vim header 4c22556 Use py3kcompat urlutils functions instead of urlparse ca7a2ab Don't use deprecated module commands 6603e8f Remove sqlalchemy-migrate 0.7.3 patching 274c7e2 Drop dependency on lockutils from oslo db code 97d8cf4 Remove lazy loading of database backend 2251cb5 Do not name variables as builtins 3acd57c Add db2 communication error code when check the db connection c2dcf6e Add [sql].connection as deprecated opt for db 001729d Modify SQLA session due to dispose of eventlet c2dcf6e Add [sql].connection as deprecated opt for db 001729d Modify SQLA session due to dispose of eventlet 4de827a Clean up db.sqla.Models.extra_keys interface 347f29e Use functools.wrap() instead of custom implementation 771d843 Move base migration test classes to common code 9721129 exception: remove 56ff3b3 Use single meta when change column type 3f2f70e Helper function to sanitize db url credentials df3f2ba BaseException.message is deprecated since Python 2.6 c76be5b Add function drop_unique_constraint() d4d8126 Change sqlalchemy/utils.py mode back to 644 cf41936 Move sqlalchemy migration from Nova 5758360 Raise ValueError if sort_dir is unknown 31c1995 python3: Add python3 compatibility support 3972c3f Migrate sqlalchemy utils from Nova 1a2df89 Enable H302 hacking check 3f503fa Add a monkey-patching util for sqlalchemy-migrate 7ba5f4b Don't use mixture of cfg.Opt() deprecated args Change-Id: Ifddb91dc2c6b80668a07267f92f13074b974776b
118 lines
3.9 KiB
Python
118 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.db.sqlalchemy import session as sa
|
|
from neutron.openstack.common import timeutils
|
|
|
|
|
|
class ModelBase(object):
|
|
"""Base class for models."""
|
|
__table_initialized__ = False
|
|
|
|
def save(self, session=None):
|
|
"""Save this object."""
|
|
if not session:
|
|
session = sa.get_session()
|
|
# 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
|
|
|
|
def next(self):
|
|
n = six.advance_iterator(self._i)
|
|
return n, getattr(self, n)
|
|
|
|
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=None):
|
|
"""Mark this object as deleted."""
|
|
self.deleted = self.id
|
|
self.deleted_at = timeutils.utcnow()
|
|
self.save(session=session)
|