Coverage for python/lsst/daf/butler/core/ddl.py : 51%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# This file is part of daf_butler. # # Developed for the LSST Data Management System. # This product includes software developed by the LSST Project # (http://www.lsst.org). # See the COPYRIGHT file at the top-level directory of this distribution # for details of code ownership. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. etc.) in Python.
This provides an extra layer on top of SQLAlchemy's classes for these concepts, because we need a level of indirection between logical tables and the actual SQL, and SQLAlchemy's DDL classes always map 1-1 to SQL.
We've opted for the rather more obscure "ddl" as the name of this module instead of "schema" because the latter is too overloaded; in most SQL databases, a "schema" is also another term for a namespace. """
"""Exceptions used to indicate problems in Registry schema configuration. """
def translate(cls, caught, message): """A decorator that re-raises exceptions as `SchemaValidationError`.
Decorated functions must be class or instance methods, with a ``config`` parameter as their first argument. This will be passed to ``message.format()`` as a keyword argument, along with ``err``, the original exception.
Parameters ---------- caught : `type` (`Exception` subclass) The type of exception to catch. message : `str` A `str.format` string that may contain named placeholders for ``config``, ``err``, or any keyword-only argument accepted by the decorated function. """ try: return func(self, config, *args, **kwds) except caught as err: raise cls(message.format(config=str(config), err=err))
"""A SQLAlchemy custom type that maps Python `bytes` to a base64-encoded `sqlalchemy.String`. """
length = 4*ceil(nbytes/3) super().__init__(*args, length=length, **kwds) self.nbytes = nbytes
# 'value' is native `bytes`. We want to encode that to base64 `bytes` # and then ASCII `str`, because `str` is what SQLAlchemy expects for # String fields. if value is None: return None if not isinstance(value, bytes): raise TypeError( f"Base64Bytes fields require 'bytes' values; got '{value}' with type {type(value)}." ) return b64encode(value).decode("ascii")
# 'value' is a `str` that must be ASCII because it's base64-encoded. # We want to transform that to base64-encoded `bytes` and then # native `bytes`. return b64decode(value.encode("ascii")) if value is not None else None
"""A SQLAlchemy custom type that maps Python `sphgeom.ConvexPolygon` to a base64-encoded `sqlalchemy.String`. """
if value is None: return None return super().process_bind_param(value.encode(), dialect)
if value is None: return None return ConvexPolygon.decode(super().process_result_value(value, dialect))
"string": sqlalchemy.String, "int": sqlalchemy.Integer, "float": sqlalchemy.Float, "region": Base64Region, "bool": sqlalchemy.Boolean, "blob": sqlalchemy.LargeBinary, "datetime": sqlalchemy.DateTime, "hash": Base64Bytes }
class FieldSpec: """A struct-like class used to define a column in a logical `Registry` table. """
"""Name of the column."""
"""Type of the column; usually a `type` subclass provided by SQLAlchemy that defines both a Python type and a corresponding precise SQL type. """
"""Length of the type in the database, for variable-length types."""
"""Natural length used for hash and encoded-region columns, to be converted into the post-encoding length. """
"""Whether this field is (part of) its table's primary key."""
"""Whether the database should insert automatically incremented values when no value is provided in an INSERT. """
"""Whether this field is allowed to be NULL."""
"""Documentation for this field."""
return self.name == other.name
return hash(self.name)
def fromConfig(cls, config: Config, **kwds) -> FieldSpec: """Create a `FieldSpec` from a subset of a `SchemaConfig`.
Parameters ---------- config: `Config` Configuration describing the column. Nested configuration keys correspond to `FieldSpec` attributes. kwds Additional keyword arguments that provide defaults for values not present in config.
Returns ------- spec: `FieldSpec` Specification structure for the column.
Raises ------ SchemaValidationError Raised if configuration keys are missing or have invalid values. """ dtype = VALID_CONFIG_COLUMN_TYPES.get(config["type"]) if dtype is None: raise SchemaValidationError(f"Invalid field type string: '{config['type']}'.") if not config["name"].islower(): raise SchemaValidationError(f"Column name '{config['name']}' is not all lowercase.") self = cls(name=config["name"], dtype=dtype, **kwds) self.length = config.get("length", self.length) self.nbytes = config.get("nbytes", self.nbytes) if self.length is not None and self.nbytes is not None: raise SchemaValidationError(f"Both length and nbytes provided for field '{self.name}'.") self.primaryKey = config.get("primaryKey", self.primaryKey) self.autoincrement = config.get("autoincrement", self.autoincrement) self.nullable = config.get("nullable", False if self.primaryKey else self.nullable) self.doc = stripIfNotNone(config.get("doc", None)) return self
"""Return a sized version of the column type, utilizing either (or neither) of ``self.length`` and ``self.nbytes``.
Returns ------- dtype : `sqlalchemy.types.TypeEngine` A SQLAlchemy column type object. """ if self.length is not None: return self.dtype(length=self.length) if self.nbytes is not None: return self.dtype(nbytes=self.nbytes) return self.dtype
class ForeignKeySpec: """A struct-like class used to define a foreign key constraint in a logical `Registry` table. """
"""Name of the target table."""
"""Tuple of source table column names."""
"""Tuple of target table column names."""
"""SQL clause indicating how to handle deletes to the target table.
If not `None` (which indicates that a constraint violation exception should be raised), should be either "SET NULL" or "CASCADE". """
def fromConfig(cls, config: Config) -> ForeignKeySpec: """Create a `ForeignKeySpec` from a subset of a `SchemaConfig`.
Parameters ---------- config: `Config` Configuration describing the constraint. Nested configuration keys correspond to `ForeignKeySpec` attributes.
Returns ------- spec: `ForeignKeySpec` Specification structure for the constraint.
Raises ------ SchemaValidationError Raised if configuration keys are missing or have invalid values. """ return cls(table=config["table"], source=tuple(iterable(config["source"])), target=tuple(iterable(config["target"])), onDelete=config.get("onDelete", None))
class TableSpec: """A struct-like class used to define a table or table-like query interface. """
"""Specifications for the columns in this table."""
"""Non-primary-key unique constraints for the table."""
"""Indexes for the table."""
"""Foreign key constraints for the table."""
"""Documentation for the table."""
def fromConfig(cls, config: Config) -> TableSpec: """Create a `ForeignKeySpec` from a subset of a `SchemaConfig`.
Parameters ---------- config: `Config` Configuration describing the constraint. Nested configuration keys correspond to `TableSpec` attributes.
Returns ------- spec: `TableSpec` Specification structure for the table.
Raises ------ SchemaValidationError Raised if configuration keys are missing or have invalid values. """ return cls( fields=NamedValueSet(FieldSpec.fromConfig(c) for c in config["columns"]), unique={tuple(u) for u in config.get("unique", ())}, foreignKeys=[ForeignKeySpec.fromConfig(c) for c in config.get("foreignKeys", ())], sql=config.get("sql"), doc=stripIfNotNone(config.get("doc")), ) |