Skip to content

Update get_item in sqlalchemy backend #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 19, 2021
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

### Changed

* 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))

### Fixed

## [2.1.1]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Change pri key for Item

Revision ID: 821aa04011e8
Revises: 407037cb1636
Create Date: 2021-10-11 12:10:34.148098

"""
from alembic import op

# revision identifiers, used by Alembic.
revision = "821aa04011e8"
down_revision = "407037cb1636"
branch_labels = None
depends_on = None


def upgrade():
op.drop_constraint("items_pkey", "items", schema="data")
op.create_primary_key("items_pkey", "items", ["id", "collection_id"], schema="data")


def downgrade():
op.drop_constraint("items_pkey", "items", schema="data")
op.create_primary_key("items_pkey", "items", ["id"], schema="data")
7 changes: 6 additions & 1 deletion stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item:
"""Get item by id."""
base_url = str(kwargs["request"].base_url)
with self.session.reader.context_session() as session:
item = self._lookup_id(item_id, self.item_table, session)
db_query = session.query(self.item_table)
db_query = db_query.filter(self.item_table.collection_id == collection_id)
db_query = db_query.filter(self.item_table.id == item_id)
item = db_query.first()
if not item:
raise NotFoundError(f"{self.item_table.__name__} {id} not found")
return self.item_serializer.db_to_stac(item, base_url=base_url)

def get_search(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Item(BaseModel): # type:ignore
properties = sa.Column(JSONB)
assets = sa.Column(JSONB)
collection_id = sa.Column(
sa.VARCHAR(1024), sa.ForeignKey(Collection.id), nullable=False
sa.VARCHAR(1024), sa.ForeignKey(Collection.id), nullable=False, primary_key=True
)
parent_collection = sa.orm.relationship("Collection", back_populates="children")
datetime = sa.Column(sa.TIMESTAMP(timezone=True), nullable=False)
Expand Down
20 changes: 20 additions & 0 deletions stac_fastapi/sqlalchemy/tests/clients/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ def test_get_collection(
assert coll["id"] == data["id"]


def test_get_item(
postgres_core: CoreCrudClient,
postgres_transactions: TransactionsClient,
load_test_data: Callable,
):
collection_data = load_test_data("test_collection.json")
postgres_transactions.create_collection(
collection_data, request=MockStarletteRequest
)
data = load_test_data("test_item.json")
postgres_transactions.create_item(data, request=MockStarletteRequest)
coll = postgres_core.get_item(
item_id=data["id"],
collection_id=data["collection"],
request=MockStarletteRequest,
)
assert coll["id"] == data["id"]
assert coll["collection"] == data["collection"]


def test_get_collection_items(
postgres_core: CoreCrudClient,
postgres_transactions: TransactionsClient,
Expand Down