From 76241470a2974cb6d52ba4018e9681c870b54d50 Mon Sep 17 00:00:00 2001 From: pedro-cf Date: Wed, 17 Jul 2024 22:32:02 +0100 Subject: [PATCH 1/5] new index --- stac_fastapi/mongo/database_logic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stac_fastapi/mongo/database_logic.py b/stac_fastapi/mongo/database_logic.py index 4c708af..b7cfe06 100644 --- a/stac_fastapi/mongo/database_logic.py +++ b/stac_fastapi/mongo/database_logic.py @@ -75,7 +75,7 @@ async def create_item_index(): collection = db[ITEMS_INDEX] try: await collection.create_index([("properties.datetime", -1)]) - await collection.create_index([("id", 1)], unique=True) + await collection.create_index([("collection", 1), ("id", 1)], unique=True) await collection.create_index([("geometry", "2dsphere")]) print(f"Indexes created successfully for collection: {ITEMS_INDEX}.") except Exception as e: From f2b39d695b6de43e5d9a2a4ab3726f25c1335d1c Mon Sep 17 00:00:00 2001 From: pedro-cf Date: Wed, 17 Jul 2024 22:36:48 +0100 Subject: [PATCH 2/5] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac44d45..c24b375 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0/ - Add support for python 3.12. [#22](https://github.com/Healy-Hyperspatial/stac-fastapi-mongo/pull/22) - Updated sfeos core to v3.0.0a0, fixed datetime functionality. [#23](https://github.com/Healy-Hyperspatial/stac-fastapi-mongo/pull/23) +### Fixed + +- Added a new index based on collection id and item id to ensure item IDs aren't required to be unique across all collections. [#26](https://github.com/Healy-Hyperspatial/stac-fastapi-mongo/pull/26) + ## [v3.2.1] From f74306d9eb8d2d06b85b2b5a96e2b1a4d283d4ae Mon Sep 17 00:00:00 2001 From: pedro-cf Date: Mon, 22 Jul 2024 21:09:24 +0100 Subject: [PATCH 3/5] fixes and test --- stac_fastapi/mongo/database_logic.py | 8 ++++-- stac_fastapi/tests/resources/test_item.py | 30 ++++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/stac_fastapi/mongo/database_logic.py b/stac_fastapi/mongo/database_logic.py index b7cfe06..70255ba 100644 --- a/stac_fastapi/mongo/database_logic.py +++ b/stac_fastapi/mongo/database_logic.py @@ -691,7 +691,9 @@ async def prep_create_item( mongo_item = self.item_serializer.stac_to_db(item, base_url) if not exist_ok: - existing_item = await items_collection.find_one({"id": mongo_item["id"]}) + existing_item = await items_collection.find_one( + {"id": mongo_item["id"], "collection": mongo_item["collection"]} + ) if existing_item: raise ConflictError( f"Item {mongo_item['id']} in collection {mongo_item['collection']} already exists" @@ -733,7 +735,9 @@ def sync_prep_create_item( mongo_item = self.item_serializer.stac_to_db(item, base_url) if not exist_ok: - existing_item = items_collection.find_one({"id": mongo_item["id"]}) + existing_item = items_collection.find_one( + {"id": mongo_item["id"], "collection": mongo_item["collection"]} + ) if existing_item: raise ConflictError( f"Item {mongo_item['id']} in collection {mongo_item['collection']} already exists" diff --git a/stac_fastapi/tests/resources/test_item.py b/stac_fastapi/tests/resources/test_item.py index f3c0361..efc647a 100644 --- a/stac_fastapi/tests/resources/test_item.py +++ b/stac_fastapi/tests/resources/test_item.py @@ -34,7 +34,7 @@ def rfc3339_str_to_datetime(s: str) -> datetime: @pytest.mark.asyncio -async def test_create_and_delete_item(app_client, ctx, txn_client): +async def test_create_item_indices(app_client, ctx, txn_client): """Test creation and deletion of a single item (transactions extension)""" test_item = ctx.item @@ -890,3 +890,31 @@ async def test_search_datetime_validation_errors(app_client): # resp = await app_client.get("/search?datetime={}".format(dt)) # assert resp.status_code == 400 # updated for same reason as sfeos + + +@pytest.mark.asyncio +async def test_create_same_item_in_different_collections( + app_client, ctx, load_test_data +): + """Test creation of items and indices""" + + test_item = load_test_data("test_item.json") + test_collection = load_test_data("test_collection.json") + + # create item in collection where an item with same id already exists + resp = await app_client.post( + f"/collections/{test_collection['id']}/items", json=test_item + ) + assert resp.status_code == 409, resp.json() + + # prep second collection + test_collection["id"] = "test_collection2" + resp = await app_client.post("/collections", json=test_collection) + assert resp.status_code == 201, resp.json() + + # create item with same id in second collection + test_item["collection"] = test_collection["id"] + resp = await app_client.post( + f"/collections/{test_collection['id']}/items", json=test_item + ) + assert resp.status_code == 201, resp.json() From 3f0a6575977af2d4e08c02be530c1c01a3f7fb0f Mon Sep 17 00:00:00 2001 From: pedro-cf Date: Mon, 22 Jul 2024 21:15:03 +0100 Subject: [PATCH 4/5] tweak, better usage of indexes --- stac_fastapi/mongo/database_logic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stac_fastapi/mongo/database_logic.py b/stac_fastapi/mongo/database_logic.py index 70255ba..5af0829 100644 --- a/stac_fastapi/mongo/database_logic.py +++ b/stac_fastapi/mongo/database_logic.py @@ -692,7 +692,7 @@ async def prep_create_item( if not exist_ok: existing_item = await items_collection.find_one( - {"id": mongo_item["id"], "collection": mongo_item["collection"]} + {"collection": mongo_item["collection"], "id": mongo_item["id"]} ) if existing_item: raise ConflictError( @@ -736,7 +736,7 @@ def sync_prep_create_item( if not exist_ok: existing_item = items_collection.find_one( - {"id": mongo_item["id"], "collection": mongo_item["collection"]} + {"collection": mongo_item["collection"], "id": mongo_item["id"]} ) if existing_item: raise ConflictError( From 7f33e61ea3d55ff204c02c3f577078c9fd43ca75 Mon Sep 17 00:00:00 2001 From: pedro-cf Date: Mon, 22 Jul 2024 23:26:20 +0100 Subject: [PATCH 5/5] rename by mistake --- stac_fastapi/tests/resources/test_item.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stac_fastapi/tests/resources/test_item.py b/stac_fastapi/tests/resources/test_item.py index efc647a..3f650eb 100644 --- a/stac_fastapi/tests/resources/test_item.py +++ b/stac_fastapi/tests/resources/test_item.py @@ -34,7 +34,7 @@ def rfc3339_str_to_datetime(s: str) -> datetime: @pytest.mark.asyncio -async def test_create_item_indices(app_client, ctx, txn_client): +async def test_create_and_delete_item(app_client, ctx, txn_client): """Test creation and deletion of a single item (transactions extension)""" test_item = ctx.item