Skip to content

: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.

fix(flags): make the scheduled changes list order total (#102063)

posthog[bot]0415592e221a
diff --git a/products/feature_flags/backend/api/scheduled_change.py b/products/feature_flags/backend/api/scheduled_change.py
index f396b9fdd2ee..46349a9ba389 100644
--- a/products/feature_flags/backend/api/scheduled_change.py
+++ b/products/feature_flags/backend/api/scheduled_change.py
@@ -371,7 +371,8 @@ class ScheduledChangeViewSet(ApprovalHandlingMixin, TeamAndOrgViewSetMixin, view
scope_object = "feature_flag"
serializer_class = ScheduledChangeSerializer
# select_related keeps the serializer's nested change_request and created_by from N+1-ing list responses.
- queryset = ScheduledChange.objects.select_related("change_request", "created_by").all()
+ # id breaks scheduled_at ties, so limit-offset pages cannot repeat or drop a schedule.
+ queryset = ScheduledChange.objects.select_related("change_request", "created_by").order_by("scheduled_at", "id")
@extend_schema(
parameters=[
diff --git a/products/feature_flags/backend/api/test/test_scheduled_change.py b/products/feature_flags/backend/api/test/test_scheduled_change.py
index 19011430261c..cd750adfb052 100644
--- a/products/feature_flags/backend/api/test/test_scheduled_change.py
+++ b/products/feature_flags/backend/api/test/test_scheduled_change.py
@@ -331,6 +331,40 @@ class TestScheduledChange(APIBaseTest):
assert result["is_recurring"] is True
assert result["recurrence_interval"] == "monthly"
+ def test_paged_list_walks_every_schedule_in_scheduled_at_order(self):
+ feature_flag = FeatureFlag.objects.create(
+ team=self.team, created_by=self.user, key="paged-flag", name="Paged Flag"
+ )
+
+ def make_schedule(schedule_id: int, scheduled_at: str) -> None:
+ ScheduledChange.objects.create(
+ id=schedule_id,
+ team=self.team,
+ record_id=feature_flag.id,
+ model_name="FeatureFlag",
+ payload={"operation": "update_status", "value": False},
+ scheduled_at=scheduled_at,
+ created_by=self.user,
+ )
+
+ # Insert in the exact reverse of the order the endpoint owes, with explicit descending ids,
+ # so neither sort term agrees with insertion order. Dropping scheduled_at reverses the pairs
+ # and dropping the id tie-breaker reverses within a pair, so both show up as a bad walk.
+ earlier, later = "2024-03-01T09:00:00Z", "2024-03-02T09:00:00Z"
+ for schedule_id, scheduled_at in ((9004, later), (9003, later), (9002, earlier), (9001, earlier)):
+ make_schedule(schedule_id, scheduled_at)
+
+ paged_ids = []
+ for offset in (0, 2):
+ response = self.client.get(
+ f"/api/projects/{self.team.id}/scheduled_changes/",
+ data={"record_id": str(feature_flag.id), "limit": 2, "offset": offset},
+ )
+ assert response.status_code == status.HTTP_200_OK, response.json()
+ paged_ids += [result["id"] for result in response.json()["results"]]
+
+ assert paged_ids == [9001, 9002, 9003, 9004]
+
def test_cannot_update_record_id(self):
"""Updating record_id is rejected to prevent cross-tenant manipulation."""
feature_flag = FeatureFlag.objects.create(