Skip to content

Commit 8b20bd5

Browse files
committed
remove catalog links
1 parent b77f991 commit 8b20bd5

File tree

3 files changed

+92
-22
lines changed

3 files changed

+92
-22
lines changed

stac_fastapi/core/stac_fastapi/core/extensions/catalogs.py

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -306,31 +306,30 @@ async def delete_catalog(
306306
db_catalog = await self.client.database.find_catalog(catalog_id)
307307
catalog = self.client.catalog_serializer.db_to_stac(db_catalog, request)
308308

309-
# If cascade is true, delete all collections linked to this catalog
310-
if cascade:
311-
# Extract collection IDs from catalog links
312-
collection_ids = []
313-
if hasattr(catalog, "links") and catalog.links:
314-
for link in catalog.links:
315-
rel = (
316-
link.get("rel")
309+
# Extract collection IDs from catalog links
310+
collection_ids = []
311+
if hasattr(catalog, "links") and catalog.links:
312+
for link in catalog.links:
313+
rel = (
314+
link.get("rel")
315+
if hasattr(link, "get")
316+
else getattr(link, "rel", None)
317+
)
318+
if rel == "child":
319+
href = (
320+
link.get("href", "")
317321
if hasattr(link, "get")
318-
else getattr(link, "rel", None)
322+
else getattr(link, "href", "")
319323
)
320-
if rel == "child":
321-
href = (
322-
link.get("href", "")
323-
if hasattr(link, "get")
324-
else getattr(link, "href", "")
325-
)
326-
if href and "/collections/" in href:
327-
# Extract collection ID from href
328-
collection_id = href.split("/collections/")[-1].split(
329-
"/"
330-
)[0]
331-
if collection_id:
332-
collection_ids.append(collection_id)
324+
if href and "/collections/" in href:
325+
# Extract collection ID from href
326+
collection_id = href.split("/collections/")[-1].split("/")[
327+
0
328+
]
329+
if collection_id:
330+
collection_ids.append(collection_id)
333331

332+
if cascade:
334333
# Delete each collection
335334
for coll_id in collection_ids:
336335
try:
@@ -348,6 +347,63 @@ async def delete_catalog(
348347
logger.warning(
349348
f"Failed to delete collection {coll_id}: {e}"
350349
)
350+
else:
351+
# Remove catalog link from each collection (orphan them)
352+
for coll_id in collection_ids:
353+
try:
354+
collection = await self.client.get_collection(
355+
coll_id, request=request
356+
)
357+
# Remove the catalog link from the collection
358+
if hasattr(collection, "links"):
359+
collection.links = [
360+
link
361+
for link in collection.links
362+
if not (
363+
getattr(link, "rel", None) == "catalog"
364+
and catalog_id in getattr(link, "href", "")
365+
)
366+
]
367+
elif isinstance(collection, dict):
368+
collection["links"] = [
369+
link
370+
for link in collection.get("links", [])
371+
if not (
372+
link.get("rel") == "catalog"
373+
and catalog_id in link.get("href", "")
374+
)
375+
]
376+
377+
# Update the collection in the database
378+
collection_dict = (
379+
collection.model_dump(mode="json")
380+
if hasattr(collection, "model_dump")
381+
else collection
382+
)
383+
collection_db = (
384+
self.client.database.collection_serializer.stac_to_db(
385+
collection_dict, request
386+
)
387+
)
388+
await self.client.database.client.index(
389+
index=COLLECTIONS_INDEX,
390+
id=coll_id,
391+
body=collection_db.model_dump()
392+
if hasattr(collection_db, "model_dump")
393+
else collection_db,
394+
refresh=True,
395+
)
396+
logger.info(f"Removed catalog link from collection {coll_id}")
397+
except Exception as e:
398+
error_msg = str(e)
399+
if "not found" in error_msg.lower():
400+
logger.debug(
401+
f"Collection {coll_id} not found, skipping (may have been deleted elsewhere)"
402+
)
403+
else:
404+
logger.warning(
405+
f"Failed to remove catalog link from collection {coll_id}: {e}"
406+
)
351407

352408
# Delete the catalog
353409
await self.client.database.delete_catalog(catalog_id)

stac_fastapi/tests/api/test_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"GET /catalogs",
5454
"POST /catalogs",
5555
"GET /catalogs/{catalog_id}",
56+
"DELETE /catalogs/{catalog_id}",
5657
"GET /catalogs/{catalog_id}/collections",
5758
"POST /catalogs/{catalog_id}/collections",
5859
"GET /catalogs/{catalog_id}/collections/{collection_id}",

stac_fastapi/tests/extensions/test_catalogs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,16 @@ async def test_delete_catalog_no_cascade(catalogs_app_client, load_test_data):
583583
# Verify collection still exists (no cascade delete)
584584
get_coll_resp = await catalogs_app_client.get(f"/collections/{collection_id}")
585585
assert get_coll_resp.status_code == 200
586+
587+
# Verify the catalog link was removed from the collection
588+
collection_data = get_coll_resp.json()
589+
collection_links = collection_data.get("links", [])
590+
catalog_link = None
591+
for link in collection_links:
592+
if link.get("rel") == "catalog" and catalog_id in link.get("href", ""):
593+
catalog_link = link
594+
break
595+
596+
assert (
597+
catalog_link is None
598+
), "Collection should not have catalog link after catalog deletion"

0 commit comments

Comments
 (0)