Coverage for python/lsst/daf/butler/registry/attributes.py: 100%

49 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-15 02:06 -0700

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 

22 

23"""The default concrete implementation of the class that manages 

24attributes for `Registry`. 

25""" 

26 

27__all__ = ["DefaultButlerAttributeManager"] 

28 

29from typing import ClassVar, Iterable, Optional, Tuple 

30 

31import sqlalchemy 

32 

33from ..core.ddl import FieldSpec, TableSpec 

34from .interfaces import ( 

35 ButlerAttributeExistsError, 

36 ButlerAttributeManager, 

37 Database, 

38 StaticTablesContext, 

39 VersionTuple, 

40) 

41 

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) 

46 

47 

48class DefaultButlerAttributeManager(ButlerAttributeManager): 

49 """An implementation of `ButlerAttributeManager` that stores attributes 

50 in a database table. 

51 

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 """ 

59 

60 def __init__(self, db: Database, table: sqlalchemy.schema.Table): 

61 self._db = db 

62 self._table = table 

63 

64 _TABLE_NAME: ClassVar[str] = "butler_attributes" 

65 

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 ) 

72 

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) 

78 

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 row = self._db.query(sql).fetchone() 

83 if row is not None: 

84 return row[0] 

85 return default 

86 

87 def set(self, name: str, value: str, *, force: bool = False) -> None: 

88 # Docstring inherited from ButlerAttributeManager. 

89 if not name or not value: 

90 raise ValueError("name and value cannot be empty") 

91 if force: 

92 self._db.replace( 

93 self._table, 

94 { 

95 "name": name, 

96 "value": value, 

97 }, 

98 ) 

99 else: 

100 try: 

101 self._db.insert( 

102 self._table, 

103 { 

104 "name": name, 

105 "value": value, 

106 }, 

107 ) 

108 except sqlalchemy.exc.IntegrityError as exc: 

109 raise ButlerAttributeExistsError(f"attribute {name} already exists") from exc 

110 

111 def delete(self, name: str) -> bool: 

112 # Docstring inherited from ButlerAttributeManager. 

113 numRows = self._db.delete(self._table, ["name"], {"name": name}) 

114 return numRows > 0 

115 

116 def items(self) -> Iterable[Tuple[str, str]]: 

117 # Docstring inherited from ButlerAttributeManager. 

118 sql = sqlalchemy.sql.select( 

119 self._table.columns.name, 

120 self._table.columns.value, 

121 ) 

122 for row in self._db.query(sql): 

123 yield row[0], row[1] 

124 

125 def empty(self) -> bool: 

126 # Docstring inherited from ButlerAttributeManager. 

127 sql = sqlalchemy.sql.select(sqlalchemy.sql.func.count()).select_from(self._table) 

128 row = self._db.query(sql).fetchone() 

129 return row[0] == 0 

130 

131 @classmethod 

132 def currentVersion(cls) -> Optional[VersionTuple]: 

133 # Docstring inherited from VersionedExtension. 

134 return _VERSION 

135 

136 def schemaDigest(self) -> Optional[str]: 

137 # Docstring inherited from VersionedExtension. 

138 return self._defaultSchemaDigest([self._table], self._db.dialect)