Coverage for python/lsst/daf/butler/registry/collections/nameKey.py: 98%
42 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-10-02 07:59 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-10-02 07:59 +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
29__all__ = ["NameKeyCollectionManager"]
31from typing import TYPE_CHECKING, Any
33import sqlalchemy
35from ...core import TimespanDatabaseRepresentation, ddl
36from ..interfaces import VersionTuple
37from ._base import (
38 CollectionTablesTuple,
39 DefaultCollectionManager,
40 makeCollectionChainTableSpec,
41 makeRunTableSpec,
42)
44if TYPE_CHECKING:
45 from ..interfaces import CollectionRecord, Database, DimensionRecordStorageManager, StaticTablesContext
48_KEY_FIELD_SPEC = ddl.FieldSpec("name", dtype=sqlalchemy.String, length=64, primaryKey=True)
51# This has to be updated on every schema change
52_VERSION = VersionTuple(2, 0, 0)
55def _makeTableSpecs(TimespanReprClass: type[TimespanDatabaseRepresentation]) -> CollectionTablesTuple:
56 return CollectionTablesTuple(
57 collection=ddl.TableSpec(
58 fields=[
59 _KEY_FIELD_SPEC,
60 ddl.FieldSpec("type", dtype=sqlalchemy.SmallInteger, nullable=False),
61 ddl.FieldSpec("doc", dtype=sqlalchemy.Text, nullable=True),
62 ],
63 ),
64 run=makeRunTableSpec("name", sqlalchemy.String, TimespanReprClass),
65 collection_chain=makeCollectionChainTableSpec("name", sqlalchemy.String),
66 )
69class NameKeyCollectionManager(DefaultCollectionManager):
70 """A `CollectionManager` implementation that uses collection names for
71 primary/foreign keys and aggressively loads all collection/run records in
72 the database into memory.
74 Most of the logic, including caching policy, is implemented in the base
75 class, this class only adds customizations specific to this particular
76 table schema.
77 """
79 @classmethod
80 def initialize(
81 cls,
82 db: Database,
83 context: StaticTablesContext,
84 *,
85 dimensions: DimensionRecordStorageManager,
86 registry_schema_version: VersionTuple | None = None,
87 ) -> NameKeyCollectionManager:
88 # Docstring inherited from CollectionManager.
89 return cls(
90 db,
91 tables=context.addTableTuple(_makeTableSpecs(db.getTimespanRepresentation())), # type: ignore
92 collectionIdName="name",
93 dimensions=dimensions,
94 registry_schema_version=registry_schema_version,
95 )
97 @classmethod
98 def getCollectionForeignKeyName(cls, prefix: str = "collection") -> str:
99 # Docstring inherited from CollectionManager.
100 return f"{prefix}_name"
102 @classmethod
103 def getRunForeignKeyName(cls, prefix: str = "run") -> str:
104 # Docstring inherited from CollectionManager.
105 return f"{prefix}_name"
107 @classmethod
108 def addCollectionForeignKey(
109 cls,
110 tableSpec: ddl.TableSpec,
111 *,
112 prefix: str = "collection",
113 onDelete: str | None = None,
114 constraint: bool = True,
115 **kwargs: Any,
116 ) -> ddl.FieldSpec:
117 # Docstring inherited from CollectionManager.
118 original = _KEY_FIELD_SPEC
119 copy = ddl.FieldSpec(
120 cls.getCollectionForeignKeyName(prefix), dtype=original.dtype, length=original.length, **kwargs
121 )
122 tableSpec.fields.add(copy)
123 if constraint:
124 tableSpec.foreignKeys.append(
125 ddl.ForeignKeySpec(
126 "collection", source=(copy.name,), target=(original.name,), onDelete=onDelete
127 )
128 )
129 return copy
131 @classmethod
132 def addRunForeignKey(
133 cls,
134 tableSpec: ddl.TableSpec,
135 *,
136 prefix: str = "run",
137 onDelete: str | None = None,
138 constraint: bool = True,
139 **kwargs: Any,
140 ) -> ddl.FieldSpec:
141 # Docstring inherited from CollectionManager.
142 original = _KEY_FIELD_SPEC
143 copy = ddl.FieldSpec(
144 cls.getRunForeignKeyName(prefix), dtype=original.dtype, length=original.length, **kwargs
145 )
146 tableSpec.fields.add(copy)
147 if constraint: 147 ↛ 151line 147 didn't jump to line 151, because the condition on line 147 was never false
148 tableSpec.foreignKeys.append(
149 ddl.ForeignKeySpec("run", source=(copy.name,), target=(original.name,), onDelete=onDelete)
150 )
151 return copy
153 def _getByName(self, name: str) -> CollectionRecord | None:
154 # Docstring inherited from DefaultCollectionManager.
155 return self._records.get(name)
157 @classmethod
158 def currentVersions(cls) -> list[VersionTuple]:
159 # Docstring inherited from VersionedExtension.
160 return [_VERSION]