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(data-warehouse): pin legacy table names when provisioning/onboarding teams (#72797)

Eric Duong3567a58029d6
diff --git a/products/data_warehouse/backend/presentation/views/managed_warehouse.py b/products/data_warehouse/backend/presentation/views/managed_warehouse.py
index cd46ee39db89..a1b1229dc2e8 100644
--- a/products/data_warehouse/backend/presentation/views/managed_warehouse.py
+++ b/products/data_warehouse/backend/presentation/views/managed_warehouse.py
@@ -320,6 +320,9 @@ def provision(
)
if status.is_success(resp.status_code) and isinstance(resp.data, dict):
_persist_duckgres_server(organization_id, database_name, resp.data)
+ # Complete the row BEFORE registering the team: registration kicks off the
+ # SQL-editor reader handshake, whose namespace grants derive from this row.
+ _complete_provisioning_team_row(organization_id, team_id, schema_name, require_enabled=require_enabled)
_register_provisioning_team(organization_id, team_id, schema_name)
# The bucket is internal infra detail, persisted above and consumed by the
# backfill via cp_bucket_for — not part of the UI-facing ProvisionWarehouseResponse
@@ -328,6 +331,35 @@ def provision(
return resp
+def _complete_provisioning_team_row(
+ organization_id: UUID | str, team_id: int, schema_name: str, *, require_enabled: bool
+) -> None:
+ """Pin the first team's legacy table names onto the row duckgres just created.
+
+ The provision body cannot carry them, and without them the team's SQL-editor reader is
+ granted only the derived schemas no data lands in yet (see onboard_team). Best-effort:
+ the warehouse is already provisioned, so a transient failure must not fail the provision —
+ re-running onboarding (or the grandfather upsert) completes the row later.
+ """
+ legacy = _grandfathered_team_fields(team_id, schema_name)
+ resp = create_team(
+ organization_id,
+ team_id,
+ schema_name,
+ events_table_name=legacy["events_table_name"],
+ persons_table_name=legacy["persons_table_name"],
+ schema_data_imports_name=legacy["schema_data_imports_name"],
+ require_enabled=require_enabled,
+ )
+ if not status.is_success(resp.status_code):
+ logger.warning(
+ "Provisioned warehouse team row could not be completed with legacy table names",
+ organization_id=str(organization_id),
+ team_id=team_id,
+ status_code=resp.status_code,
+ )
+
+
def _strip_bucket_fields(body: dict) -> None:
"""Drop the internal bucket fields from a UI-facing response body, in place."""
body.pop("bucket", None)
@@ -573,7 +605,21 @@ def onboard_team(
except DucklingBackfillEnableError as exc:
return Response({"error": str(exc)}, status=status.HTTP_400_BAD_REQUEST)
- resp = create_team(organization_id, team_id, schema_name, require_enabled=require_enabled)
+ # Pin the legacy table names the duckling DAG writes today (posthog.events_<suffix>,
+ # posthog_data_imports_<suffix>): the suffix for a newly onboarded team IS its schema
+ # name. A row without them describes the derived layout no data lands in yet, which
+ # grants the project's SQL-editor reader nothing (the EU placeholder-row bug). Drop
+ # this once the duckling DAG writes the derived <schema_name>.events layout for real.
+ legacy = _grandfathered_team_fields(team_id, schema_name)
+ resp = create_team(
+ organization_id,
+ team_id,
+ schema_name,
+ events_table_name=legacy["events_table_name"],
+ persons_table_name=legacy["persons_table_name"],
+ schema_data_imports_name=legacy["schema_data_imports_name"],
+ require_enabled=require_enabled,
+ )
if resp.status_code == status.HTTP_409_CONFLICT:
return Response(
{"error": f"The schema name '{schema_name}' is already used by another project in this organization."},
diff --git a/products/data_warehouse/backend/tests/api/test_managed_warehouse.py b/products/data_warehouse/backend/tests/api/test_managed_warehouse.py
index a465906d8e22..43b1dcf3053d 100644
--- a/products/data_warehouse/backend/tests/api/test_managed_warehouse.py
+++ b/products/data_warehouse/backend/tests/api/test_managed_warehouse.py
@@ -274,6 +274,39 @@ def test_provision_sends_team_id_and_schema_name_to_control_plane(mock_request:
assert json_body["schema_name"] == "prod_events"
assert "default_team_id" not in json_body
+ # The provision body cannot carry legacy table names, so the first team's row is
+ # completed with a follow-up org-teams upsert — same fields onboard_team writes.
+ method, org_id, path = mock_request.call_args_list[1].args
+ assert (method, org_id, path) == ("POST", org.id, "/teams")
+ teams_body = mock_request.call_args_list[1].kwargs["json_body"]
+ assert teams_body == {
+ "team_id": team.id,
+ "schema_name": "prod_events",
+ "events_table_name": "events_prod_events",
+ "persons_table_name": "persons_prod_events",
+ "schema_data_imports_name": "posthog_data_imports_prod_events",
+ }
+
+
+@patch("products.data_warehouse.backend.presentation.views.managed_warehouse._request")
+def test_provision_succeeds_even_when_the_team_row_completion_fails(mock_request: MagicMock) -> None:
+ # The follow-up upsert is best-effort: the warehouse is already provisioned, so a
+ # transient teams-API failure must not fail the provision response.
+ org = Organization.objects.create(name="Org")
+ team = Team.objects.create(organization=org)
+ mock_request.side_effect = [
+ Response(
+ {"status": "provisioning started", "org": str(org.id), "username": "root", "password": "secret"},
+ status=202,
+ ),
+ Response({"error": "store unavailable"}, status=500),
+ ]
+
+ resp = managed_warehouse.provision(org.id, "my-warehouse", team.id, "prod_events")
+
+ assert resp.status_code == 202
+
@parameterized.expand(
[
@@ -601,12 +634,20 @@ def test_onboard_team_dual_writes_duckgres_and_django(mock_request: MagicMock, _
assert resp.status_code == 200
assert resp.data == {"onboarded": True, "schema_name": "my_events"}
- # duckgres team row created via the org-teams upsert, without legacy table names
- # (NULL legacy fields mean the derived <schema>.events layout).
+ # duckgres team row created via the org-teams upsert WITH the legacy table names the
+ # duckling DAG actually writes today (posthog.events_<suffix> + posthog_data_imports_<suffix>).
+ # A row without them grants the project reader only nonexistent derived schemas — the
+ # empty-SQL-editor-sidebar bug the EU placeholder rows hit.
method, org_id, path = mock_request.call_args_list[0].args
assert (method, org_id, path) == ("POST", org.id, "/teams")
json_body = mock_request.call_args_list[0].kwargs["json_body"]
- assert json_body == {"team_id": team.id, "schema_name": "my_events"}
+ assert json_body == {
+ "team_id": team.id,
+ "schema_name": "my_events",
+ "events_table_name": "events_my_events",
+ "persons_table_name": "persons_my_events",
+ "schema_data_imports_name": "posthog_data_imports_my_events",
+ }
link = DuckgresServerTeam.objects.get(server=server, team_id=team.id)
assert link.backfill_enabled is True