sqlalchemy: Use built-in declarative
sqlalchemy.ext.declarative was deprecated in sqlalchemy 1.4.0, due to the built-in implementations[1]. [1] https://github.com/sqlalchemy/sqlalchemy/commit/450f5c0d6519a439f40 Change-Id: I56f95f90f53e880f73a08b7eaaf853b35601abe4
This commit is contained in:
@@ -13,7 +13,6 @@
|
||||
from oslo_db.sqlalchemy import models
|
||||
from oslo_utils import uuidutils
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.ext import declarative
|
||||
from sqlalchemy import orm
|
||||
|
||||
from neutron_lib.db import constants as db_const
|
||||
@@ -32,7 +31,7 @@ class HasProject:
|
||||
def set_tenant_id(self, value):
|
||||
self.project_id = value
|
||||
|
||||
@declarative.declared_attr
|
||||
@orm.declared_attr
|
||||
def tenant_id(cls):
|
||||
return orm.synonym(
|
||||
'project_id',
|
||||
@@ -104,17 +103,11 @@ class _NeutronBase(models.ModelBase):
|
||||
|
||||
class NeutronBaseV2(_NeutronBase):
|
||||
|
||||
@declarative.declared_attr
|
||||
@orm.declared_attr
|
||||
def __tablename__(cls):
|
||||
# Use the pluralized name of the class as the table name.
|
||||
return cls.__name__.lower() + 's'
|
||||
|
||||
|
||||
try:
|
||||
# SQLAlchemy 2.0
|
||||
class BASEV2(orm.DeclarativeBase, NeutronBaseV2):
|
||||
pass
|
||||
except AttributeError:
|
||||
# SQLAlchemy < 2.0
|
||||
BASEV2 = declarative.declarative_base( # type: ignore[misc]
|
||||
cls=NeutronBaseV2)
|
||||
class BASEV2(orm.DeclarativeBase, NeutronBaseV2):
|
||||
pass
|
||||
|
||||
@@ -16,8 +16,8 @@ from oslo_utils import timeutils
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import event # noqa
|
||||
from sqlalchemy.ext.associationproxy import association_proxy
|
||||
from sqlalchemy.ext import declarative
|
||||
from sqlalchemy.orm import attributes
|
||||
from sqlalchemy.orm import declared_attr
|
||||
from sqlalchemy.orm import session as se
|
||||
|
||||
from neutron_lib._i18n import _
|
||||
@@ -138,7 +138,7 @@ class HasStandardAttributes:
|
||||
def validate_tag_support(cls):
|
||||
return getattr(cls, 'tag_support', False)
|
||||
|
||||
@declarative.declared_attr
|
||||
@declared_attr
|
||||
def standard_attr_id(cls):
|
||||
return sa.Column(
|
||||
sa.BigInteger().with_variant(sa.Integer(), 'sqlite'),
|
||||
@@ -150,7 +150,7 @@ class HasStandardAttributes:
|
||||
# NOTE(kevinbenton): we have to disable the following pylint check because
|
||||
# it thinks we are overriding this method in the __init__ method.
|
||||
# pylint: disable=method-hidden
|
||||
@declarative.declared_attr
|
||||
@declared_attr
|
||||
def standard_attr(cls):
|
||||
return sa.orm.relationship(StandardAttribute,
|
||||
lazy='joined',
|
||||
@@ -174,15 +174,15 @@ class HasStandardAttributes:
|
||||
self.standard_attr = StandardAttribute(
|
||||
resource_type=self.__tablename__, **standard_attr_kwargs)
|
||||
|
||||
@declarative.declared_attr
|
||||
@declared_attr
|
||||
def description(cls):
|
||||
return association_proxy('standard_attr', 'description')
|
||||
|
||||
@declarative.declared_attr
|
||||
@declared_attr
|
||||
def created_at(cls):
|
||||
return association_proxy('standard_attr', 'created_at')
|
||||
|
||||
@declarative.declared_attr
|
||||
@declared_attr
|
||||
def updated_at(cls):
|
||||
return association_proxy('standard_attr', 'updated_at')
|
||||
|
||||
@@ -193,7 +193,7 @@ class HasStandardAttributes:
|
||||
new_dict.pop('updated_at', None)
|
||||
super().update(new_dict)
|
||||
|
||||
@declarative.declared_attr
|
||||
@declared_attr
|
||||
def revision_number(cls):
|
||||
return association_proxy('standard_attr', 'revision_number')
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
|
||||
import gc
|
||||
|
||||
from sqlalchemy.ext import declarative
|
||||
from sqlalchemy import orm
|
||||
import testtools
|
||||
|
||||
@@ -30,17 +29,11 @@ class StandardAttrTestCase(base.BaseTestCase):
|
||||
def _make_decl_base(self):
|
||||
# construct a new base so we don't interfere with the main
|
||||
# base used in the sql test fixtures
|
||||
try:
|
||||
# SQLAlchemy 2.0
|
||||
class BaseV2(orm.DeclarativeBase,
|
||||
standard_attr.model_base.NeutronBaseV2):
|
||||
pass
|
||||
class BaseV2(orm.DeclarativeBase,
|
||||
standard_attr.model_base.NeutronBaseV2):
|
||||
pass
|
||||
|
||||
return BaseV2
|
||||
except AttributeError:
|
||||
# SQLAlchemy < 2.0
|
||||
return declarative.declarative_base(
|
||||
cls=standard_attr.model_base.NeutronBaseV2)
|
||||
return BaseV2
|
||||
|
||||
def test_standard_attr_resource_model_map(self):
|
||||
rs_map = standard_attr.get_standard_attr_resource_model_map()
|
||||
|
||||
@@ -15,7 +15,6 @@ from unittest import mock
|
||||
from oslo_config import cfg
|
||||
from oslo_db.sqlalchemy import models
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.ext import declarative
|
||||
from sqlalchemy import orm
|
||||
|
||||
from neutron_lib.api import attributes
|
||||
@@ -26,14 +25,8 @@ from neutron_lib import exceptions as n_exc
|
||||
from neutron_lib.tests import _base as base
|
||||
|
||||
|
||||
try:
|
||||
# SQLAlchemy 2.0
|
||||
class ModelBaseV2(orm.DeclarativeBase, models.ModelBase):
|
||||
pass
|
||||
except AttributeError:
|
||||
# SQLAlchemy < 2.0
|
||||
ModelBaseV2 = declarative.declarative_base( # type: ignore[misc]
|
||||
cls=models.ModelBase)
|
||||
class ModelBaseV2(orm.DeclarativeBase, models.ModelBase):
|
||||
pass
|
||||
|
||||
|
||||
class FakePort(ModelBaseV2):
|
||||
|
||||
Reference in New Issue
Block a user