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(stripe): coerce incremental watermark to int before cursor compare (#65662)

Tom Owersa2adfc7d50da
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
index c1022a1a87af..4b41ea4561c0 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
@@ -145,6 +145,24 @@ def _is_stripe_resource_missing_error(error: stripe_lib.StripeError) -> bool:
return getattr(error, "code", None) == "resource_missing"
+def _coerce_incremental_cursor(value: Any) -> Optional[int]:
+ """Coerce a stored incremental watermark to the Unix-timestamp int that Stripe object
+ `created`/`date` fields are. The persisted watermark can come back as a numeric string, so
+ comparing it directly against the int field raises `'<=' not supported between instances of
+ 'int' and 'str'`. Returns None when the value can't be read as an int, so the caller skips
+ the cursor comparison rather than crashing."""
+ if isinstance(value, bool):
+ return None
+ if isinstance(value, int):
+ return value
+ if isinstance(value, float):
+ return int(value)
+ try:
+ return int(value)
+ except (TypeError, ValueError):
+ return None
+
+
def _stripe_base_addresses() -> BaseAddresses:
# Redirect Stripe API calls to a local mock (e.g. STRIPE_API_BASE=http://localhost:12111)
# when running the stripe-mock dev service. No-op in production where the var is unset.
@@ -410,8 +428,9 @@ def get_rows(
f"created[gt]": db_incremental_field_last_value,
},
)
+ last_value_cursor = _coerce_incremental_cursor(db_incremental_field_last_value)
for obj in stripe_objects.auto_paging_iter():
- if obj[incremental_field_name] <= db_incremental_field_last_value:
+ if last_value_cursor is not None and obj[incremental_field_name] <= last_value_cursor:
break
yield obj
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
index 7b5840a23b67..dc7d7a3bcc19 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
@@ -1,8 +1,11 @@
+from typing import Any, cast
+
import pytest
from unittest import mock
from unittest.mock import MagicMock, patch
import stripe as stripe_lib
+from stripe import ListObject
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs import (
StripeAuthMethodConfig,
@@ -21,6 +24,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.stripe.str
StripeNestedResource,
StripeResource,
_all_known_webhook_events,
+ _coerce_incremental_cursor,
get_rows,
)
@@ -31,6 +35,62 @@ def _list_object(items):
return obj
+class _FakeStripeList:
+ def __init__(self, objects):
+ self._objects = objects
+
+ def auto_paging_iter(self):
+ return iter(self._objects)
+
+
+class TestStripeGetRowsIncrementalCursor:
+ @pytest.mark.parametrize(
+ "value,expected",
+ [
+ (1700000000, 1700000000),
+ (1700000000.0, 1700000000),
+ # The persisted watermark is read back from JSON config as a numeric string.
+ ("1700000000", 1700000000),
+ (None, None),
+ ("not-a-timestamp", None),
+ (True, None),
+ ],
+ )
+ def test_coerce_incremental_cursor(self, value, expected):
+ assert _coerce_incremental_cursor(value) == expected
+
+ def test_get_rows_handles_string_incremental_watermark(self):
+ # Stripe object timestamps are ints, but the stored watermark can come back as a numeric
+ # string. The cursor comparison must not crash with `'<=' not supported between instances
+ # of 'int' and 'str'`, and must still stop once it reaches an object at/under the watermark.
+ objects = [
+ {"id": "ch_2", "created": 1700000100},
+ {"id": "ch_1", "created": 1700000040},
+ ]
+ resource = StripeResource(method=lambda params: cast(ListObject[Any], _FakeStripeList(objects)))
+ resumable_source_manager = mock.MagicMock()
+ resumable_source_manager.can_resume.return_value = False
+
+ with mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.stripe.stripe._build_resources",
+ return_value={"charge": resource},
+ ):
+ rows = list(
+ get_rows(
+ api_key="sk_test_123",
+ endpoint="charge",
+ account_id=None,
+ db_incremental_field_last_value="1700000050",
+ db_incremental_field_earliest_value=None,
+ logger=mock.MagicMock(),
+ resumable_source_manager=resumable_source_manager,
+ should_use_incremental_field=True,
+ )
+ )
+
+ assert [obj["id"] for obj in rows] == ["ch_2"]
+
+
class TestStripeSource:
def setup_method(self):
self.source = StripeSource()