Skip to content

Commit 9b64acc

Browse files
authored
Merge pull request #2915 from BrandonKowalski/feature/collection-updated-after-query-param
Feature/collection-updated-after-query-param
2 parents cddb38c + 842e627 commit 9b64acc

3 files changed

Lines changed: 69 additions & 8 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Add indexes on updated_at for collections and smart_collections tables
2+
3+
Revision ID: 0065_collections_updated_at_idx
4+
Revises: 0064_add_updated_at_indexes
5+
Create Date: 2026-01-17
6+
7+
"""
8+
9+
from alembic import op
10+
11+
# revision identifiers, used by Alembic.
12+
revision = "0065_collections_updated_at_idx"
13+
down_revision = "0064_add_updated_at_indexes"
14+
branch_labels = None
15+
depends_on = None
16+
17+
18+
def upgrade():
19+
with op.batch_alter_table("collections", schema=None) as batch_op:
20+
batch_op.create_index("ix_collections_updated_at", ["updated_at"], unique=False)
21+
22+
with op.batch_alter_table("smart_collections", schema=None) as batch_op:
23+
batch_op.create_index(
24+
"ix_smart_collections_updated_at", ["updated_at"], unique=False
25+
)
26+
27+
28+
def downgrade():
29+
with op.batch_alter_table("collections", schema=None) as batch_op:
30+
batch_op.drop_index("ix_collections_updated_at")
31+
32+
with op.batch_alter_table("smart_collections", schema=None) as batch_op:
33+
batch_op.drop_index("ix_smart_collections_updated_at")

backend/endpoints/collections.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import json
2+
from datetime import datetime
23
from io import BytesIO
34
from typing import Annotated
45

56
from fastapi import Path as PathVar
6-
from fastapi import Request, UploadFile, status
7+
from fastapi import Query, Request, UploadFile, status
78

89
from decorators.auth import protected_route
910
from endpoints.responses.collection import (
@@ -144,18 +145,26 @@ async def add_smart_collection(
144145

145146

146147
@protected_route(router.get, "", [Scope.COLLECTIONS_READ])
147-
def get_collections(request: Request) -> list[CollectionSchema]:
148+
def get_collections(
149+
request: Request,
150+
updated_after: Annotated[
151+
datetime | None,
152+
Query(
153+
description="Filter collections updated after this datetime (ISO 8601 format with timezone information)."
154+
),
155+
] = None,
156+
) -> list[CollectionSchema]:
148157
"""Get collections endpoint
149158
150159
Args:
151160
request (Request): Fastapi Request object
152-
id (int, optional): Collection id. Defaults to None.
161+
updated_after: Filter collections updated after this datetime
153162
154163
Returns:
155164
list[CollectionSchema]: List of collections
156165
"""
157166

158-
collections = db_collection_handler.get_collections()
167+
collections = db_collection_handler.get_collections(updated_after=updated_after)
159168

160169
return CollectionSchema.for_user(request.user.id, [c for c in collections])
161170

@@ -181,17 +190,28 @@ def get_virtual_collections(
181190

182191

183192
@protected_route(router.get, "/smart", [Scope.COLLECTIONS_READ])
184-
def get_smart_collections(request: Request) -> list[SmartCollectionSchema]:
193+
def get_smart_collections(
194+
request: Request,
195+
updated_after: Annotated[
196+
datetime | None,
197+
Query(
198+
description="Filter smart collections updated after this datetime (ISO 8601 format with timezone information)."
199+
),
200+
] = None,
201+
) -> list[SmartCollectionSchema]:
185202
"""Get smart collections endpoint
186203
187204
Args:
188205
request (Request): Fastapi Request object
206+
updated_after: Filter smart collections updated after this datetime
189207
190208
Returns:
191209
list[SmartCollectionSchema]: List of smart collections
192210
"""
193211

194-
smart_collections = db_collection_handler.get_smart_collections(request.user.id)
212+
smart_collections = db_collection_handler.get_smart_collections(
213+
request.user.id, updated_after=updated_after
214+
)
195215

196216
return SmartCollectionSchema.for_user(
197217
request.user.id, [s for s in smart_collections]
@@ -216,7 +236,7 @@ def get_collection(request: Request, id: int) -> CollectionSchema:
216236

217237
if collection.user_id != request.user.id and not collection.is_public:
218238
raise CollectionPermissionError(id)
219-
239+
220240
return CollectionSchema.model_validate(collection)
221241

222242

@@ -414,7 +434,7 @@ async def delete_collection(
414434
collection = db_collection_handler.get_collection(id)
415435
if not collection:
416436
raise CollectionNotFoundInDatabaseException(id)
417-
437+
418438
if collection.user_id != request.user.id:
419439
raise CollectionPermissionError(id)
420440

backend/handler/database/collections_handler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
from collections.abc import Sequence
3+
from datetime import datetime
34
from typing import Any
45

56
from sqlalchemy import delete, insert, literal, or_, select, update
@@ -85,9 +86,12 @@ def get_favorite_collection(
8586
@with_roms
8687
def get_collections(
8788
self,
89+
updated_after: datetime | None = None,
8890
query: Query = None, # type: ignore
8991
session: Session = None, # type: ignore
9092
) -> Sequence[Collection]:
93+
if updated_after:
94+
query = query.filter(Collection.updated_at > updated_after)
9195
return session.scalars(query.order_by(Collection.name.asc())).unique().all()
9296

9397
@begin_session
@@ -201,6 +205,7 @@ def get_smart_collection_by_name(
201205
def get_smart_collections(
202206
self,
203207
user_id: int | None = None,
208+
updated_after: datetime | None = None,
204209
session: Session = None, # type: ignore
205210
) -> Sequence[SmartCollection]:
206211
query = select(SmartCollection).order_by(SmartCollection.name.asc())
@@ -211,6 +216,9 @@ def get_smart_collections(
211216
(SmartCollection.user_id == user_id) | SmartCollection.is_public
212217
)
213218

219+
if updated_after:
220+
query = query.filter(SmartCollection.updated_at > updated_after)
221+
214222
return session.scalars(query).unique().all()
215223

216224
@begin_session

0 commit comments

Comments
 (0)