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(hogql_query): Extend support for all projects under org (#126125)

Tomás Farías Santanabcad34b6b7c9
diff --git a/products/batch_exports/backend/presentation/views/batch_export/exports.py b/products/batch_exports/backend/presentation/views/batch_export/exports.py
index b4c276596855..e32164b3cb1b 100644
--- a/products/batch_exports/backend/presentation/views/batch_export/exports.py
+++ b/products/batch_exports/backend/presentation/views/batch_export/exports.py
@@ -368,10 +368,10 @@ class BatchExportSerializer(serializers.ModelSerializer):
# TODO: Once these teams are migrated, remove this.
if model == BatchExportModel.EVENTS and (hogql_query := attrs.get("hogql_query")) is not None:
- is_grandfathered_team_id = BatchExport.objects.filter(
- schema__isnull=False, team=self.context["get_team"]()
+ is_grandfathered_organization = BatchExport.objects.filter(
+ schema__isnull=False, team__organization_id=self.context["get_team"]().organization_id
).exists()
- if is_grandfathered_team_id:
+ if is_grandfathered_organization:
attrs["hogql_query"] = self._validate_events_hogql_query(hogql_query)
else:
self._validate_attribute_only_supported_for_model(
diff --git a/products/batch_exports/backend/tests/api/conftest.py b/products/batch_exports/backend/tests/api/conftest.py
index 3e50785ab38f..1881af5923f2 100644
--- a/products/batch_exports/backend/tests/api/conftest.py
+++ b/products/batch_exports/backend/tests/api/conftest.py
@@ -107,7 +107,15 @@ def user(organization):
@pytest.fixture
def grandfathered_batch_export(team: Team, request: pytest.FixtureRequest) -> Iterator[BatchExport]:
- export_team = team if getattr(request, "param", True) else create_team(team.organization)
+ match getattr(request, "param", "same-team"):
+ case "same-team":
+ export_team = team
+ case "same-organization":
+ export_team = create_team(team.organization)
+ case "other-organization":
+ export_team = create_team(create_organization("Other organization"))
+ case scope:
+ raise ValueError(f"Unknown grandfathering scope: {scope}")
destination = create_destination()
batch_export = BatchExport.objects.create(
team=export_team,
diff --git a/products/batch_exports/backend/tests/api/test_create.py b/products/batch_exports/backend/tests/api/test_create.py
index c84f38794021..6ecd23cd0991 100644
--- a/products/batch_exports/backend/tests/api/test_create.py
+++ b/products/batch_exports/backend/tests/api/test_create.py
@@ -467,7 +467,7 @@ FROM events
@pytest.mark.parametrize(
- "grandfathered_batch_export", [True, False], indirect=True, ids=["grandfathered", "other-team"]
+ "grandfathered_batch_export", ["same-team", "same-organization", "other-organization"], indirect=True
)
def test_create_batch_export_with_custom_schema(
client: HttpClient,
@@ -497,7 +497,7 @@ def test_create_batch_export_with_custom_schema(
batch_export_data,
)
- if grandfathered_batch_export.team_id != team.pk:
+ if grandfathered_batch_export.team.organization_id != team.organization_id:
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()
assert response.json()["attr"] == "hogql_query"
assert not BatchExport.objects.filter(team=team).exists()
diff --git a/products/batch_exports/backend/tests/api/test_update.py b/products/batch_exports/backend/tests/api/test_update.py
index cf58b95afd0e..b29b6e6bdd6b 100644
--- a/products/batch_exports/backend/tests/api/test_update.py
+++ b/products/batch_exports/backend/tests/api/test_update.py
@@ -657,7 +657,7 @@ def test_put_rejects_destination_type_change(
@pytest.mark.parametrize(
- "grandfathered_batch_export", [True, False], indirect=True, ids=["grandfathered", "other-team"]
+ "grandfathered_batch_export", ["same-team", "same-organization", "other-organization"], indirect=True
)
def test_can_patch_hogql_query(
client: HttpClient,
@@ -685,7 +685,7 @@ def test_can_patch_hogql_query(
}
response = patch_batch_export(client, team.pk, batch_export["id"], new_batch_export_data)
- if grandfathered_batch_export.team_id != team.pk:
+ if grandfathered_batch_export.team.organization_id != team.organization_id:
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()
assert response.json()["attr"] == "hogql_query"
assert get_batch_export_ok(client, team.pk, batch_export["id"])["schema"] is None