: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): resolve warehouse group types by project, not team (#88989)
Tom Owers70d1d9990bf4
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.pyindex d5319bdb3443..ce466abbdfd8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.py@@ -40,7 +40,7 @@ from posthog.kafka_client.routing import producer_scopefrom posthog.kafka_client.topics import KAFKA_WAREHOUSE_PERSON_PROPERTY_UPDATESfrom posthog.models import PropertyDefinition, Teamfrom posthog.models.group.util import get_groups_by_identifiers-from posthog.models.group_type_mapping import get_group_types_for_team+from posthog.models.group_type_mapping import get_group_types_for_projectfrom posthog.models.person.util import get_persons_mapped_by_distinct_idfrom posthog.sync import database_sync_to_async@@ -349,9 +349,11 @@ def _filter_existing_ids(team_id: int, source: PersonPropertySyncSource, ids: lireturn existing-def _group_type_name(team_id: int, group_type_index: int) -> str | None:+def _group_type_name(project_id: int, group_type_index: int) -> str | None:# $groupidentify carries the group-type *name*; the config stores the index, so resolve it.- for group_type in get_group_types_for_team(team_id):+ # Group types are project-scoped, and GroupTypeMapping stores the project's root team id, so a+ # team that is a child environment has no rows of its own — resolve by project, not by team.+ for group_type in get_group_types_for_project(project_id, caller_tag="warehouse_sources/person_property_sync"):if group_type.get("group_type_index") == group_type_index:return group_type.get("group_type")return None@@ -442,6 +444,7 @@ def _stamp_provenance(async def _process_source_bundles(*,team_id: int,+ project_id: int,binding: WarehouseBinding,team_api_token: str,team_uuid: str,@@ -481,7 +484,7 @@ async def _process_source_bundles(group_type_name = Noneif source.target == _GROUP_TARGET and source.group_type_index is not None:group_type_name = await database_sync_to_async(_group_type_name, thread_sensitive=False)(- team_id, source.group_type_index+ project_id, source.group_type_index)if group_type_name is None:# Without the group-type name the consumer can't build a valid $groupidentify and would@@ -561,6 +564,7 @@ async def run_person_property_sync(*, team_id: int, binding: WarehouseBinding, jbundles = build_bundles(rows, source.key_column, source.column_property_map or {})ps = await _process_source_bundles(team_id=team_id,+ project_id=team.project_id,binding=binding,team_api_token=team.api_token,team_uuid=str(team.uuid),@@ -702,6 +706,7 @@ async def run_person_property_backfill(*, team_id: int, binding: WarehouseBindinbundles = list(accumulated[str(source.source_id)].items())ps = await _process_source_bundles(team_id=team_id,+ project_id=team.project_id,binding=binding,team_api_token=team.api_token,team_uuid=str(team.uuid),diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.pyindex fedf90f8470a..a78e1c206bd7 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.py@@ -429,6 +429,36 @@ class TestGroupTarget:write_snapshot.assert_not_awaited()assert result.produced == 0+ @pytest.mark.asyncio+ async def test_group_type_is_resolved_by_project_not_team(self):+ # Group types are project-scoped, and GroupTypeMapping stores the project's root team id, so+ # a child environment has no rows under its own team id. Resolving by team id found nothing+ # there, which skipped every group source on that team and produced no intents at all.+ team = MagicMock(api_token="tok", uuid="team-uuid", project_id=77)+ rows = [{"group_key": "acme", "plan": "pro"}]+ by_project = {77: [{"group_type_index": 0, "group_type": "organization"}]}+ with (+ patch(f"{_MODULE}.person_property_sync_sources_for", return_value=[self._group_source()]),+ patch(f"{_MODULE}.Team") as team_cls,+ patch(f"{_MODULE}._read_staged_rows", new=AsyncMock(return_value=rows)),+ patch(f"{_MODULE}._read_snapshot_hashes", new=AsyncMock(return_value={})),+ patch(f"{_MODULE}._filter_existing_ids", return_value={"acme"}),+ patch(+ f"{_MODULE}.get_group_types_for_project",+ side_effect=lambda project_id, **_kwargs: by_project.get(project_id, []),+ ) as lookup,+ patch(f"{_MODULE}._produce_intents", return_value=1) as produce,+ patch(f"{_MODULE}._write_snapshot_hashes", new=AsyncMock()),+ patch(f"{_MODULE}._stamp_provenance"),+ patch(f"{_MODULE}._clear_staged", new=AsyncMock()),+ ):+ team_cls.objects.get.return_value = team+ result = await pps.run_person_property_sync(team_id=1, binding=_SCHEMA, job_id="job-1")++ assert lookup.call_args.args[0] == 77 # the project, not team_id=1+ assert produce.call_args.kwargs["group_type_name"] == "organization"+ assert result.produced == 1+class TestExistenceLookupChunking:"""Both personhog helpers return whole models (properties included) and hold every one before