Source code for scim2_sqlalchemy.storage

import datetime
from collections.abc import AsyncGenerator
from collections.abc import Awaitable
from collections.abc import Callable
from collections.abc import Generator
from collections.abc import Mapping
from collections.abc import Sequence
from contextlib import asynccontextmanager
from contextlib import contextmanager
from typing import TYPE_CHECKING
from typing import Any

from scim2_models import ConflictException
from scim2_models import NotFoundException
from scim2_models import PreconditionFailedException
from scim2_models import Resource
from scim2_models import ResourceType
from scim2_models import ResponseParameters
from scim2_models import ScimPolicy
from scim2_models import ScimProvider
from scim2_models import SearchRequest
from scim2_models import UniquenessException
from scim2_server.keyset import Bound
from scim2_server.keyset import Position
from scim2_server.keyset import keyset_page
from scim2_server.keyset import page_rows
from scim2_server.mapping import Collection
from scim2_server.mapping.conversion import assign_links
from scim2_server.mapping.conversion import check_storable
from scim2_server.mapping.conversion import dates
from scim2_server.mapping.conversion import link_entries
from scim2_server.storage import AsyncScimStorage
from scim2_server.storage import ScimStorage
from scim2_server.storage import SearchPage
from scim2_server.storage import uniqueness_conflict
from sqlalchemy import Dialect
from sqlalchemy import Table
from sqlalchemy import inspect
from sqlalchemy.exc import DBAPIError
from sqlalchemy.orm import Session
from sqlalchemy.orm.exc import StaleDataError

from .comparison import _Comparator
from .comparison import _comparator
from .conversion import _from_scim
from .conversion import _to_scim
from .mapping import ResourceMapping
from .mapping import _as_key
from .mapping import _Column
from .query import _links_statement
from .query import _load_statement
from .query import _records_statement
from .query import _root_statements
from .query import _search_statements

if TYPE_CHECKING:
    from sqlalchemy.ext.asyncio import AsyncSession


def _utcnow() -> datetime.datetime:
    """Return the current time, in UTC."""
    return datetime.datetime.now(datetime.UTC)


def _expire(
    session: "Session | AsyncSession", mapping: ResourceMapping, resource_id: str | None
) -> None:
    """Expire the record of an identifier when the session holds it, so that loading it reads the database again.

    Only this record is read again: a record linking to itself, such as a
    group holding itself, is then loaded once, with all its collections.
    """
    key = _as_key(mapping._id, resource_id)
    if key is None:
        return
    record = session.identity_map.get(session.identity_key(mapping.record, key))
    if record is not None:
        session.expire(record)


def _has_text_id(id_column: _Column, record: Any, value: str | None) -> bool:
    """Tell whether a record found by a string identifier has exactly this identifier.

    A collation of the database, such as the default one of MariaDB, can
    ignore the case and the trailing spaces of a string.
    """
    key = _as_key(id_column, value)
    return not isinstance(key, str) or getattr(record, id_column.key) == key


def _load_position(
    position: Any, search_request: SearchRequest[Any], type_count: int
) -> Position | None:
    """Read the position of a cursor page, or return None for the first page.

    At the root, the position holds the index of a resource type.
    """
    return Position.load(
        position,
        has_sort=search_request.sort_by is not None,
        type_count=type_count if type_count > 1 else None,
    )


def _by_position(rows: Sequence[Any]) -> dict[int, list[str]]:
    """Group the identifiers of a page at the root by position of their resource type."""
    ids: dict[int, list[str]] = {}
    for position, record_id, _ in rows:
        ids.setdefault(position, []).append(record_id)
    return ids


_UNIQUE_VIOLATIONS = {
    "23505",
    "SQLITE_CONSTRAINT_UNIQUE",
    "SQLITE_CONSTRAINT_PRIMARYKEY",
    1062,
}
"""The codes of a unique value already taken: the SQLSTATE of PostgreSQL, the
name of the SQLite error, and the number of the MariaDB error."""

_FOREIGN_KEY_VIOLATIONS = {
    "23503",
    "SQLITE_CONSTRAINT_FOREIGNKEY",
    1216,
    1217,
    1451,
    1452,
}
"""The codes of a foreign key the change breaks, such as a record still
referring to a deleted one, as _UNIQUE_VIOLATIONS gives them."""

_CHANGED_MEANWHILE = 1020
"""The number of the MariaDB error on a record another transaction changed since it was read."""

_SAVEPOINT_GONE = 1305
"""The number of the MariaDB error on a savepoint that no longer exists."""


def _error_code(error: BaseException | None) -> Any:
    """Return the code of an error of a database driver.

    The MariaDB drivers give the number of the error, in errno or first in
    args, and a SQLSTATE too generic to tell a unique value taken.
    """
    errno = getattr(error, "errno", None)
    if isinstance(errno, int):
        return errno
    args = getattr(error, "args", ())
    if args and isinstance(args[0], int):
        return args[0]
    for attribute in ("sqlstate", "pgcode", "sqlite_errorname"):
        code = getattr(error, attribute, None)
        if code is not None:
            return code
    return None


def _translate(exception: DBAPIError) -> None:
    """Raise the SCIM error of a database error: 409 for a foreign key broken, 412 for a record changed meanwhile.

    MariaDB rolls back the whole transaction on a record changed meanwhile.
    Rolling back to the savepoint then fails, and its error holds the first
    one as its context.
    """
    code = _error_code(exception.orig)
    if code == _SAVEPOINT_GONE and isinstance(exception.__context__, DBAPIError):
        code = _error_code(exception.__context__.orig)
    if code in _FOREIGN_KEY_VIOLATIONS:
        raise ConflictException(
            detail="Another record refers to this resource, or it refers to a missing one"
        ) from exception
    if code == _CHANGED_MEANWHILE:
        raise PreconditionFailedException from exception


def _violation(error: Any) -> tuple[str | None, str | None, list[str]]:
    """Return the table, the constraint and the columns of a unique violation, as far as the database tells them.

    PostgreSQL tells the table and the constraint. MariaDB tells the
    constraint at the end of its message, after the value, and MySQL
    prefixes it with the table. SQLite tells the columns, or the index of an
    expression. Another driver tells nothing the storage reads.
    """
    diag = getattr(error, "diag", None)
    if diag is not None:
        return diag.table_name, diag.constraint_name, []
    message = str(error.args[-1])
    if isinstance(_error_code(error), int):
        key = message.rpartition(" for key '")[2].removesuffix("'")
        table, _, name = key.rpartition(".")
        return table or None, name, []
    if getattr(error, "sqlite_errorname", None) is None:
        return None, None, []
    failed = message.partition(": ")[2]
    if failed.startswith("index '"):
        return None, failed.removeprefix("index '").removesuffix("'"), []
    columns = [column.strip().partition(".") for column in failed.split(",")]
    return columns[0][0], None, [column for _, _, column in columns]


def _constraint_columns(inspector: Any, table: Table, name: str) -> list[str]:
    """Return the columns of a unique constraint, a unique index or the primary key of a table, by its name."""
    constraints = [
        *inspector.get_unique_constraints(table.name, schema=table.schema),
        *(
            index
            for index in inspector.get_indexes(table.name, schema=table.schema)
            if index["unique"]
        ),
    ]
    for constraint in constraints:
        if constraint["name"] == name:
            return list(constraint["column_names"])
    primary = inspector.get_pk_constraint(table.name, schema=table.schema)
    if name in (primary["name"], "PRIMARY"):
        return list(primary["constrained_columns"])
    return []


def _violated_columns(
    connection: Any, error: Any, tables: list[Table], record: Table | None
) -> list[tuple[Table, str]]:
    """Return the table and the name of each column of the constraint a unique violation breaks, or nothing when it is unclear.

    MariaDB names the constraint without its table, and two tables may have
    a constraint of the same name, such as PRIMARY: the table of the record
    comes first, as the database numbers the entries of the other tables.
    """
    table_name, name, columns = _violation(error)
    if columns:
        return [
            (table, column)
            for table in tables
            if table.name == table_name
            for column in columns
        ]
    if name is None:
        return []
    inspector = inspect(connection)
    matches = [
        (table, found)
        for table in tables
        if table_name in (None, table.name)
        for found in [_constraint_columns(inspector, table, name)]
        if found
    ]
    if len(matches) > 1:
        matches = [(table, found) for table, found in matches if table is record]
    if len(matches) != 1:
        return []
    table, found = matches[0]
    return [(table, column) for column in found]


class _StorageBase:
    """What the synchronous and asynchronous storages share: everything but the queries."""

    def __init__(
        self,
        mappings: Mapping[str, ResourceMapping],
        provider: ScimProvider | None,
        version_key: bytes | None,
    ) -> None:
        self.mappings = dict(mappings)
        self.provider = provider
        self.version_key = version_key
        self._policy = provider.policy if provider else ScimPolicy()
        self._comparators: dict[str, _Comparator] = {}
        self._endpoints = {
            str(resource_type.name): str(resource_type.endpoint).strip("/")
            for resource_type in (provider.resource_types if provider else ())
        }
        for resource_type in provider.resource_types if provider else ():
            if resource_type.name not in self.mappings:
                raise ValueError(
                    f"No mapping for the resource type {resource_type.name!r}"
                )
        for name, mapping in self.mappings.items():
            if mapping.version is None and not version_key:
                raise ValueError(
                    f"The mapping of {name!r} has no version, so the storage "
                    "needs a version_key to hash the resources"
                )
        links = [mapping._targets(self.mappings) for mapping in self.mappings.values()]
        for mapping, targets in zip(self.mappings.values(), links, strict=True):
            mapping._bind(targets)
        for mapping in self.mappings.values():
            mapping._check_constraints()

    @staticmethod
    def _conflict(
        attributes: dict[tuple[Table, str], Any], columns: list[tuple[Table, str]]
    ) -> UniquenessException:
        """Return the 409 of a unique violation, telling the attribute when a single column of its constraint holds it."""
        if len(columns) == 1 and columns[0] in attributes:
            return uniqueness_conflict(attributes[columns[0]])
        return UniquenessException()

    def _mapping(self, resource_type: ResourceType) -> ResourceMapping:
        """Return the mapping of a resource type."""
        return self.mappings[resource_type.name]  # type: ignore[index]

    def _root_page(
        self,
        resource_types: list[ResourceType],
        rows: Sequence[Any],
        records: dict[tuple[int, str], Any],
        search_request: SearchRequest[Any],
    ) -> list[Resource[Any]]:
        """Build the resources of a page at the root, in the order of the page.

        records holds the loaded records by position of their resource type
        and identifier as text. A record deleted after the page was selected
        is left out.
        """
        return [
            self._to_scim(
                resource_types[position], records[position, record_id], search_request
            )
            for position, record_id, _ in rows
            if (position, record_id) in records
        ]

    def _root_search_page(
        self,
        resource_types: list[ResourceType],
        total: int,
        rows: list[Any],
        more: bool,
        records: dict[tuple[int, str], Any],
        search_request: SearchRequest[Any],
        anchor: Position | None,
    ) -> SearchPage:
        """Build the page of a search at the root, from its rows and the records they load."""
        resources = self._root_page(resource_types, rows, records, search_request)
        if search_request.cursor is None:
            return SearchPage(total, resources)
        if not rows:
            return keyset_page(total, resources, None, None, more, anchor)
        first, last = (
            Bound(key, record_id, type_position)
            for type_position, record_id, key in (rows[0], rows[-1])
        )
        return keyset_page(total, resources, first, last, more, anchor)

    def _page(
        self,
        resource_type: ResourceType,
        total: int,
        rows: Sequence[Any],
        search_request: SearchRequest[Any],
        anchor: Position | None,
    ) -> SearchPage:
        """Build the page of a search on one resource type, from its records with their sort value and identifier."""
        page, more = page_rows(rows, search_request, anchor)
        resources = [
            self._to_scim(resource_type, record, search_request)
            for record, _, _ in page
        ]
        if search_request.cursor is None:
            return SearchPage(total, resources)
        if not page:
            return keyset_page(total, resources, None, None, more, anchor)
        first, last = (
            Bound(key, record_id) for _, key, record_id in (page[0], page[-1])
        )
        return keyset_page(total, resources, first, last, more, anchor)

    def _model(self, resource_type: ResourceType) -> type[Resource[Any]]:
        """Return the model of the resources of a resource type: the one of the provider, if any."""
        model = self.provider.model_for(resource_type) if self.provider else None
        return model or self._mapping(resource_type).model  # type: ignore[return-value]

    def _to_scim(
        self,
        resource_type: ResourceType,
        record: Any,
        parameters: ResponseParameters[Any] | None = None,
    ) -> Resource[Any]:
        """Build the resource a record stores, with the collections the response keeps."""
        mapping = self._mapping(resource_type)
        model = self._model(resource_type)
        collections = mapping._returned_collections(parameters)
        return _to_scim(
            mapping,
            record,
            resource_type.name,  # type: ignore[arg-type]
            model,
            self._endpoints,
            collections,
            self.version_key,
        )

    def _check_storable(
        self,
        resource_type: ResourceType,
        resource: Resource[Any],
        stored: Resource[Any] | None,
    ) -> None:
        """Refuse a change the mapping cannot store. stored is the stored resource, None for a new one."""
        check_storable(self._mapping(resource_type), resource, stored)

    @staticmethod
    def _not_found(
        resource_type: ResourceType, resource_id: str | None
    ) -> NotFoundException:
        """Return the error of a resource that does not exist."""
        return NotFoundException(
            detail=f"{resource_type.name} {resource_id!r} not found"
        )

    def _check_version(
        self,
        resource_type: ResourceType,
        record: Any,
        expected_version: str | None,
        stored: Resource[Any] | None = None,
    ) -> None:
        """Refuse to change a record whose version is not the expected one.

        A version counter is read from the record. A hash is the one of the
        stored resource, built from the record when not given, all of its
        collections loaded.
        """
        if expected_version is None:
            return
        mapping = self._mapping(resource_type)
        if mapping.version is not None:
            version = f'W/"{mapping._counter(record)}"'
        else:
            stored = stored or self._to_scim(resource_type, record)
            version = stored.meta.version  # type: ignore[union-attr,assignment]
        if version != expected_version:
            raise PreconditionFailedException

    def _comparator(self, dialect: Dialect) -> _Comparator:
        """Return how a database compares strings, built once for each dialect."""
        if dialect.name not in self._comparators:
            self._comparators[dialect.name] = _comparator(
                dialect, self._policy, self.mappings.values()
            )
        return self._comparators[dialect.name]

    @staticmethod
    def _clear_entries(mapping: ResourceMapping, record: Any) -> bool:
        """Remove the entries a record owns before an update, and tell whether it had any.

        The session inserts the new entries before it deletes the old ones.
        Flushing the removal first lets a unique index of the entries accept a
        value the resource keeps.
        """
        cleared = False
        for collection in mapping._all_collections():
            key = collection.relation.key
            if collection.owned and collection.writable and getattr(record, key):
                setattr(record, key, [])
                cleared = True
        return cleared

    def _new_record(self, resource_type: ResourceType, resource_id: str | None) -> Any:
        """Build an empty record for a new resource, with its identifier when it is given, and its dates.

        The resource is written once the record is in the session, so that the
        records it links to do not pull a record the session does not know.
        """
        mapping = self._mapping(resource_type)
        record = mapping.record()
        if resource_id is not None:
            setattr(record, mapping._id.key, mapping._new_key(resource_id))
        for column, now in dates(mapping, _utcnow(), creating=True):
            setattr(record, column.key, now)
        return record

    def _write_record(
        self,
        mapping: ResourceMapping,
        resource: Resource[Any],
        record: Any,
        links: dict[Collection, list[Any]],
    ) -> None:
        """Write a resource in its record, and date the write.

        The date updates the record even when only links change, such as the
        members of a group, so that its version counter changes too. Without
        date, the storage counts the write in the version itself.
        """
        _from_scim(mapping, resource, record, links, creating=False)
        for column, now in dates(mapping, _utcnow(), creating=False):
            setattr(record, column.key, now)
        if mapping.version is not None and mapping._last_modified is None:
            version = mapping.version.key
            generate = inspect(mapping.record).version_id_generator
            setattr(record, version, generate(getattr(record, version)))


[docs] class SqlAlchemyStorage(_StorageBase, ScimStorage): """Store SCIM resources in the records of SQLAlchemy models. The storage never commits: committing the request is left to the application. Each write is :ref:`flushed <sqlalchemy:session_flushing>`, so that the database fills the identifiers and checks its constraints. A change the mappings cannot store raises an error rather than being lost: :class:`~scim2_models.InvalidValueException` for an attribute no mapping stores, and :class:`~scim2_models.MutabilityException` for an attribute the mapping only reads. A value equal to the stored one is accepted, so a resource read and sent back can always be written. :param mappings: The mapping of each resource type, by resource type name. :param session: Return the current :class:`~sqlalchemy.orm.Session`, such as a :class:`~sqlalchemy.orm.scoped_session`. :param provider: The provider of the server. The storage then returns instances of its models, rather than of the models of the mappings, and gives each link a ``$ref`` relative to the SCIM root, from the endpoints of its resource types. Strings are compared with the :attr:`~scim2_models.ScimPolicy.comparison_key` of its policy. Each of its resource types needs a mapping. :param version_key: The key of the hash giving the version of the resources whose mapping has no version. Keep it secret, as the hash covers the values clients never read, such as a password hash. The hash covers the links too, so renaming a member changes the version of its groups. A write on such a resource locks its row until the end of the transaction. :raises ValueError: When a resource type has no mapping, or when a mapping has no version and no key is given. The storage warns with a :exc:`~scim2_sqlalchemy.UniqueConstraintWarning` about each unique attribute that no unique constraint of the models covers: the database checks the unique values, so two resources could hold the same value. """ supports_cursors = True def __init__( self, mappings: Mapping[str, ResourceMapping], session: Callable[[], Session], provider: ScimProvider | None = None, *, version_key: bytes | None = None, ) -> None: super().__init__(mappings, provider, version_key) self.session = session
[docs] @contextmanager def operation(self) -> Generator[None]: """Run the operation in a savepoint. The savepoint comes from :meth:`~sqlalchemy.orm.Session.begin_nested`. A failed operation of a bulk request is then rolled back alone (:rfc:`RFC 7644 §3.7 <7644#section-3.7>`). """ with self.session().begin_nested(): yield
[docs] def generate_id( self, resource_type: ResourceType, resource: Resource[Any] ) -> str | None: """Return the identifier of a new resource, or :data:`None` to let its column give one. Override this method to choose the identifiers, as :meth:`InMemoryStorage.generate_id <scim2_server.memory.InMemoryStorage.generate_id>` allows. The following storage derives the identifier of a user from its userName at creation, so that two servers give the same user the same identifier: >>> import uuid >>> USERS = uuid.UUID("6ba7b811-9dad-11d1-80b4-00c04fd430c8") >>> class Storage(SqlAlchemyStorage): ... def generate_id(self, resource_type, resource): ... if resource_type.name != "User": ... return None ... return str(uuid.uuid5(USERS, resource.user_name)) The storage calls it once for each new resource, after checking the resource. The database checks the unique values when the storage writes the resource, after this method. So this method can run for a resource that the database then refuses with a 409. An identifier that another resource of the same type holds raises a 409. Per :rfc:`7643#section-3.1`, an identifier is unique across all the resource types, but the storage does not check it: choose identifiers that cannot clash, such as UUIDs. The storage stores the identifier in the type of its column, and the created resource has the identifier as the column gives it back. For example, ``007`` in an integer column becomes ``7``. The storage raises a :class:`ValueError` when the column cannot hold the identifier, or when it cannot write it, such as an ``id`` mapped to a property without setter. When the database numbers the records, as an autoincrement column does, give an identifier to all the resources of a type, or to none. The database does not skip the numbers given by this method, so a resource that it numbers later can get one of them, and a 409. """ return None
def get( self, resource_type: ResourceType, resource_id: str, *, response_parameters: ResponseParameters[Any] | None = None, ) -> Resource[Any]: """Return a resource, with the attributes the response keeps.""" record = self._load(resource_type, resource_id, response_parameters) return self._to_scim(resource_type, record, response_parameters) def search( self, resource_types: list[ResourceType], search_request: SearchRequest[Any], *, position: Any = None, ) -> SearchPage: """Return a page of the resources matching a search, on one resource type or at the root.""" if not resource_types: return SearchPage(0, []) session = self.session() comparator = self._ready_comparator(session) mappings = [self._mapping(resource_type) for resource_type in resource_types] anchor = _load_position(position, search_request, len(mappings)) if len(mappings) == 1: count, statement = _search_statements( mappings[0], search_request, comparator, anchor ) total = session.scalar(count) or 0 rows = session.execute(statement).all() return self._page(resource_types[0], total, rows, search_request, anchor) count, statement = _root_statements( mappings, search_request, comparator, anchor ) total = session.scalar(count) or 0 rows, more = page_rows(session.execute(statement).all(), search_request, anchor) records = {} for type_position, ids in _by_position(rows).items(): loading = _records_statement(mappings[type_position], ids, search_request) for record_id, record in session.execute(loading): records[type_position, record_id] = record return self._root_search_page( resource_types, total, rows, more, records, search_request, anchor ) def create( self, resource_type: ResourceType, resource: Resource[Any] ) -> Resource[Any]: """Store a new resource, and return it as stored.""" mapping = self._mapping(resource_type) self._check_storable(resource_type, resource, None) links = self._links(mapping, resource) resource_id = self.generate_id(resource_type, resource) record = self._new_record(resource_type, resource_id) def change() -> None: self.session().add(record) _from_scim(mapping, resource, record, links, creating=True) self._flush(mapping, change) return self.get(resource_type, str(getattr(record, mapping._id.key))) def update( self, resource_type: ResourceType, resource: Resource[Any], *, expected_version: str | None = None, ) -> Resource[Any]: """Replace a resource, and return it as stored.""" mapping = self._mapping(resource_type) record = self._load(resource_type, resource.id, locked=True) stored = self._to_scim(resource_type, record) self._check_version(resource_type, record, expected_version, stored) self._check_storable(resource_type, resource, stored) links = self._links(mapping, resource) def change() -> None: if self._clear_entries(mapping, record): self.session().flush() self._write_record(mapping, resource, record, links) self._flush(mapping, change) return self.get(resource_type, resource.id) # type: ignore[arg-type] def delete( self, resource_type: ResourceType, resource_id: str, *, expected_version: str | None = None, ) -> None: """Delete a resource.""" record = self._load(resource_type, resource_id, locked=True) self._check_version(resource_type, record, expected_version) self._flush(self._mapping(resource_type), lambda: self.session().delete(record)) def _load( self, resource_type: ResourceType, resource_id: str | None, parameters: ResponseParameters[Any] | None = None, locked: bool = False, ) -> Any: """Load a record, with the collections the response keeps. A record locked for a write whose version is a hash stays locked until the end of the transaction, so that two writes cannot both pass the check of the same version. """ mapping = self._mapping(resource_type) statement = _load_statement( mapping, resource_id, parameters, locked and mapping.version is None ) _expire(self.session(), mapping, resource_id) record = None if statement is None else self.session().scalar(statement) if record is None or not _has_text_id(mapping._id, record, resource_id): raise self._not_found(resource_type, resource_id) return record def _ready_comparator(self, session: Session) -> _Comparator: """Return how the database of the session compares strings, ready to compare them.""" connection = session.connection() comparator = self._comparator(connection.dialect) comparator.prepare(connection.connection.driver_connection) return comparator def _links( self, mapping: ResourceMapping, resource: Resource[Any] ) -> dict[Collection, list[Any]]: """Return the records each Link of a resource links to, refusing a value that links to nothing.""" links: dict[Collection, list[Any]] = {} for collections, entries in link_entries(mapping, resource): ids = [entry.value for entry in entries] found = [ ( collection, list( self.session().scalars(_links_statement(collection, ids)).all() ), ) for collection in collections ] links |= assign_links(found, entries, self._policy) return links def _flush(self, mapping: ResourceMapping, change: Callable[[], None]) -> None: """Apply a change of a resource in a savepoint, and flush it. A version changed in the meantime by another transaction raises a 412. A value that a unique constraint of the database refuses raises a 409, which tells the attribute when the constraint covers it alone. A foreign key the change breaks raises a 409 too, such as a record outside SCIM still referring to a deleted resource. """ session = self.session() try: with session.begin_nested(): change() session.flush() except StaleDataError as exception: raise PreconditionFailedException from exception except DBAPIError as exception: if _error_code(exception.orig) in _UNIQUE_VIOLATIONS: attributes = mapping._unique_columns() tables = list(dict.fromkeys(table for table, _ in attributes)) columns = _violated_columns( session.connection(), exception.orig, tables, mapping._record_table(), ) raise self._conflict(attributes, columns) from exception _translate(exception) raise
[docs] class AsyncSqlAlchemyStorage(_StorageBase, AsyncScimStorage): """The asynchronous variant of :class:`SqlAlchemyStorage`. It needs the ``asyncio`` extra, which installs the asynchronous support of SQLAlchemy: ``pip install "scim2-sqlalchemy[asyncio]"``. :param mappings: The mapping of each resource type, by resource type name. :param session: Return the current :class:`~sqlalchemy.ext.asyncio.AsyncSession`, such as an :class:`~sqlalchemy.ext.asyncio.async_scoped_session`. :param provider: The provider of the server. The storage then returns instances of its models, rather than of the models of the mappings. Strings are compared with the :attr:`~scim2_models.ScimPolicy.comparison_key` of its policy. Each of its resource types needs a mapping. :param version_key: The key of the hash giving the version of the resources whose mapping has no version. Keep it secret, as the hash covers the values clients never read, such as a password hash. The hash covers the links too, so renaming a member changes the version of its groups. A write on such a resource locks its row until the end of the transaction. :raises ValueError: When a resource type has no mapping, or when a mapping has no version and no key is given. """ supports_cursors = True def __init__( self, mappings: Mapping[str, ResourceMapping], session: Callable[[], "AsyncSession"], provider: ScimProvider | None = None, *, version_key: bytes | None = None, ) -> None: super().__init__(mappings, provider, version_key) self.session = session
[docs] @asynccontextmanager async def operation(self) -> AsyncGenerator[None]: """Run the operation in a savepoint. See :meth:`SqlAlchemyStorage.operation`.""" async with self.session().begin_nested(): yield
[docs] async def generate_id( self, resource_type: ResourceType, resource: Resource[Any] ) -> str | None: """Return the identifier of a new resource, or :data:`None` to let its column give one. See :meth:`SqlAlchemyStorage.generate_id`. This method can await, for instance to create the account in another service and take its identifier. A write that fails afterwards, such as with a 409 from the database, does not undo what the method did: override :meth:`create` to undo it. """ return None
async def get( self, resource_type: ResourceType, resource_id: str, *, response_parameters: ResponseParameters[Any] | None = None, ) -> Resource[Any]: """Return a resource, with the attributes the response keeps.""" record = await self._load(resource_type, resource_id, response_parameters) return self._to_scim(resource_type, record, response_parameters) async def search( self, resource_types: list[ResourceType], search_request: SearchRequest[Any], *, position: Any = None, ) -> SearchPage: """Return a page of the resources matching a search, on one resource type or at the root.""" if not resource_types: return SearchPage(0, []) session = self.session() comparator = await self._ready_comparator(session) mappings = [self._mapping(resource_type) for resource_type in resource_types] anchor = _load_position(position, search_request, len(mappings)) if len(mappings) == 1: count, statement = _search_statements( mappings[0], search_request, comparator, anchor ) total = await session.scalar(count) or 0 rows = (await session.execute(statement)).all() return self._page(resource_types[0], total, rows, search_request, anchor) count, statement = _root_statements( mappings, search_request, comparator, anchor ) total = await session.scalar(count) or 0 rows, more = page_rows( (await session.execute(statement)).all(), search_request, anchor ) records = {} for type_position, ids in _by_position(rows).items(): loading = _records_statement(mappings[type_position], ids, search_request) for record_id, record in await session.execute(loading): records[type_position, record_id] = record return self._root_search_page( resource_types, total, rows, more, records, search_request, anchor ) async def create( self, resource_type: ResourceType, resource: Resource[Any] ) -> Resource[Any]: """Store a new resource, and return it as stored.""" mapping = self._mapping(resource_type) self._check_storable(resource_type, resource, None) links = await self._links(mapping, resource) resource_id = await self.generate_id(resource_type, resource) record = self._new_record(resource_type, resource_id) async def change() -> None: self.session().add(record) _from_scim(mapping, resource, record, links, creating=True) await self._flush(mapping, change) return await self.get(resource_type, str(getattr(record, mapping._id.key))) async def update( self, resource_type: ResourceType, resource: Resource[Any], *, expected_version: str | None = None, ) -> Resource[Any]: """Replace a resource, and return it as stored.""" mapping = self._mapping(resource_type) record = await self._load(resource_type, resource.id, locked=True) stored = self._to_scim(resource_type, record) self._check_version(resource_type, record, expected_version, stored) self._check_storable(resource_type, resource, stored) links = await self._links(mapping, resource) async def change() -> None: if self._clear_entries(mapping, record): await self.session().flush() self._write_record(mapping, resource, record, links) await self._flush(mapping, change) return await self.get(resource_type, resource.id) # type: ignore[arg-type] async def delete( self, resource_type: ResourceType, resource_id: str, *, expected_version: str | None = None, ) -> None: """Delete a resource.""" record = await self._load(resource_type, resource_id, locked=True) self._check_version(resource_type, record, expected_version) async def change() -> None: await self.session().delete(record) await self._flush(self._mapping(resource_type), change) async def _load( self, resource_type: ResourceType, resource_id: str | None, parameters: ResponseParameters[Any] | None = None, locked: bool = False, ) -> Any: """Load a record, with the collections the response keeps. A record locked for a write whose version is a hash stays locked until the end of the transaction, so that two writes cannot both pass the check of the same version. """ mapping = self._mapping(resource_type) statement = _load_statement( mapping, resource_id, parameters, locked and mapping.version is None ) _expire(self.session(), mapping, resource_id) record = None if statement is None else await self.session().scalar(statement) if record is None or not _has_text_id(mapping._id, record, resource_id): raise self._not_found(resource_type, resource_id) return record async def _ready_comparator(self, session: "AsyncSession") -> _Comparator: """Return how the database of the session compares strings, ready to compare them.""" connection = await session.connection() comparator = self._comparator(connection.dialect) raw = await connection.get_raw_connection() await comparator.prepare_async(raw.driver_connection) return comparator async def _links( self, mapping: ResourceMapping, resource: Resource[Any] ) -> dict[Collection, list[Any]]: """Return the records each Link of a resource links to, refusing a value that links to nothing.""" links: dict[Collection, list[Any]] = {} for collections, entries in link_entries(mapping, resource): ids = [entry.value for entry in entries] found = [] for collection in collections: statement = _links_statement(collection, ids) records = (await self.session().scalars(statement)).all() found.append((collection, list(records))) links |= assign_links(found, entries, self._policy) return links async def _flush( self, mapping: ResourceMapping, change: Callable[[], Awaitable[None]] ) -> None: """Apply a change of a resource in a savepoint, and flush it, as the synchronous storage does.""" session = self.session() try: async with session.begin_nested(): await change() await session.flush() except StaleDataError as exception: raise PreconditionFailedException from exception except DBAPIError as exception: if _error_code(exception.orig) in _UNIQUE_VIOLATIONS: attributes = mapping._unique_columns() tables = list(dict.fromkeys(table for table, _ in attributes)) connection = await session.connection() columns = await connection.run_sync( _violated_columns, exception.orig, tables, mapping._record_table() ) raise self._conflict(attributes, columns) from exception _translate(exception) raise