:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(annotations): give the list endpoint a stable pagination order (#102089)
posthog[bot]60bc914abb46
diff --git a/products/annotations/backend/api/annotation.py b/products/annotations/backend/api/annotation.pyindex 0fa475e477e3..a6f6f33b2ea5 100644--- a/products/annotations/backend/api/annotation.py+++ b/products/annotations/backend/api/annotation.py@@ -195,7 +195,8 @@ class AnnotationsViewSet(TeamAndOrgViewSetMixin, ForbidDestroyModel, viewsets.Modef safely_get_queryset(self, queryset) -> QuerySet:if self.action == "list":- queryset = queryset.order_by("-date_marker")+ # `date_marker` alone is not a total order, so limit-offset pages can repeat or drop rows.+ queryset = queryset.order_by("-date_marker", "-id")if self.action != "partial_update":# We never want deleted items to be included in the queryset… except when we want to restore an annotation# That's because annotations are restored with a PATCH request setting `deleted` to `False`diff --git a/products/annotations/backend/api/test/__snapshots__/test_annotation.ambr b/products/annotations/backend/api/test/__snapshots__/test_annotation.ambrindex 04731d55ad92..815456378556 100644--- a/products/annotations/backend/api/test/__snapshots__/test_annotation.ambr+++ b/products/annotations/backend/api/test/__snapshots__/test_annotation.ambr@@ -520,7 +520,8 @@AND (("posthog_annotation"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuidAND "posthog_annotation"."scope" = 'organization')OR "posthog_annotation"."team_id" = 99999))- ORDER BY "posthog_annotation"."date_marker" DESC+ ORDER BY "posthog_annotation"."date_marker" DESC,+ "posthog_annotation"."id" DESCLIMIT 1000'''# ---@@ -1187,7 +1188,8 @@AND (("posthog_annotation"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuidAND "posthog_annotation"."scope" = 'organization')OR "posthog_annotation"."team_id" = 99999))- ORDER BY "posthog_annotation"."date_marker" DESC+ ORDER BY "posthog_annotation"."date_marker" DESC,+ "posthog_annotation"."id" DESCLIMIT 1000'''# ---@@ -1727,7 +1729,8 @@AND (("posthog_annotation"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuidAND "posthog_annotation"."scope" = 'organization')OR "posthog_annotation"."team_id" = 99999))- ORDER BY "posthog_annotation"."date_marker" DESC+ ORDER BY "posthog_annotation"."date_marker" DESC,+ "posthog_annotation"."id" DESCLIMIT 1000'''# ---diff --git a/products/annotations/backend/api/test/test_annotation.py b/products/annotations/backend/api/test/test_annotation.pyindex d308fd3c11be..32f9e0271f41 100644--- a/products/annotations/backend/api/test/test_annotation.py+++ b/products/annotations/backend/api/test/test_annotation.py@@ -35,6 +35,36 @@ class TestAnnotation(APIBaseTest, QueryMatchingTest):assert len(response["results"]) == 1assert response["results"][0]["content"] == "hello world!"+ def test_paginated_list_is_stable_when_annotations_share_a_date_marker(self) -> None:+ # Insertion order is the reverse of the order the endpoint must return, so a page walk that+ # falls back to row order returns a different sequence.+ earlier = [+ Annotation.objects.create(+ organization=self.organization,+ team=self.team,+ content=f"earlier {index}",+ date_marker=datetime(2024, 1, 1, 0, 0, 0, tzinfo=ZoneInfo("UTC")),+ )+ for index in range(2)+ ]+ later = [+ Annotation.objects.create(+ organization=self.organization,+ team=self.team,+ content=f"later {index}",+ date_marker=datetime(2024, 1, 2, 0, 0, 0, tzinfo=ZoneInfo("UTC")),+ )+ for index in range(2)+ ]++ paged_ids = []+ for offset in (0, 2):+ response = self.client.get(f"/api/projects/{self.team.id}/annotations/?limit=2&offset={offset}")+ assert response.status_code == status.HTTP_200_OK+ paged_ids += [result["id"] for result in response.json()["results"]]++ assert paged_ids == [later[1].id, later[0].id, earlier[1].id, earlier[0].id]+@patch("products.annotations.backend.activity_logging.report_user_action")def test_retrieving_annotation_is_not_n_plus_1(self, _mock_capture: MagicMock) -> None:with self.assertNumQueries(FuzzyInt(9, 10)), snapshot_postgres_queries_context(self):