: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): tolerate string team ids from the control plane (#72846)
Benjamin Knofe-Vider4607fc26c1a2
diff --git a/posthog/dags/events_backfill_to_duckling.py b/posthog/dags/events_backfill_to_duckling.pyindex 7ea4d31a33d4..80234a36ef9f 100644--- a/posthog/dags/events_backfill_to_duckling.py+++ b/posthog/dags/events_backfill_to_duckling.py@@ -3018,7 +3018,14 @@ def _reconcile_earliest_event_dates_with_cp(backfills: list[DuckgresServerTeam])teams = managed_warehouse._teams_from_response(managed_warehouse.list_teams(org_id, require_enabled=False))if teams is None:continue- cp_dates = {row.get("team_id"): row.get("earliest_event_date") for row in teams}+ # Coerce team ids to int: a control plane serializing them as JSON strings+ # would otherwise make every team look unknown and silently skip the repair.+ cp_dates = {}+ for row in teams:+ try:+ cp_dates[int(row["team_id"])] = row.get("earliest_event_date")+ except (KeyError, TypeError, ValueError):+ continuefor bf in rows:if bf.team_id not in cp_dates:continuediff --git a/posthog/dags/test_events_backfill_to_duckling.py b/posthog/dags/test_events_backfill_to_duckling.pyindex 581d5c2e9f2e..1e1205daa804 100644--- a/posthog/dags/test_events_backfill_to_duckling.py+++ b/posthog/dags/test_events_backfill_to_duckling.py@@ -931,6 +931,8 @@ class TestEarliestEventDateReconcile:[# CP has no date for the team -> re-push("cp_missing_date", {"team_id": 1, "earliest_event_date": None}, True),+ # A CP serializing team_id as a string must not make the team look unknown+ ("cp_string_team_id", {"team_id": "1", "earliest_event_date": None}, True),# CP has a stale date -> re-push("cp_stale_date", {"team_id": 1, "earliest_event_date": "2019-01-01"}, True),# CP already converged -> nothing to dodiff --git a/products/data_warehouse/backend/presentation/views/managed_warehouse.py b/products/data_warehouse/backend/presentation/views/managed_warehouse.pyindex 1867bd18f714..793bd4c35607 100644--- a/products/data_warehouse/backend/presentation/views/managed_warehouse.py+++ b/products/data_warehouse/backend/presentation/views/managed_warehouse.py@@ -198,11 +198,19 @@ def _get_project_team_row(*, organization_id: UUID | str, team_id: int) -> dictif not status.is_success(resp.status_code) or not isinstance(resp.data, dict):raise RuntimeError("Failed to read the organization's managed warehouse team rows")for row in resp.data.get("teams") or []:- if isinstance(row, dict) and row.get("team_id") == team_id:+ if isinstance(row, dict) and _row_team_id(row) == team_id:return rowreturn None+def _row_team_id(row: dict) -> int | None:+ """A control-plane row's team id as int, tolerating a string serialization."""+ try:+ return int(row["team_id"])+ except (KeyError, TypeError, ValueError):+ return None++def configure_project_reader(*, organization_id: UUID | str, team_id: int, table_suffix: str, password: str) -> dict[str, str]:@@ -639,7 +647,7 @@ def team_onboarding_state(organization_id: UUID | str, team_id: int) -> dict:try:teams = _teams_from_response(list_teams(organization_id, require_enabled=False))if teams is not None:- duckgres_team = next((row for row in teams if row.get("team_id") == team_id), None)+ duckgres_team = next((row for row in teams if _row_team_id(row) == team_id), None)if duckgres_team is None and has_django_row:duckgres_team = _push_grandfathered_team(organization_id, team_id, table_suffix)elif duckgres_team is not None and not has_django_row: