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(square): repeat original query params on paginated requests (#71332)

Tom Owers4a6b4204854b
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/square/square.py b/products/warehouse_sources/backend/temporal/data_imports/sources/square/square.py
index e2a5c5eb642f..969bb20e8f71 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/square/square.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/square/square.py
@@ -195,9 +195,10 @@ def get_rows(
restarts_remaining = MAX_CURSOR_RESTARTS
while True:
- # Square encodes the original query in the cursor, so subsequent pages are
- # requested with the cursor alone — re-sending filters/sort can error.
- params = {"cursor": cursor} if cursor else initial_params
+ # Square ties a cursor to the exact query it was issued for, so every follow-up
+ # request must repeat the original params and add the cursor. Sending the cursor
+ # alone drops sort/filter/limit, which Square rejects as an incompatible cursor.
+ params = {**initial_params, "cursor": cursor} if cursor else initial_params
try:
data = fetch_page(params)
except SquareInvalidCursorError:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/square/test_square.py b/products/warehouse_sources/backend/temporal/data_imports/sources/square/test_square.py
index 51741979ff9b..a8af5d6c643e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/square/test_square.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/square/test_square.py
@@ -205,10 +205,12 @@ class TestGetRowsPagination:
]
sent_params = self._drive("payments", manager, responses)
- # First request carries the query params; subsequent pages carry cursor only.
- assert "cursor" not in sent_params[0]
- assert sent_params[1] == {"cursor": "cur-1"}
- assert sent_params[2] == {"cursor": "cur-2"}
+ # First request carries the query params; follow-up pages repeat them unchanged
+ # and add the cursor. Square rejects a cursor sent with a changed parameter set.
+ base = {"sort_order": "ASC", "limit": "50"}
+ assert sent_params[0] == base
+ assert sent_params[1] == {**base, "cursor": "cur-1"}
+ assert sent_params[2] == {**base, "cursor": "cur-2"}
saved = [call.args[0] for call in manager.save_state.call_args_list]
assert saved == [SquareResumeConfig(cursor="cur-1"), SquareResumeConfig(cursor="cur-2")]
@@ -221,7 +223,7 @@ class TestGetRowsPagination:
responses = [_make_response({"payments": [{"id": "p9"}]})]
sent_params = self._drive("payments", manager, responses)
- assert sent_params == [{"cursor": "cur-resumed"}]
+ assert sent_params == [{"sort_order": "ASC", "limit": "50", "cursor": "cur-resumed"}]
manager.load_state.assert_called_once()
def test_terminal_single_page_does_not_save_state(self) -> None:
@@ -264,10 +266,12 @@ class TestGetRowsPagination:
sent_params = self._drive("customers", manager, responses)
# First request uses the stale cursor and is rejected; the retry drops the
- # cursor and re-issues the original query, then pages normally.
- assert sent_params[0] == {"cursor": "stale-cursor"}
- assert "cursor" not in sent_params[1]
- assert sent_params[2] == {"cursor": "cur-1"}
+ # cursor and re-issues the original query, then pages normally. Every request
+ # carries the original query params, with the cursor added on follow-ups.
+ base = {"sort_field": "CREATED_AT", "sort_order": "ASC", "limit": "50"}
+ assert sent_params[0] == {**base, "cursor": "stale-cursor"}
+ assert sent_params[1] == base
+ assert sent_params[2] == {**base, "cursor": "cur-1"}
def test_invalid_cursor_restart_evicts_stale_cursor_within_single_page(self) -> None:
# When the restart finishes within a single page there's no fresh next_cursor
@@ -301,7 +305,7 @@ class TestGetRowsPagination:
sent_params = self._drive("payments", manager, responses)
assert "cursor" not in sent_params[0]
- assert sent_params[1] == {"cursor": "cur-1"}
+ assert sent_params[1] == {"sort_order": "ASC", "limit": "50", "cursor": "cur-1"}
# The restart drops the cursor and seeds begin_time from the last seen created_at
# rather than issuing a bare full restart.
assert "cursor" not in sent_params[2]
@@ -325,9 +329,10 @@ class TestGetRowsPagination:
sent_params = self._drive("customers", manager, responses)
# stale cursor rejected -> restart -> page (cur-1) -> cur-1 rejected -> restart -> final page.
- assert sent_params[0] == {"cursor": "stale-cursor"}
+ base = {"sort_field": "CREATED_AT", "sort_order": "ASC", "limit": "50"}
+ assert sent_params[0] == {**base, "cursor": "stale-cursor"}
assert "cursor" not in sent_params[1]
- assert sent_params[2] == {"cursor": "cur-1"}
+ assert sent_params[2] == {**base, "cursor": "cur-1"}
assert "cursor" not in sent_params[3]
def test_invalid_cursor_gives_up_after_restart_budget_exhausted(self) -> None: