diff --git a/neutron/db/model_base.py b/neutron/db/model_base.py deleted file mode 100644 index 7a9af24eb92..00000000000 --- a/neutron/db/model_base.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright (c) 2012 OpenStack Foundation. -# -# 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. - -from neutron_lib.db import model_base as lib_mb -import sqlalchemy as sa - -from neutron.common import _deprecate - - -_deprecate._moved_global('HasTenant', new_module=lib_mb, new_name='HasProject') - - -def get_unique_keys(model): - try: - constraints = model.__table__.constraints - except AttributeError: - constraints = [] - return [[c.name for c in constraint.columns] - for constraint in constraints - if isinstance(constraint, sa.UniqueConstraint)] - -# This shim is used to deprecate the old contents. -_deprecate._MovedGlobals(lib_mb) diff --git a/neutron/objects/base.py b/neutron/objects/base.py index 5f4e7858027..e39a29905f1 100644 --- a/neutron/objects/base.py +++ b/neutron/objects/base.py @@ -18,6 +18,7 @@ import itertools from neutron_lib import exceptions as n_exc from oslo_db import exception as obj_exc +from oslo_db.sqlalchemy import utils as db_utils from oslo_serialization import jsonutils from oslo_versionedobjects import base as obj_base from oslo_versionedobjects import fields as obj_fields @@ -26,7 +27,6 @@ import six from neutron._i18n import _ from neutron.api.v2 import attributes from neutron.db import api as db_api -from neutron.db import model_base from neutron.db import standard_attr from neutron.objects.db import api as obj_db_api from neutron.objects import exceptions as o_exc @@ -240,7 +240,8 @@ class DeclarativeObject(abc.ABCMeta): model_to_obj_translation = { v: k for (k, v) in cls.fields_need_translation.items()} - for model_unique_key in model_base.get_unique_keys(model): + keys = db_utils.get_unique_keys(model) or [] + for model_unique_key in keys: obj_unique_key = [model_to_obj_translation.get(key, key) for key in model_unique_key] if obj_field_names.issuperset(obj_unique_key): diff --git a/neutron/tests/unit/db/test_model_base.py b/neutron/tests/unit/db/test_model_base.py deleted file mode 100644 index 6fdef1b0b7d..00000000000 --- a/neutron/tests/unit/db/test_model_base.py +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright (c) 2016 Mirantis, 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. - -import mock -import sqlalchemy as sa - -from neutron.db import model_base -from neutron.tests import base as test_base - - -class GetUniqueKeysTestCase(test_base.BaseTestCase): - - def test_with_unique_constraints(self): - model = mock.Mock() - metadata = sa.MetaData() - model.__table__ = sa.Table( - "test_table", metadata, - sa.Column("a", sa.Integer, unique=True), - sa.Column("b", sa.Integer), - sa.Column("c", sa.Integer), - sa.Column("d", sa.Integer), - sa.UniqueConstraint("c", "d")) - expected = {("a",), ("c", "d")} - observed = {tuple(sorted(key)) for key in - model_base.get_unique_keys(model)} - self.assertEqual(expected, observed) - - def test_without_unique_constraints(self): - model = mock.Mock() - metadata = sa.MetaData() - model.__table__ = sa.Table( - "test_table", metadata, - sa.Column("a", sa.Integer), - sa.Column("b", sa.Integer)) - self.assertEqual([], model_base.get_unique_keys(model)) - - def test_not_a_model(self): - self.assertEqual([], model_base.get_unique_keys(None)) diff --git a/neutron/tests/unit/objects/test_base.py b/neutron/tests/unit/objects/test_base.py index 5156ef29871..a11f263a181 100644 --- a/neutron/tests/unit/objects/test_base.py +++ b/neutron/tests/unit/objects/test_base.py @@ -20,6 +20,7 @@ import mock import netaddr from neutron_lib import exceptions as n_exc from oslo_db import exception as obj_exc +from oslo_db.sqlalchemy import utils as db_utils from oslo_utils import timeutils from oslo_utils import uuidutils from oslo_versionedobjects import base as obj_base @@ -31,7 +32,6 @@ from neutron.common import constants from neutron.common import utils from neutron import context from neutron.db import db_base_plugin_v2 -from neutron.db import model_base from neutron.db.models import external_net as ext_net_model from neutron.db.models import l3 as l3_model from neutron.db import standard_attr @@ -661,7 +661,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): self.assertTrue(self._is_test_class(obj)) self._check_equal(self.objs[0], obj) get_object_mock.assert_called_once_with( - self.context, self._test_class.db_model, + mock.ANY, self._test_class.db_model, **self._test_class.modify_fields_to_db(obj_keys)) def _get_synthetic_fields_get_objects_calls(self, db_objs): @@ -1019,7 +1019,7 @@ class BaseDbObjectUniqueKeysTestCase(BaseObjectIfaceTestCase): class UniqueKeysTestCase(test_base.BaseTestCase): def test_class_creation(self): - m_get_unique_keys = mock.patch.object(model_base, 'get_unique_keys') + m_get_unique_keys = mock.patch.object(db_utils, 'get_unique_keys') with m_get_unique_keys as get_unique_keys: get_unique_keys.return_value = [['field1'], ['field2', 'db_field3']]