Skip to content

Commit 607e182

Browse files
jonhealy1robintwlossyrob
authored
Update get_item in sqlalchemy backend (#275)
* query by item and collection id * add get item test * add parameters * run pre-commit * Alter items table to have primary key on both id and collection_id * run pre-commit * run pre-commit * Update CHANGES Co-authored-by: Robin Wilson <[email protected]> Co-authored-by: Rob Emanuele <[email protected]>
1 parent 6cef4d4 commit 607e182

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
### Changed
1313

14+
* Update get_item in sqlalchemy backend to allow for querying for items with same ids but in different collections. ([#275](https://github.com/stac-utils/stac-fastapi/pull/275))
15+
1416
### Fixed
1517

1618
## [2.1.1]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Change pri key for Item
2+
3+
Revision ID: 821aa04011e8
4+
Revises: 407037cb1636
5+
Create Date: 2021-10-11 12:10:34.148098
6+
7+
"""
8+
from alembic import op
9+
10+
# revision identifiers, used by Alembic.
11+
revision = "821aa04011e8"
12+
down_revision = "407037cb1636"
13+
branch_labels = None
14+
depends_on = None
15+
16+
17+
def upgrade():
18+
op.drop_constraint("items_pkey", "items", schema="data")
19+
op.create_primary_key("items_pkey", "items", ["id", "collection_id"], schema="data")
20+
21+
22+
def downgrade():
23+
op.drop_constraint("items_pkey", "items", schema="data")
24+
op.create_primary_key("items_pkey", "items", ["id"], schema="data")

stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/core.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,12 @@ def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item:
174174
"""Get item by id."""
175175
base_url = str(kwargs["request"].base_url)
176176
with self.session.reader.context_session() as session:
177-
item = self._lookup_id(item_id, self.item_table, session)
177+
db_query = session.query(self.item_table)
178+
db_query = db_query.filter(self.item_table.collection_id == collection_id)
179+
db_query = db_query.filter(self.item_table.id == item_id)
180+
item = db_query.first()
181+
if not item:
182+
raise NotFoundError(f"{self.item_table.__name__} {id} not found")
178183
return self.item_serializer.db_to_stac(item, base_url=base_url)
179184

180185
def get_search(

stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/models/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Item(BaseModel): # type:ignore
6969
properties = sa.Column(JSONB)
7070
assets = sa.Column(JSONB)
7171
collection_id = sa.Column(
72-
sa.VARCHAR(1024), sa.ForeignKey(Collection.id), nullable=False
72+
sa.VARCHAR(1024), sa.ForeignKey(Collection.id), nullable=False, primary_key=True
7373
)
7474
parent_collection = sa.orm.relationship("Collection", back_populates="children")
7575
datetime = sa.Column(sa.TIMESTAMP(timezone=True), nullable=False)

stac_fastapi/sqlalchemy/tests/clients/test_postgres.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ def test_get_collection(
8585
assert coll["id"] == data["id"]
8686

8787

88+
def test_get_item(
89+
postgres_core: CoreCrudClient,
90+
postgres_transactions: TransactionsClient,
91+
load_test_data: Callable,
92+
):
93+
collection_data = load_test_data("test_collection.json")
94+
postgres_transactions.create_collection(
95+
collection_data, request=MockStarletteRequest
96+
)
97+
data = load_test_data("test_item.json")
98+
postgres_transactions.create_item(data, request=MockStarletteRequest)
99+
coll = postgres_core.get_item(
100+
item_id=data["id"],
101+
collection_id=data["collection"],
102+
request=MockStarletteRequest,
103+
)
104+
assert coll["id"] == data["id"]
105+
assert coll["collection"] == data["collection"]
106+
107+
88108
def test_get_collection_items(
89109
postgres_core: CoreCrudClient,
90110
postgres_transactions: TransactionsClient,

0 commit comments

Comments
 (0)