db: Replace use of reverse cascades

Resolve the following RemovedIn20Warning warning:

  "SecretStoreMetadatum" object is being merged into a Session along the
  backref cascade path for relationship "Secret.secret_store_metadata";
  in SQLAlchemy 2.0, this reverse cascade will not take place.  Set
  cascade_backrefs to False in either the relationship() or backref()
  function for the 2.0 behavior; or to set globally for the whole
  Session, set the future=True flag

In effect, this means if you have a model that refers to another model,
creating/saving the former will no longer create/save the latter. We
have only one instance of this error - the error message above - and in
our case we are explicitly saving the 'Secret' instance before saving
the 'SecretStoreMetadatum' instance. As such, we can opt-in to the 2.0
behavior with no further changes. We do this for all relationships to be
safe.

More information on this issue can be found at [1].

[1] https://groups.google.com/g/sqlalchemy/c/VoY-qEiJA3U?pli=1

Change-Id: I4b4fa4c224113863643e16153478183447796146
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2023-06-15 15:16:30 -07:00
parent 059eaaa7b3
commit bb84f2b706
2 changed files with 20 additions and 17 deletions

View File

@ -307,18 +307,21 @@ class Secret(BASE, SoftDeleteMixIn, ModelBase):
"SecretStoreMetadatum",
collection_class=col.attribute_mapped_collection('key'),
backref="secret",
cascade="all, delete-orphan")
cascade="all, delete-orphan",
cascade_backrefs=False)
secret_user_metadata = orm.relationship(
"SecretUserMetadatum",
collection_class=col.attribute_mapped_collection('key'),
backref="secret",
cascade="all, delete-orphan")
cascade="all, delete-orphan",
cascade_backrefs=False)
consumers = orm.relationship(
"SecretConsumerMetadatum",
backref="secret",
cascade="all, delete-orphan")
cascade="all, delete-orphan",
cascade_backrefs=False)
def __init__(self, parsed_request=None, check_exc=True):
"""Creates secret from a dict."""
@ -573,13 +576,15 @@ class Order(BASE, SoftDeleteMixIn, ModelBase):
"OrderPluginMetadatum",
collection_class=col.attribute_mapped_collection('key'),
backref="order",
cascade="all, delete-orphan")
cascade="all, delete-orphan",
cascade_backrefs=False)
order_barbican_metadata = orm.relationship(
"OrderBarbicanMetadatum",
collection_class=col.attribute_mapped_collection('key'),
backref="order",
cascade="all, delete-orphan")
cascade="all, delete-orphan",
cascade_backrefs=False)
def __init__(self, parsed_request=None, check_exc=True):
"""Creates a Order entity from a dict."""
@ -910,7 +915,8 @@ class CertificateAuthority(BASE, ModelBase):
'CertificateAuthorityMetadatum',
collection_class=col.attribute_mapped_collection('key'),
backref="ca",
cascade="all, delete-orphan"
cascade="all, delete-orphan",
cascade_backrefs=False,
)
def __init__(self, parsed_ca_in=None, check_exc=True):
@ -1134,8 +1140,10 @@ class SecretACL(BASE, ModelBase):
'Secret', backref=orm.backref('secret_acls', lazy=False))
acl_users = orm.relationship(
'SecretACLUser', backref=orm.backref('secret_acl', lazy=False),
cascade="all, delete-orphan")
'SecretACLUser',
backref=orm.backref('secret_acl', lazy=False),
cascade="all, delete-orphan",
cascade_backrefs=False)
__table_args__ = (sa.UniqueConstraint(
'secret_id', 'operation', name='_secret_acl_operation_uc'),)
@ -1210,8 +1218,10 @@ class ContainerACL(BASE, ModelBase):
'Container', backref=orm.backref('container_acls', lazy=False))
acl_users = orm.relationship(
'ContainerACLUser', backref=orm.backref('container_acl', lazy=False),
cascade="all, delete-orphan")
'ContainerACLUser',
backref=orm.backref('container_acl', lazy=False),
cascade="all, delete-orphan",
cascade_backrefs=False)
__table_args__ = (sa.UniqueConstraint(
'container_id', 'operation', name='_container_acl_operation_uc'),)

View File

@ -190,13 +190,6 @@ class WarningsFixture(fixtures.Fixture):
category=sqla_exc.SADeprecationWarning,
)
warnings.filterwarnings(
'ignore',
module='barbican',
message=r'".*" object is being merged into a Session along .*',
category=sqla_exc.SADeprecationWarning,
)
warnings.filterwarnings(
'ignore',
module='barbican',