: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(data-warehouse): skip chargebee cursor filter until a watermark exists (#103163)
Vardhman Bengani0757cc327a37
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/chargebee/chargebee.py b/products/warehouse_sources/backend/temporal/data_imports/sources/chargebee/chargebee.pyindex 54a213c3abf8..07b63c3b279a 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/chargebee/chargebee.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/chargebee/chargebee.py@@ -27,7 +27,17 @@ def incremental_param(cursor_path: str) -> dict[str, Any]:}-def get_resource(name: str, should_use_incremental_field: bool) -> EndpointResource:+def get_resource(+ name: str, should_use_incremental_field: bool, db_incremental_field_last_value: Optional[Any] = None+) -> EndpointResource:+ # Chargebee only guarantees `updated_at`/`occurred_at` on records updated after+ # 2016-09-28 and omits it on some older, never-updated records (e.g. voided+ # authorization transactions). Attaching the cursor filter with its initial value+ # (0) on the first sync excludes those records server-side, and no later resync+ # recovers them because the filter is sent again. Only send the filter once a real+ # watermark exists from a previous sync; the watermark is computed from synced+ # rows, so the first unfiltered sync still advances it.+ apply_incremental_filter = should_use_incremental_field and db_incremental_field_last_value is not Noneresources: dict[str, EndpointResource] = {"Customers": {"name": "Customers",@@ -43,7 +53,7 @@ def get_resource(name: str, should_use_incremental_field: bool) -> EndpointResou"path": "/v2/customers","params": {# the parameters below can optionally be configured- "updated_at[after]": incremental_param("updated_at") if should_use_incremental_field else None,+ "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,"limit": 100,# by default, API does not return deleted resources"include_deleted": "true",@@ -67,7 +77,7 @@ def get_resource(name: str, should_use_incremental_field: bool) -> EndpointResou"path": "/v2/events","params": {# the parameters below can optionally be configured- "occurred_at[after]": incremental_param("occurred_at") if should_use_incremental_field else None,+ "occurred_at[after]": incremental_param("occurred_at") if apply_incremental_filter else None,"limit": 100,},},@@ -87,7 +97,7 @@ def get_resource(name: str, should_use_incremental_field: bool) -> EndpointResou"path": "/v2/invoices","params": {# the parameters below can optionally be configured- "updated_at[after]": incremental_param("updated_at") if should_use_incremental_field else None,+ "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,"limit": 100,# by default, API does not return deleted resources"include_deleted": "true",@@ -109,7 +119,7 @@ def get_resource(name: str, should_use_incremental_field: bool) -> EndpointResou"path": "/v2/orders","params": {# the parameters below can optionally be configured- "updated_at[after]": incremental_param("updated_at") if should_use_incremental_field else None,+ "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,"limit": 100,# by default, API does not return deleted resources"include_deleted": "true",@@ -131,7 +141,7 @@ def get_resource(name: str, should_use_incremental_field: bool) -> EndpointResou"path": "/v2/subscriptions","params": {# the parameters below can optionally be configured- "updated_at[after]": incremental_param("updated_at") if should_use_incremental_field else None,+ "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,"limit": 100,# by default, API does not return deleted resources"include_deleted": "true",@@ -153,7 +163,7 @@ def get_resource(name: str, should_use_incremental_field: bool) -> EndpointResou"path": "/v2/transactions","params": {# the parameters below can optionally be configured- "updated_at[after]": incremental_param("updated_at") if should_use_incremental_field else None,+ "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,"limit": 100,# by default, API does not return deleted resources"include_deleted": "true",@@ -238,7 +248,7 @@ def chargebee_source(if should_use_incremental_fieldelse "replace",},- "resources": [get_resource(endpoint, should_use_incremental_field)],+ "resources": [get_resource(endpoint, should_use_incremental_field, db_incremental_field_last_value)],}initial_paginator_state: Optional[dict[str, Any]] = Nonediff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/chargebee/tests/test_chargebee.py b/products/warehouse_sources/backend/temporal/data_imports/sources/chargebee/tests/test_chargebee.pyindex 835f94e057e0..e1abebca3a63 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/chargebee/tests/test_chargebee.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/chargebee/tests/test_chargebee.py@@ -263,3 +263,73 @@ class TestChargebeeSiteNameValidation:assert is_valid is Falseassert message is not None and "chargebee.com" in messagemock_validate.assert_not_called()+++class TestChargebeeIncrementalFilter:+ """The server-side cursor filter must only be sent once a real watermark exists (#76090).++ Chargebee omits `updated_at`/`occurred_at` on some records (e.g. voided authorization+ transactions), and its API excludes those records whenever the filter param is present.+ Sending the filter with the initial value 0 on the first sync therefore drops those+ records permanently. The first incremental sync must go out unfiltered; the pipeline+ still advances the watermark from the synced rows.+ """++ CURSOR_PARAMS = {+ "Customers": "updated_at[after]",+ "Events": "occurred_at[after]",+ "Invoices": "updated_at[after]",+ "Orders": "updated_at[after]",+ "Subscriptions": "updated_at[after]",+ "Transactions": "updated_at[after]",+ }++ def _drive(self, endpoint: str, *, incremental: bool, last_value: Any) -> list[dict[str, Any]]:+ sent_params: list[dict[str, Any]] = []+ response_iter = iter([_make_http_response({"list": [{"customer": {"id": "c1"}}]})])++ def fake_send(request: Any, *_args: Any, **_kwargs: Any) -> Response:+ sent_params.append(dict(request.params or {}))+ return next(response_iter)++ manager = MagicMock(spec=ResumableSourceManager)+ manager.can_resume.return_value = False++ with patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client.make_tracked_session"+ ) as MockSession:+ mock_session = MockSession.return_value+ mock_session.headers = {}+ mock_session.prepare_request.side_effect = lambda req: req+ mock_session.send.side_effect = fake_send++ resource = chargebee_source(+ api_key="test-key",+ site_name="site-test",+ endpoint=endpoint,+ team_id=123,+ job_id="test_job",+ resumable_source_manager=manager,+ db_incremental_field_last_value=last_value,+ should_use_incremental_field=incremental,+ )+ list(cast(Iterable[Any], resource))+ return sent_params++ @pytest.mark.parametrize("endpoint", sorted(CURSOR_PARAMS))+ def test_first_incremental_sync_omits_cursor_filter(self, endpoint: str) -> None:+ sent_params = self._drive(endpoint, incremental=True, last_value=None)++ assert self.CURSOR_PARAMS[endpoint] not in sent_params[0]++ @pytest.mark.parametrize("endpoint", sorted(CURSOR_PARAMS))+ def test_incremental_sync_with_watermark_sends_cursor_filter(self, endpoint: str) -> None:+ sent_params = self._drive(endpoint, incremental=True, last_value=1750000000)++ assert sent_params[0][self.CURSOR_PARAMS[endpoint]] == 1750000000++ @pytest.mark.parametrize("endpoint", sorted(CURSOR_PARAMS))+ def test_full_refresh_never_sends_cursor_filter(self, endpoint: str) -> None:+ sent_params = self._drive(endpoint, incremental=False, last_value=1750000000)++ assert self.CURSOR_PARAMS[endpoint] not in sent_params[0]