From dc4b3cbb314b6294676436999b6d9e1634926298 Mon Sep 17 00:00:00 2001 From: Tim Jenness Date: Mon, 19 Aug 2024 14:17:17 -0700 Subject: [PATCH] Catch and reraise the IntegrityError in collections.x_remove --- python/lsst/daf/butler/_butler_collections.py | 2 +- .../daf/butler/direct_butler/_direct_butler_collections.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/python/lsst/daf/butler/_butler_collections.py b/python/lsst/daf/butler/_butler_collections.py index 71045f2937..8a3d2e2d63 100644 --- a/python/lsst/daf/butler/_butler_collections.py +++ b/python/lsst/daf/butler/_butler_collections.py @@ -384,7 +384,7 @@ def x_remove(self, name: str) -> None: ------ lsst.daf.butler.registry.MissingCollectionError Raised if no collection with the given name exists. - sqlalchemy.exc.IntegrityError + lsst.daf.butler.registry.OrphanedRecordError Raised if the database rows associated with the collection are still referenced by some other table, such as a dataset in a datastore (for `~CollectionType.RUN` collections only) or a diff --git a/python/lsst/daf/butler/direct_butler/_direct_butler_collections.py b/python/lsst/daf/butler/direct_butler/_direct_butler_collections.py index baa1c92176..8a4b7c70e8 100644 --- a/python/lsst/daf/butler/direct_butler/_direct_butler_collections.py +++ b/python/lsst/daf/butler/direct_butler/_direct_butler_collections.py @@ -31,10 +31,12 @@ from collections.abc import Iterable, Sequence, Set +import sqlalchemy from lsst.utils.iteration import ensure_iterable from .._butler_collections import ButlerCollections, CollectionInfo from .._collection_type import CollectionType +from ..registry._exceptions import OrphanedRecordError from ..registry.interfaces import ChainedCollectionRecord from ..registry.sql_registry import SqlRegistry @@ -151,4 +153,7 @@ def register(self, name: str, type: CollectionType = CollectionType.RUN, doc: st return self._registry.registerCollection(name, type, doc) def x_remove(self, name: str) -> None: - self._registry.removeCollection(name) + try: + self._registry.removeCollection(name) + except sqlalchemy.exc.IntegrityError as e: + raise OrphanedRecordError(f"Datasets in run {name} are still referenced elsewhere.") from e