Coverage for python/lsst/daf/butler/registry/collections/nameKey.py: 98%

43 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-27 09:43 +0000

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 software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27from __future__ import annotations 

28 

29from ... import ddl 

30 

31__all__ = ["NameKeyCollectionManager"] 

32 

33from typing import TYPE_CHECKING, Any 

34 

35import sqlalchemy 

36 

37from ..._timespan import TimespanDatabaseRepresentation 

38from ..interfaces import VersionTuple 

39from ._base import ( 

40 CollectionTablesTuple, 

41 DefaultCollectionManager, 

42 makeCollectionChainTableSpec, 

43 makeRunTableSpec, 

44) 

45 

46if TYPE_CHECKING: 

47 from ..interfaces import CollectionRecord, Database, DimensionRecordStorageManager, StaticTablesContext 

48 

49 

50_KEY_FIELD_SPEC = ddl.FieldSpec("name", dtype=sqlalchemy.String, length=64, primaryKey=True) 

51 

52 

53# This has to be updated on every schema change 

54_VERSION = VersionTuple(2, 0, 0) 

55 

56 

57def _makeTableSpecs(TimespanReprClass: type[TimespanDatabaseRepresentation]) -> CollectionTablesTuple: 

58 return CollectionTablesTuple( 

59 collection=ddl.TableSpec( 

60 fields=[ 

61 _KEY_FIELD_SPEC, 

62 ddl.FieldSpec("type", dtype=sqlalchemy.SmallInteger, nullable=False), 

63 ddl.FieldSpec("doc", dtype=sqlalchemy.Text, nullable=True), 

64 ], 

65 ), 

66 run=makeRunTableSpec("name", sqlalchemy.String, TimespanReprClass), 

67 collection_chain=makeCollectionChainTableSpec("name", sqlalchemy.String), 

68 ) 

69 

70 

71class NameKeyCollectionManager(DefaultCollectionManager): 

72 """A `CollectionManager` implementation that uses collection names for 

73 primary/foreign keys and aggressively loads all collection/run records in 

74 the database into memory. 

75 

76 Most of the logic, including caching policy, is implemented in the base 

77 class, this class only adds customizations specific to this particular 

78 table schema. 

79 """ 

80 

81 @classmethod 

82 def initialize( 

83 cls, 

84 db: Database, 

85 context: StaticTablesContext, 

86 *, 

87 dimensions: DimensionRecordStorageManager, 

88 registry_schema_version: VersionTuple | None = None, 

89 ) -> NameKeyCollectionManager: 

90 # Docstring inherited from CollectionManager. 

91 return cls( 

92 db, 

93 tables=context.addTableTuple(_makeTableSpecs(db.getTimespanRepresentation())), # type: ignore 

94 collectionIdName="name", 

95 dimensions=dimensions, 

96 registry_schema_version=registry_schema_version, 

97 ) 

98 

99 @classmethod 

100 def getCollectionForeignKeyName(cls, prefix: str = "collection") -> str: 

101 # Docstring inherited from CollectionManager. 

102 return f"{prefix}_name" 

103 

104 @classmethod 

105 def getRunForeignKeyName(cls, prefix: str = "run") -> str: 

106 # Docstring inherited from CollectionManager. 

107 return f"{prefix}_name" 

108 

109 @classmethod 

110 def addCollectionForeignKey( 

111 cls, 

112 tableSpec: ddl.TableSpec, 

113 *, 

114 prefix: str = "collection", 

115 onDelete: str | None = None, 

116 constraint: bool = True, 

117 **kwargs: Any, 

118 ) -> ddl.FieldSpec: 

119 # Docstring inherited from CollectionManager. 

120 original = _KEY_FIELD_SPEC 

121 copy = ddl.FieldSpec( 

122 cls.getCollectionForeignKeyName(prefix), dtype=original.dtype, length=original.length, **kwargs 

123 ) 

124 tableSpec.fields.add(copy) 

125 if constraint: 

126 tableSpec.foreignKeys.append( 

127 ddl.ForeignKeySpec( 

128 "collection", source=(copy.name,), target=(original.name,), onDelete=onDelete 

129 ) 

130 ) 

131 return copy 

132 

133 @classmethod 

134 def addRunForeignKey( 

135 cls, 

136 tableSpec: ddl.TableSpec, 

137 *, 

138 prefix: str = "run", 

139 onDelete: str | None = None, 

140 constraint: bool = True, 

141 **kwargs: Any, 

142 ) -> ddl.FieldSpec: 

143 # Docstring inherited from CollectionManager. 

144 original = _KEY_FIELD_SPEC 

145 copy = ddl.FieldSpec( 

146 cls.getRunForeignKeyName(prefix), dtype=original.dtype, length=original.length, **kwargs 

147 ) 

148 tableSpec.fields.add(copy) 

149 if constraint: 149 ↛ 153line 149 didn't jump to line 153, because the condition on line 149 was never false

150 tableSpec.foreignKeys.append( 

151 ddl.ForeignKeySpec("run", source=(copy.name,), target=(original.name,), onDelete=onDelete) 

152 ) 

153 return copy 

154 

155 def _getByName(self, name: str) -> CollectionRecord | None: 

156 # Docstring inherited from DefaultCollectionManager. 

157 return self._records.get(name) 

158 

159 @classmethod 

160 def currentVersions(cls) -> list[VersionTuple]: 

161 # Docstring inherited from VersionedExtension. 

162 return [_VERSION]