Coverage for python/lsst/daf/butler/registry/attributes.py: 100%
53 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-04 02:04 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-04 02:04 -0800
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21from __future__ import annotations
23"""The default concrete implementation of the class that manages
24attributes for `Registry`.
25"""
27__all__ = ["DefaultButlerAttributeManager"]
29from typing import ClassVar, Iterable, Optional, Tuple
31import sqlalchemy
33from ..core.ddl import FieldSpec, TableSpec
34from .interfaces import (
35 ButlerAttributeExistsError,
36 ButlerAttributeManager,
37 Database,
38 StaticTablesContext,
39 VersionTuple,
40)
42# This manager is supposed to have super-stable schema that never changes
43# but there may be cases when we need data migration on this table so we
44# keep version for it as well.
45_VERSION = VersionTuple(1, 0, 0)
48class DefaultButlerAttributeManager(ButlerAttributeManager):
49 """An implementation of `ButlerAttributeManager` that stores attributes
50 in a database table.
52 Parameters
53 ----------
54 db : `Database`
55 Database engine interface for the namespace in which this table lives.
56 table : `sqlalchemy.schema.Table`
57 SQLAlchemy representation of the table that stores attributes.
58 """
60 def __init__(self, db: Database, table: sqlalchemy.schema.Table):
61 self._db = db
62 self._table = table
64 _TABLE_NAME: ClassVar[str] = "butler_attributes"
66 _TABLE_SPEC: ClassVar[TableSpec] = TableSpec(
67 fields=[
68 FieldSpec("name", dtype=sqlalchemy.String, length=1024, primaryKey=True),
69 FieldSpec("value", dtype=sqlalchemy.String, length=65535, nullable=False),
70 ],
71 )
73 @classmethod
74 def initialize(cls, db: Database, context: StaticTablesContext) -> ButlerAttributeManager:
75 # Docstring inherited from ButlerAttributeManager.
76 table = context.addTable(cls._TABLE_NAME, cls._TABLE_SPEC)
77 return cls(db=db, table=table)
79 def get(self, name: str, default: Optional[str] = None) -> Optional[str]:
80 # Docstring inherited from ButlerAttributeManager.
81 sql = sqlalchemy.sql.select(self._table.columns.value).where(self._table.columns.name == name)
82 with self._db.query(sql) as sql_result:
83 row = sql_result.fetchone()
84 if row is not None:
85 return row[0]
86 return default
88 def set(self, name: str, value: str, *, force: bool = False) -> None:
89 # Docstring inherited from ButlerAttributeManager.
90 if not name or not value:
91 raise ValueError("name and value cannot be empty")
92 if force:
93 self._db.replace(
94 self._table,
95 {
96 "name": name,
97 "value": value,
98 },
99 )
100 else:
101 try:
102 self._db.insert(
103 self._table,
104 {
105 "name": name,
106 "value": value,
107 },
108 )
109 except sqlalchemy.exc.IntegrityError as exc:
110 raise ButlerAttributeExistsError(f"attribute {name} already exists") from exc
112 def delete(self, name: str) -> bool:
113 # Docstring inherited from ButlerAttributeManager.
114 numRows = self._db.delete(self._table, ["name"], {"name": name})
115 return numRows > 0
117 def items(self) -> Iterable[Tuple[str, str]]:
118 # Docstring inherited from ButlerAttributeManager.
119 sql = sqlalchemy.sql.select(
120 self._table.columns.name,
121 self._table.columns.value,
122 )
123 with self._db.query(sql) as sql_result:
124 sql_rows = sql_result.fetchall()
125 for row in sql_rows:
126 yield row[0], row[1]
128 def empty(self) -> bool:
129 # Docstring inherited from ButlerAttributeManager.
130 sql = sqlalchemy.sql.select(sqlalchemy.sql.func.count()).select_from(self._table)
131 with self._db.query(sql) as sql_result:
132 row = sql_result.fetchone()
133 return row[0] == 0
135 @classmethod
136 def currentVersion(cls) -> Optional[VersionTuple]:
137 # Docstring inherited from VersionedExtension.
138 return _VERSION
140 def schemaDigest(self) -> Optional[str]:
141 # Docstring inherited from VersionedExtension.
142 return self._defaultSchemaDigest([self._table], self._db.dialect)