Skip to content

Commit

Permalink
fix: duplicate revisions annotation enpdoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cakeinsauce committed Sep 24, 2024
1 parent 9ea8c63 commit f1f64e5
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions annotation/annotation/annotations/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Response,
status,
)
from sqlalchemy import and_, desc
from sqlalchemy import and_, desc, func
from sqlalchemy.orm import Session
from tenant_dependency import TenantData

Expand Down Expand Up @@ -89,15 +89,22 @@ async def get_annotations(
request.dict(), AnnotatedDoc.__name__
)
# Distinct on revision, filter_lib doesn't work right with
# distinct and sorting
subquery = (
db.query(AnnotatedDoc)
.filter(AnnotatedDoc.tenant == x_current_tenant)
.distinct(AnnotatedDoc.revision)
# distinct and sorting. Prioritize latest revisions first
revision_subquery = (
db.query(
AnnotatedDoc.revision, func.max(AnnotatedDoc.date).label("latest")
)
.group_by(AnnotatedDoc.revision)
.subquery()
)
query = db.query(AnnotatedDoc).join(
subquery, AnnotatedDoc.revision == subquery.c.revision
query = (
db.query(AnnotatedDoc)
.join(
revision_subquery,
(AnnotatedDoc.revision == revision_subquery.c.revision)
& (AnnotatedDoc.date == revision_subquery.c.latest),
)
.filter(AnnotatedDoc.tenant == x_current_tenant)
)
query, pagination = filter_lib.form_query(filter_args, query)
return filter_lib.paginate(
Expand Down

0 comments on commit f1f64e5

Please sign in to comment.