Skip to content

Commit

Permalink
Metadata: sqla2.0 update for 'meta_conventions'; Fix #5224
Browse files Browse the repository at this point in the history
  • Loading branch information
voetberg authored and bari12 committed Mar 7, 2024
1 parent ea765b7 commit e85183f
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions lib/rucio/core/meta_conventions.py
Expand Up @@ -16,6 +16,7 @@
from re import match
from typing import TYPE_CHECKING, Optional, Union

from sqlalchemy import select, and_
from sqlalchemy.exc import IntegrityError, NoResultFound

from rucio.common.constraints import AUTHORIZED_VALUE_TYPES
Expand Down Expand Up @@ -87,7 +88,8 @@ def del_key(key: str, *, session: "Session") -> None:
:param key: the name for the key.
:param session: The database session in use.
"""
session.query(models.DIDMetaConventionsKey).filter(key == key).delete()
statement = select(models.DIDMetaConventionsKey.key).where(models.DIDMetaConventionsKey.key == key)
session.delete(statement)


@read_session
Expand All @@ -100,9 +102,10 @@ def list_keys(*, session: "Session") -> list[str]:
:returns: A list containing all keys.
"""
key_list = []
query = session.query(models.DIDMetaConventionsKey)
statement = select(models.DIDMetaConventionsKey.key)
query = session.execute(statement).scalars()
for row in query:
key_list.append(row.key)
key_list.append(row)
return key_list


Expand Down Expand Up @@ -140,16 +143,21 @@ def add_value(key: str, value: str, *, session: "Session") -> None:

raise RucioException(error.args)

k = session.query(models.DIDMetaConventionsKey).filter_by(key=key).one()
statement = select(
models.DIDMetaConventionsKey,
).where(
models.DIDMetaConventionsKey.key == key
)
query = session.execute(statement).scalar_one()

# Check value against regexp, if defined
if k.value_regexp and not match(k.value_regexp, value):
raise InvalidValueForKey("The value '%s' for the key '%s' does not match the regular expression '%s'" % (value, key, k.value_regexp))
if query.value_regexp and not match(query.value_regexp, value):
raise InvalidValueForKey(f"The value {value} for the key {key} does not match the regular expression {query.value_regexp}")

# Check value type, if defined
type_map = dict([(str(t), t) for t in AUTHORIZED_VALUE_TYPES])
if k.value_type and not isinstance(value, type_map.get(k.value_type)): # type: ignore ; Typing error caused by 'isinstaince' not thinking types count as classes
raise InvalidValueForKey("The value '%s' for the key '%s' does not match the required type '%s'" % (value, key, k.value_type))
if query.value_type and not isinstance(value, type_map.get(query.value_type)): # type: ignore ; Typing error caused by 'isinstaince' not thinking types count as classes
raise InvalidValueForKey(f"The value {value} for the key {key} does not match the required type {query.value_type}")


@read_session
Expand All @@ -163,9 +171,10 @@ def list_values(key: str, *, session: "Session") -> list[str]:
:returns: A list containing all values.
"""
value_list = []
query = session.query(models.DIDMetaConventionsConstraints).filter_by(key=key)
statement = select(models.DIDMetaConventionsConstraints.value).where(models.DIDMetaConventionsConstraints.key == key)
query = session.execute(statement).scalars()
for row in query:
value_list.append(row.value)
value_list.append(row)
return value_list


Expand All @@ -184,10 +193,11 @@ def validate_meta(meta: dict, did_type: DIDType, *, session: "Session") -> None:
key = 'datatype'
if did_type == DIDType.DATASET and key in meta:
try:
session.query(models.DIDMetaConventionsConstraints.value).\
filter_by(key=key).\
filter_by(value=meta[key]).\
one()
statement = select(
models.DIDMetaConventionsConstraints.value
).where(
and_(models.DIDMetaConventionsConstraints.value == meta[key], models.DIDMetaConventionsConstraints.key == key)
)
session.execute(statement).one()
except NoResultFound:
print("The value '%s' for the key '%s' is not valid" % (meta[key], key))
raise InvalidObject("The value '%s' for the key '%s' is not valid" % (meta[key], key))
raise InvalidObject(f"The value {meta[key]}' for the key {key} is not valid")

0 comments on commit e85183f

Please sign in to comment.