: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(plain): use Plain's `after` datetime filter for incremental syncs (#63059)
Tom Owers878b024cfe2c
diff --git a/posthog/temporal/data_imports/sources/plain/plain.py b/posthog/temporal/data_imports/sources/plain/plain.pyindex b0aa95e34d3a..7ecc2e99a4cc 100644--- a/posthog/temporal/data_imports/sources/plain/plain.py+++ b/posthog/temporal/data_imports/sources/plain/plain.py@@ -37,6 +37,16 @@ def _datetime_to_plain_iso8601(value: datetime) -> str:return value.isoformat().replace("+00:00", "Z")+def _updated_at_filter(updated_at_gte: datetime) -> dict[str, Any]:+ """Build Plain's server-side ``updatedAt`` filter for incremental syncs.++ Plain's ``DatetimeFilter`` exposes ``after`` (>= the given timestamp) and ``before`` (< it);+ it has no ``gte`` field, so sending one is rejected with a 400. ``after`` matches the inclusive+ lower-bound semantics we want for incremental syncs.+ """+ return {"updatedAt": {"after": _datetime_to_plain_iso8601(updated_at_gte)}}++def _parse_plain_datetime(value: str | datetime | None) -> datetime | None:if value is None:return None@@ -235,7 +245,7 @@ def _fetch_paginated_endpoint(variables: dict[str, Any] = {"first": PLAIN_DEFAULT_PAGE_SIZE}if updated_at_gte is not None:- variables["filter"] = {"updatedAt": {"gte": _datetime_to_plain_iso8601(updated_at_gte)}}+ variables["filter"] = _updated_at_filter(updated_at_gte)has_next_page = Truewhile has_next_page:@@ -267,7 +277,7 @@ def _fetch_timeline_entries("""variables: dict[str, Any] = {"first": PLAIN_DEFAULT_PAGE_SIZE}if created_at_gte is not None:- variables["filter"] = {"updatedAt": {"gte": _datetime_to_plain_iso8601(created_at_gte)}}+ variables["filter"] = _updated_at_filter(created_at_gte)has_next_page = Truewhile has_next_page:diff --git a/posthog/temporal/data_imports/sources/plain/tests/test_plain.py b/posthog/temporal/data_imports/sources/plain/tests/test_plain.pyindex e70dffc2e28b..7f540e68a787 100644--- a/posthog/temporal/data_imports/sources/plain/tests/test_plain.py+++ b/posthog/temporal/data_imports/sources/plain/tests/test_plain.py@@ -8,6 +8,7 @@ import requestsfrom posthog.temporal.data_imports.sources.plain.plain import (PlainRetryableError,_datetime_to_plain_iso8601,+ _fetch_paginated_endpoint,_fetch_thread_timeline_entries,_fetch_timeline_entries,_flatten_datetime,@@ -340,6 +341,51 @@ class TestTimelineEntryIncrementalFilter:assert [e["id"] for e in pages[0]] == ["te_null"]+class TestFetchPaginatedEndpointIncrementalFilter:+ def test_sends_after_filter_when_incremental(self):+ recorded = []++ def execute(query, variables):+ recorded.append((query, dict(variables)))+ return {"data": {"customers": {"edges": [], "pageInfo": {"hasNextPage": False, "endCursor": None}}}}++ list(+ _fetch_paginated_endpoint(+ execute,+ endpoint_name="customers",+ query="query PaginatedCustomers { customers { edges { node { id } } } }",+ logger=mock.MagicMock(),+ updated_at_gte=datetime(2024, 1, 15, 10, 30, 0, tzinfo=UTC),+ )+ )++ assert recorded, "expected customers query to be issued"+ _, variables = recorded[0]+ # Plain's DatetimeFilter uses `after` (>=), not `gte` — sending `gte` is rejected with a 400.+ assert variables["filter"] == {"updatedAt": {"after": "2024-01-15T10:30:00Z"}}++ def test_omits_filter_for_full_sync(self):+ recorded = []++ def execute(query, variables):+ recorded.append((query, dict(variables)))+ return {"data": {"customers": {"edges": [], "pageInfo": {"hasNextPage": False, "endCursor": None}}}}++ list(+ _fetch_paginated_endpoint(+ execute,+ endpoint_name="customers",+ query="query PaginatedCustomers { customers { edges { node { id } } } }",+ logger=mock.MagicMock(),+ updated_at_gte=None,+ )+ )++ assert recorded+ _, variables = recorded[0]+ assert "filter" not in variables++class TestFetchTimelineEntriesStreaming:def test_sends_updatedat_filter_when_incremental(self):recorded = []@@ -360,7 +406,8 @@ class TestFetchTimelineEntriesStreaming:assert recorded, "expected threads query to be issued"_, variables = recorded[0]- assert variables["filter"] == {"updatedAt": {"gte": "2024-01-15T10:30:00Z"}}+ # Plain's DatetimeFilter uses `after` (>=), not `gte` — sending `gte` is rejected with a 400.+ assert variables["filter"] == {"updatedAt": {"after": "2024-01-15T10:30:00Z"}}def test_streams_thread_pages_without_buffering_all_ids(self):executed_queries: list[str] = []