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.

perf(scoping): reuse the loaded team row on scoped api requests (#130133)

Thiago Salvatore8274fefb6cae
diff --git a/ee/api/test/test_action.py b/ee/api/test/test_action.py
index cd25b068d9f5..f9dabe2e6a9e 100644
--- a/ee/api/test/test_action.py
+++ b/ee/api/test/test_action.py
@@ -84,7 +84,7 @@ class TestActionApi(APIBaseTest):
# base request queries + the single bulk access-control preload (one query covering all
# actions, replacing the previous per-action access-control lookups)
- with self.assertNumQueries(13):
+ with self.assertNumQueries(12):
response = self.client.get(f"/api/projects/{self.team.id}/actions")
self.assertEqual(response.json()["results"][0]["tags"][0], "tag")
self.assertEqual(response.status_code, status.HTTP_200_OK)
diff --git a/posthog/api/routing.py b/posthog/api/routing.py
index 40f1adccae2b..685fdc4cad71 100644
--- a/posthog/api/routing.py
+++ b/posthog/api/routing.py
@@ -375,9 +375,11 @@ class TeamAndOrgViewSetMixin(_GenericViewSet):
# being a @cached_property this still degrades safely.
if "team" in self.__dict__:
canonical_team_id = self.team.parent_team_id or team_id
+ resolved_from_team_id: int | None = team_id
else:
canonical_team_id = team_id
- self._team_scope_token = set_current_team_id(canonical_team_id)
+ resolved_from_team_id = None
+ self._team_scope_token = set_current_team_id(canonical_team_id, resolved_from_team_id=resolved_from_team_id)
def dangerously_get_permissions(self):
"""
@@ -615,9 +617,13 @@ class TeamAndOrgViewSetMixin(_GenericViewSet):
def _is_team_view(self):
return self.param_derived_from_user_current_team == "team_id" or "team_id" in self.parent_query_kwargs
+ @property
+ def _is_nested_project_view(self) -> bool:
+ return "project_id" in self.parent_query_kwargs
+
@property
def _is_project_view(self):
- return self.param_derived_from_user_current_team == "project_id" or "project_id" in self.parent_query_kwargs
+ return self.param_derived_from_user_current_team == "project_id" or self._is_nested_project_view
@cached_property
def team_id(self) -> int:
@@ -704,6 +710,15 @@ class TeamAndOrgViewSetMixin(_GenericViewSet):
except (Project.DoesNotExist, ValueError):
raise NotFound(detail="Project not found.")
+ def _get_project_team_or_404(self) -> Team:
+ try:
+ team = self.team
+ except Team.DoesNotExist:
+ raise NotFound(detail="Project not found.")
+ if team.project_id != self.project_id:
+ raise NotFound(detail="Project not found.")
+ return team
+
@cached_property
def organization_id(self) -> str:
try:
@@ -714,6 +729,8 @@ class TeamAndOrgViewSetMixin(_GenericViewSet):
if self._is_team_view:
# TODO: self.team.project.organization_id when project environments are rolled out
current_organization_id = self.team.organization_id
+ elif self._is_nested_project_view:
+ current_organization_id = self._get_project_team_or_404().organization_id
elif self._is_project_view:
current_organization_id = self.project.organization_id
elif user:
@@ -727,6 +744,8 @@ class TeamAndOrgViewSetMixin(_GenericViewSet):
def organization(self) -> Organization:
if self._is_team_view:
return self.team.organization
+ if self._is_nested_project_view:
+ return self._get_project_team_or_404().organization
try:
return Organization.objects.get(
id=self.project.organization_id if self._is_project_view else self.organization_id
diff --git a/posthog/api/test/notebooks/__snapshots__/test_notebook.ambr b/posthog/api/test/notebooks/__snapshots__/test_notebook.ambr
index 5c4a55368afc..1ecdac3eb098 100644
--- a/posthog/api/test/notebooks/__snapshots__/test_notebook.ambr
+++ b/posthog/api/test/notebooks/__snapshots__/test_notebook.ambr
@@ -224,160 +224,6 @@
'''
# ---
# name: TestNotebooks.test_updates_notebook.10
- '''
- SELECT "posthog_team"."id",
- "posthog_team"."uuid",
- "posthog_team"."organization_id",
- "posthog_team"."parent_team_id",
- "posthog_team"."project_id",
- "posthog_team"."api_token",
- "posthog_team"."app_urls",
- "posthog_team"."name",
- "posthog_team"."slack_incoming_webhook",
- "posthog_team"."created_at",
- "posthog_team"."updated_at",
- "posthog_team"."anonymize_ips",
- "posthog_team"."completed_snippet_onboarding",
- "posthog_team"."has_completed_onboarding_for",
- "posthog_team"."onboarding_tasks",
- "posthog_team"."ingested_event",
- "posthog_team"."ingested_production_event",
- "posthog_team"."ingested_production_event_last_checked_at",
- "posthog_team"."person_processing_opt_out",
- "posthog_team"."secret_api_token",
- "posthog_team"."secret_api_token_backup",
- "posthog_team"."session_recording_opt_in",
- "posthog_team"."session_recording_sample_rate",
- "posthog_team"."session_recording_minimum_duration_milliseconds",
- "posthog_team"."session_recording_linked_flag",
- "posthog_team"."session_recording_network_payload_capture_config",
- "posthog_team"."session_recording_masking_config",
- "posthog_team"."session_recording_url_trigger_config",
- "posthog_team"."session_recording_url_blocklist_config",
- "posthog_team"."session_recording_event_trigger_config",
- "posthog_team"."session_recording_trigger_match_type_config",
- "posthog_team"."session_recording_trigger_groups",
- "posthog_team"."session_replay_config",
- "posthog_team"."session_recording_retention_period",
- "posthog_team"."event_retention_months",
- "posthog_team"."conversations_enabled",
- "posthog_team"."conversations_settings",
- "posthog_team"."proactive_tasks_enabled",
- "posthog_team"."survey_config",
- "posthog_team"."surveys_opt_in",
- "posthog_team"."product_tours_opt_in",
- "posthog_team"."capture_console_log_opt_in",
- "posthog_team"."capture_performance_opt_in",
- "posthog_team"."capture_dead_clicks",
- "posthog_team"."autocapture_opt_out",
- "posthog_team"."autocapture_web_vitals_opt_in",
- "posthog_team"."autocapture_web_vitals_allowed_metrics",
- "posthog_team"."autocapture_exceptions_opt_in",
- "posthog_team"."autocapture_exceptions_errors_to_ignore",
- "posthog_team"."logs_settings",
- "posthog_team"."llm_gateway_enabled_at",
- "posthog_team"."llm_gateway_revoked_at",
- "posthog_team"."llm_gateway_overspend_allowance_usd",
- "posthog_team"."heatmaps_opt_in",
- "posthog_team"."receive_org_level_activity_logs",
- "posthog_team"."web_analytics_pre_aggregated_tables_enabled",
- "posthog_team"."web_analytics_pre_aggregated_tables_version",
- "posthog_team"."flags_persistence_default",
- "posthog_team"."feature_flag_confirmation_enabled",
- "posthog_team"."feature_flag_confirmation_message",
- "posthog_team"."default_evaluation_environments_enabled",
- "posthog_team"."require_evaluation_environment_tags",
- "posthog_team"."default_evaluation_contexts_enabled",
- "posthog_team"."require_evaluation_contexts",
- "posthog_team"."session_recording_version",
- "posthog_team"."signup_token",
- "posthog_team"."is_demo",
- "posthog_team"."access_control",
- "posthog_team"."week_start_day",
- "posthog_team"."inject_web_apps",
- "posthog_team"."test_account_filters",
- "posthog_team"."test_account_filters_default_checked",
- "posthog_team"."path_cleaning_filters",
- "posthog_team"."timezone",
- "posthog_team"."data_attributes",
- "posthog_team"."person_display_name_properties",
- "posthog_team"."live_events_columns",
- "posthog_team"."recording_domains",
- "posthog_team"."human_friendly_comparison_periods",
- "posthog_team"."cookieless_server_hash_mode",
- "posthog_team"."primary_dashboard_id",
- "posthog_team"."default_data_theme",
- "posthog_team"."extra_settings",
- "posthog_team"."modifiers",
- "posthog_team"."session_recording_retention_period_days",
- "posthog_team"."external_data_workspace_id",
- "posthog_team"."external_data_workspace_last_synced_at",
- "posthog_team"."api_query_rate_limit",
- "posthog_team"."drop_events_older_than",
- "posthog_team"."base_currency",
- "posthog_team"."business_model",
- "posthog_organization"."id",
- "posthog_organization"."name",
- "posthog_organization"."slug",
- "posthog_organization"."logo_media_id",
- "posthog_organization"."created_at",
- "posthog_organization"."updated_at",
- "posthog_organization"."is_active",
- "posthog_organization"."is_not_active_reason",
- "posthog_organization"."session_cookie_age",
- "posthog_organization"."is_member_join_email_enabled",
- "posthog_organization"."is_ai_data_processing_approved",
- "posthog_organization"."is_ai_training_opted_in",
- "posthog_organization"."is_ai_training_locked",
- "posthog_organization"."is_ai_training_cta_shown",
- "posthog_organization"."enforce_2fa",
- "posthog_organization"."enforce_verified_domains",
- "posthog_organization"."members_can_invite",
- "posthog_organization"."members_can_create_projects",
- "posthog_organization"."members_can_use_personal_api_keys",
- "posthog_organization"."members_can_see_org_members",
- "posthog_organization"."uses_most_specific_access_resolution",
- "posthog_organization"."allow_publicly_shared_resources",
- "posthog_organization"."member_notice",
- "posthog_organization"."read_only_mcp_access",
- "posthog_organization"."default_role_id",
- "posthog_organization"."plugins_access_level",
- "posthog_organization"."for_internal_metrics",
- "posthog_organization"."default_anonymize_ips",
- "posthog_organization"."is_pending_deletion",
- "posthog_organization"."customer_id",
- "posthog_organization"."available_product_features",
- "posthog_organization"."usage",
- "posthog_organization"."never_drop_data",
- "posthog_organization"."customer_trust_scores",
- "posthog_organization"."has_active_subscription",
- "posthog_organization"."setup_section_2_completed",
- "posthog_organization"."personalization",
- "posthog_organization"."domain_whitelist",
- "posthog_organization"."is_platform"
- FROM "posthog_team"
- INNER JOIN "posthog_organization" ON ("posthog_team"."organization_id" = "posthog_organization"."id")
- WHERE "posthog_team"."id" = 99999
- LIMIT 21
- '''
-# ---
-# name: TestNotebooks.test_updates_notebook.11
- '''
- SELECT "posthog_project"."updated_at",
- "posthog_project"."id",
- "posthog_project"."organization_id",
- "posthog_project"."name",
- "posthog_project"."logo_media_id",
- "posthog_project"."created_at",
- "posthog_project"."product_description",
- "posthog_project"."is_pending_deletion",
- "posthog_project"."deletion_scheduled_at"
- FROM "posthog_project"
- WHERE "posthog_project"."id" = 99999
- LIMIT 21
- '''
-# ---
-# name: TestNotebooks.test_updates_notebook.12
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -432,7 +278,7 @@
LIMIT 21
'''
# ---
-# name: TestNotebooks.test_updates_notebook.13
+# name: TestNotebooks.test_updates_notebook.11
'''
SELECT "ee_accesscontrol"."id",
"ee_accesscontrol"."team_id",
@@ -455,7 +301,7 @@
AND "ee_accesscontrol"."team_id" = 99999))
'''
# ---
-# name: TestNotebooks.test_updates_notebook.14
+# name: TestNotebooks.test_updates_notebook.12
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -508,7 +354,7 @@
WHERE "posthog_organizationmembership"."user_id" = 99999
'''
# ---
-# name: TestNotebooks.test_updates_notebook.15
+# name: TestNotebooks.test_updates_notebook.13
'''
SELECT "posthog_notebook"."id",
"posthog_notebook"."short_id",
@@ -715,7 +561,7 @@
LIMIT 21
'''
# ---
-# name: TestNotebooks.test_updates_notebook.16
+# name: TestNotebooks.test_updates_notebook.14
'''
SELECT "posthog_notebook"."id",
"posthog_notebook"."short_id",
@@ -739,7 +585,7 @@
LIMIT 21
'''
# ---
-# name: TestNotebooks.test_updates_notebook.17
+# name: TestNotebooks.test_updates_notebook.15
'''
SELECT "posthog_notebook"."id",
"posthog_notebook"."short_id",
@@ -765,7 +611,7 @@
UPDATE
'''
# ---
-# name: TestNotebooks.test_updates_notebook.18
+# name: TestNotebooks.test_updates_notebook.16
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -863,7 +709,7 @@
LIMIT 21
'''
# ---
-# name: TestNotebooks.test_updates_notebook.19
+# name: TestNotebooks.test_updates_notebook.17
'''
SELECT "posthog_organization"."id",
"posthog_organization"."name",
@@ -909,23 +755,7 @@
LIMIT 21
'''
# ---
-# name: TestNotebooks.test_updates_notebook.2
- '''
- SELECT "posthog_project"."updated_at",
- "posthog_project"."id",
- "posthog_project"."organization_id",
- "posthog_project"."name",
- "posthog_project"."logo_media_id",
- "posthog_project"."created_at",
- "posthog_project"."product_description",
- "posthog_project"."is_pending_deletion",
- "posthog_project"."deletion_scheduled_at"
- FROM "posthog_project"
- WHERE "posthog_project"."id" = 99999
- LIMIT 21
- '''
-# ---
-# name: TestNotebooks.test_updates_notebook.20
+# name: TestNotebooks.test_updates_notebook.18
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
@@ -950,7 +780,7 @@
AND "posthog_filesystem"."shortcut" IS NOT NULL))
'''
# ---
-# name: TestNotebooks.test_updates_notebook.21
+# name: TestNotebooks.test_updates_notebook.19
'''
SELECT "posthog_filesystemshortcut"."id",
"posthog_filesystemshortcut"."team_id",
@@ -973,7 +803,62 @@
AND "posthog_filesystemshortcut"."href" IS NOT NULL))
'''
# ---
-# name: TestNotebooks.test_updates_notebook.22
+# name: TestNotebooks.test_updates_notebook.2
+ '''
+ SELECT "posthog_organizationmembership"."id",
+ "posthog_organizationmembership"."organization_id",
+ "posthog_organizationmembership"."user_id",
+ "posthog_organizationmembership"."level",
+ "posthog_organizationmembership"."joined_at",
+ "posthog_organizationmembership"."updated_at",
+ "posthog_organizationmembership"."invited_by_id",
+ "posthog_organization"."id",
+ "posthog_organization"."name",
+ "posthog_organization"."slug",
+ "posthog_organization"."logo_media_id",
+ "posthog_organization"."created_at",
+ "posthog_organization"."updated_at",
+ "posthog_organization"."is_active",
+ "posthog_organization"."is_not_active_reason",
+ "posthog_organization"."session_cookie_age",
+ "posthog_organization"."is_member_join_email_enabled",
+ "posthog_organization"."is_ai_data_processing_approved",
+ "posthog_organization"."is_ai_training_opted_in",
+ "posthog_organization"."is_ai_training_locked",
+ "posthog_organization"."is_ai_training_cta_shown",
+ "posthog_organization"."enforce_2fa",
+ "posthog_organization"."enforce_verified_domains",
+ "posthog_organization"."members_can_invite",
+ "posthog_organization"."members_can_create_projects",
+ "posthog_organization"."members_can_use_personal_api_keys",
+ "posthog_organization"."members_can_see_org_members",
+ "posthog_organization"."uses_most_specific_access_resolution",
+ "posthog_organization"."allow_publicly_shared_resources",
+ "posthog_organization"."member_notice",
+ "posthog_organization"."read_only_mcp_access",
+ "posthog_organization"."default_role_id",
+ "posthog_organization"."plugins_access_level",
+ "posthog_organization"."for_internal_metrics",
+ "posthog_organization"."default_anonymize_ips",
+ "posthog_organization"."is_pending_deletion",
+ "posthog_organization"."customer_id",
+ "posthog_organization"."available_product_features",
+ "posthog_organization"."usage",
+ "posthog_organization"."never_drop_data",
+ "posthog_organization"."customer_trust_scores",
+ "posthog_organization"."has_active_subscription",
+ "posthog_organization"."setup_section_2_completed",
+ "posthog_organization"."personalization",
+ "posthog_organization"."domain_whitelist",
+ "posthog_organization"."is_platform"
+ FROM "posthog_organizationmembership"
+ INNER JOIN "posthog_organization" ON ("posthog_organizationmembership"."organization_id" = "posthog_organization"."id")
+ WHERE ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
+ AND "posthog_organizationmembership"."user_id" = 99999)
+ LIMIT 21
+ '''
+# ---
+# name: TestNotebooks.test_updates_notebook.20
'''
SELECT "posthog_sharingconfiguration"."id",
"posthog_sharingconfiguration"."team_id",
@@ -993,7 +878,7 @@
WHERE "posthog_sharingconfiguration"."notebook_id" = '00000000000000000000000000000000'::uuid
'''
# ---
-# name: TestNotebooks.test_updates_notebook.23
+# name: TestNotebooks.test_updates_notebook.21
'''
SELECT "posthog_sharingconfiguration"."id",
"posthog_sharingconfiguration"."team_id",
@@ -1013,7 +898,7 @@
WHERE "posthog_sharingconfiguration"."notebook_id" = '00000000000000000000000000000000'::uuid
'''
# ---
-# name: TestNotebooks.test_updates_notebook.24
+# name: TestNotebooks.test_updates_notebook.22
'''
SELECT "posthog_resourcenotebook"."id",
"posthog_resourcenotebook"."notebook_id",
@@ -1023,7 +908,7 @@
WHERE "posthog_resourcenotebook"."notebook_id" = '00000000000000000000000000000000'::uuid
'''
# ---
-# name: TestNotebooks.test_updates_notebook.25
+# name: TestNotebooks.test_updates_notebook.23
'''
SELECT "posthog_resourcenotebook"."id",
"posthog_resourcenotebook"."notebook_id",
@@ -1033,7 +918,7 @@
WHERE "posthog_resourcenotebook"."notebook_id" = '00000000000000000000000000000000'::uuid
'''
# ---
-# name: TestNotebooks.test_updates_notebook.26
+# name: TestNotebooks.test_updates_notebook.24
'''
SELECT "posthog_user"."id",
"posthog_user"."password",
@@ -1080,7 +965,7 @@
LIMIT 21
'''
# ---
-# name: TestNotebooks.test_updates_notebook.27
+# name: TestNotebooks.test_updates_notebook.25
'''
SELECT "posthog_resourcenotebook"."id",
"posthog_resourcenotebook"."account_id"
@@ -1091,7 +976,7 @@
LIMIT 1
'''
# ---
-# name: TestNotebooks.test_updates_notebook.28
+# name: TestNotebooks.test_updates_notebook.26
'''
SELECT "posthog_user"."id",
"posthog_user"."password",
@@ -1177,7 +1062,7 @@
AND "posthog_user"."id" = 99999)
'''
# ---
-# name: TestNotebooks.test_updates_notebook.29
+# name: TestNotebooks.test_updates_notebook.27
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -1315,7 +1200,7 @@
LIMIT 21
'''
# ---
-# name: TestNotebooks.test_updates_notebook.3
+# name: TestNotebooks.test_updates_notebook.28
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -1370,78 +1255,30 @@
LIMIT 21
'''
# ---
-# name: TestNotebooks.test_updates_notebook.30
- '''
- SELECT "posthog_project"."updated_at",
- "posthog_project"."id",
- "posthog_project"."organization_id",
- "posthog_project"."name",
- "posthog_project"."logo_media_id",
- "posthog_project"."created_at",
- "posthog_project"."product_description",
- "posthog_project"."is_pending_deletion",
- "posthog_project"."deletion_scheduled_at"
- FROM "posthog_project"
- WHERE "posthog_project"."id" = 99999
- LIMIT 21
- '''
-# ---
-# name: TestNotebooks.test_updates_notebook.31
+# name: TestNotebooks.test_updates_notebook.29
'''
- SELECT "posthog_organizationmembership"."id",
- "posthog_organizationmembership"."organization_id",
- "posthog_organizationmembership"."user_id",
- "posthog_organizationmembership"."level",
- "posthog_organizationmembership"."joined_at",
- "posthog_organizationmembership"."updated_at",
- "posthog_organizationmembership"."invited_by_id",
- "posthog_organization"."id",
- "posthog_organization"."name",
- "posthog_organization"."slug",
- "posthog_organization"."logo_media_id",
- "posthog_organization"."created_at",
- "posthog_organization"."updated_at",
- "posthog_organization"."is_active",
- "posthog_organization"."is_not_active_reason",
- "posthog_organization"."session_cookie_age",
- "posthog_organization"."is_member_join_email_enabled",
- "posthog_organization"."is_ai_data_processing_approved",
- "posthog_organization"."is_ai_training_opted_in",
- "posthog_organization"."is_ai_training_locked",
- "posthog_organization"."is_ai_training_cta_shown",
- "posthog_organization"."enforce_2fa",
- "posthog_organization"."enforce_verified_domains",
- "posthog_organization"."members_can_invite",
- "posthog_organization"."members_can_create_projects",
- "posthog_organization"."members_can_use_personal_api_keys",
- "posthog_organization"."members_can_see_org_members",
- "posthog_organization"."uses_most_specific_access_resolution",
- "posthog_organization"."allow_publicly_shared_resources",
- "posthog_organization"."member_notice",
- "posthog_organization"."read_only_mcp_access",
- "posthog_organization"."default_role_id",
- "posthog_organization"."plugins_access_level",
- "posthog_organization"."for_internal_metrics",
- "posthog_organization"."default_anonymize_ips",
- "posthog_organization"."is_pending_deletion",
- "posthog_organization"."customer_id",
- "posthog_organization"."available_product_features",
- "posthog_organization"."usage",
- "posthog_organization"."never_drop_data",
- "posthog_organization"."customer_trust_scores",
- "posthog_organization"."has_active_subscription",
- "posthog_organization"."setup_section_2_completed",
- "posthog_organization"."personalization",
- "posthog_organization"."domain_whitelist",
- "posthog_organization"."is_platform"
- FROM "posthog_organizationmembership"
- INNER JOIN "posthog_organization" ON ("posthog_organizationmembership"."organization_id" = "posthog_organization"."id")
- WHERE ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
- AND "posthog_organizationmembership"."user_id" = 99999)
- LIMIT 21
+ SELECT "ee_accesscontrol"."id",
+ "ee_accesscontrol"."team_id",
+ "ee_accesscontrol"."access_level",
+ "ee_accesscontrol"."resource",
+ "ee_accesscontrol"."resource_id",
+ "ee_accesscontrol"."organization_member_id",
+ "ee_accesscontrol"."role_id",
+ "ee_accesscontrol"."created_by_id",
+ "ee_accesscontrol"."created_at",
+ "ee_accesscontrol"."updated_at"
+ FROM "ee_accesscontrol"
+ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id")
+ WHERE (("ee_accesscontrol"."organization_member_id" IS NULL
+ AND "ee_accesscontrol"."role_id" IS NULL
+ AND "ee_accesscontrol"."team_id" = 99999)
+ OR ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
+ AND "posthog_organizationmembership"."user_id" = 99999
+ AND "ee_accesscontrol"."role_id" IS NULL
+ AND "ee_accesscontrol"."team_id" = 99999))
'''
# ---
-# name: TestNotebooks.test_updates_notebook.32
+# name: TestNotebooks.test_updates_notebook.3
'''
SELECT "ee_accesscontrol"."id",
"ee_accesscontrol"."team_id",
@@ -1464,7 +1301,7 @@
AND "ee_accesscontrol"."team_id" = 99999))
'''
# ---
-# name: TestNotebooks.test_updates_notebook.33
+# name: TestNotebooks.test_updates_notebook.30
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -1517,7 +1354,7 @@
WHERE "posthog_organizationmembership"."user_id" = 99999
'''
# ---
-# name: TestNotebooks.test_updates_notebook.34
+# name: TestNotebooks.test_updates_notebook.31
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_activitylog"
@@ -1525,7 +1362,7 @@
AND "posthog_activitylog"."team_id" = 99999)
'''
# ---
-# name: TestNotebooks.test_updates_notebook.35
+# name: TestNotebooks.test_updates_notebook.32
'''
SELECT "posthog_activitylog"."id",
"posthog_activitylog"."team_id",
@@ -1592,29 +1429,6 @@
'''
# ---
# name: TestNotebooks.test_updates_notebook.4
- '''
- SELECT "ee_accesscontrol"."id",
- "ee_accesscontrol"."team_id",
- "ee_accesscontrol"."access_level",
- "ee_accesscontrol"."resource",
- "ee_accesscontrol"."resource_id",
- "ee_accesscontrol"."organization_member_id",
- "ee_accesscontrol"."role_id",
- "ee_accesscontrol"."created_by_id",
- "ee_accesscontrol"."created_at",
- "ee_accesscontrol"."updated_at"
- FROM "ee_accesscontrol"
- LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id")
- WHERE (("ee_accesscontrol"."organization_member_id" IS NULL
- AND "ee_accesscontrol"."role_id" IS NULL
- AND "ee_accesscontrol"."team_id" = 99999)
- OR ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
- AND "posthog_organizationmembership"."user_id" = 99999
- AND "ee_accesscontrol"."role_id" IS NULL
- AND "ee_accesscontrol"."team_id" = 99999))
- '''
-# ---
-# name: TestNotebooks.test_updates_notebook.5
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -1667,7 +1481,7 @@
WHERE "posthog_organizationmembership"."user_id" = 99999
'''
# ---
-# name: TestNotebooks.test_updates_notebook.6
+# name: TestNotebooks.test_updates_notebook.5
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
@@ -1692,7 +1506,7 @@
AND "posthog_filesystem"."shortcut" IS NOT NULL))
'''
# ---
-# name: TestNotebooks.test_updates_notebook.7
+# name: TestNotebooks.test_updates_notebook.6
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -1790,7 +1604,7 @@
LIMIT 21
'''
# ---
-# name: TestNotebooks.test_updates_notebook.8
+# name: TestNotebooks.test_updates_notebook.7
'''
SELECT "posthog_resourcenotebook"."id",
"posthog_resourcenotebook"."account_id"
@@ -1801,7 +1615,7 @@
LIMIT 1
'''
# ---
-# name: TestNotebooks.test_updates_notebook.9
+# name: TestNotebooks.test_updates_notebook.8
'''
SELECT "posthog_user"."id",
"posthog_user"."password",
@@ -1887,3 +1701,141 @@
AND "posthog_user"."id" = 99999)
'''
# ---
+# name: TestNotebooks.test_updates_notebook.9
+ '''
+ SELECT "posthog_team"."id",
+ "posthog_team"."uuid",
+ "posthog_team"."organization_id",
+ "posthog_team"."parent_team_id",
+ "posthog_team"."project_id",
+ "posthog_team"."api_token",
+ "posthog_team"."app_urls",
+ "posthog_team"."name",
+ "posthog_team"."slack_incoming_webhook",
+ "posthog_team"."created_at",
+ "posthog_team"."updated_at",
+ "posthog_team"."anonymize_ips",
+ "posthog_team"."completed_snippet_onboarding",
+ "posthog_team"."has_completed_onboarding_for",
+ "posthog_team"."onboarding_tasks",
+ "posthog_team"."ingested_event",
+ "posthog_team"."ingested_production_event",
+ "posthog_team"."ingested_production_event_last_checked_at",
+ "posthog_team"."person_processing_opt_out",
+ "posthog_team"."secret_api_token",
+ "posthog_team"."secret_api_token_backup",
+ "posthog_team"."session_recording_opt_in",
+ "posthog_team"."session_recording_sample_rate",
+ "posthog_team"."session_recording_minimum_duration_milliseconds",
+ "posthog_team"."session_recording_linked_flag",
+ "posthog_team"."session_recording_network_payload_capture_config",
+ "posthog_team"."session_recording_masking_config",
+ "posthog_team"."session_recording_url_trigger_config",
+ "posthog_team"."session_recording_url_blocklist_config",
+ "posthog_team"."session_recording_event_trigger_config",
+ "posthog_team"."session_recording_trigger_match_type_config",
+ "posthog_team"."session_recording_trigger_groups",
+ "posthog_team"."session_replay_config",
+ "posthog_team"."session_recording_retention_period",
+ "posthog_team"."event_retention_months",
+ "posthog_team"."conversations_enabled",
+ "posthog_team"."conversations_settings",
+ "posthog_team"."proactive_tasks_enabled",
+ "posthog_team"."survey_config",
+ "posthog_team"."surveys_opt_in",
+ "posthog_team"."product_tours_opt_in",
+ "posthog_team"."capture_console_log_opt_in",
+ "posthog_team"."capture_performance_opt_in",
+ "posthog_team"."capture_dead_clicks",
+ "posthog_team"."autocapture_opt_out",
+ "posthog_team"."autocapture_web_vitals_opt_in",
+ "posthog_team"."autocapture_web_vitals_allowed_metrics",
+ "posthog_team"."autocapture_exceptions_opt_in",
+ "posthog_team"."autocapture_exceptions_errors_to_ignore",
+ "posthog_team"."logs_settings",
+ "posthog_team"."llm_gateway_enabled_at",
+ "posthog_team"."llm_gateway_revoked_at",
+ "posthog_team"."llm_gateway_overspend_allowance_usd",
+ "posthog_team"."heatmaps_opt_in",
+ "posthog_team"."receive_org_level_activity_logs",
+ "posthog_team"."web_analytics_pre_aggregated_tables_enabled",
+ "posthog_team"."web_analytics_pre_aggregated_tables_version",
+ "posthog_team"."flags_persistence_default",
+ "posthog_team"."feature_flag_confirmation_enabled",
+ "posthog_team"."feature_flag_confirmation_message",
+ "posthog_team"."default_evaluation_environments_enabled",
+ "posthog_team"."require_evaluation_environment_tags",
+ "posthog_team"."default_evaluation_contexts_enabled",
+ "posthog_team"."require_evaluation_contexts",
+ "posthog_team"."session_recording_version",
+ "posthog_team"."signup_token",
+ "posthog_team"."is_demo",
+ "posthog_team"."access_control",
+ "posthog_team"."week_start_day",
+ "posthog_team"."inject_web_apps",
+ "posthog_team"."test_account_filters",
+ "posthog_team"."test_account_filters_default_checked",
+ "posthog_team"."path_cleaning_filters",
+ "posthog_team"."timezone",
+ "posthog_team"."data_attributes",
+ "posthog_team"."person_display_name_properties",
+ "posthog_team"."live_events_columns",
+ "posthog_team"."recording_domains",
+ "posthog_team"."human_friendly_comparison_periods",
+ "posthog_team"."cookieless_server_hash_mode",
+ "posthog_team"."primary_dashboard_id",
+ "posthog_team"."default_data_theme",
+ "posthog_team"."extra_settings",
+ "posthog_team"."modifiers",
+ "posthog_team"."session_recording_retention_period_days",
+ "posthog_team"."external_data_workspace_id",
+ "posthog_team"."external_data_workspace_last_synced_at",
+ "posthog_team"."api_query_rate_limit",
+ "posthog_team"."drop_events_older_than",
+ "posthog_team"."base_currency",
+ "posthog_team"."business_model",
+ "posthog_organization"."id",
+ "posthog_organization"."name",
+ "posthog_organization"."slug",
+ "posthog_organization"."logo_media_id",
+ "posthog_organization"."created_at",
+ "posthog_organization"."updated_at",
+ "posthog_organization"."is_active",
+ "posthog_organization"."is_not_active_reason",
+ "posthog_organization"."session_cookie_age",
+ "posthog_organization"."is_member_join_email_enabled",
+ "posthog_organization"."is_ai_data_processing_approved",
+ "posthog_organization"."is_ai_training_opted_in",
+ "posthog_organization"."is_ai_training_locked",
+ "posthog_organization"."is_ai_training_cta_shown",
+ "posthog_organization"."enforce_2fa",
+ "posthog_organization"."enforce_verified_domains",
+ "posthog_organization"."members_can_invite",
+ "posthog_organization"."members_can_create_projects",
+ "posthog_organization"."members_can_use_personal_api_keys",
+ "posthog_organization"."members_can_see_org_members",
+ "posthog_organization"."uses_most_specific_access_resolution",
+ "posthog_organization"."allow_publicly_shared_resources",
+ "posthog_organization"."member_notice",
+ "posthog_organization"."read_only_mcp_access",
+ "posthog_organization"."default_role_id",
+ "posthog_organization"."plugins_access_level",
+ "posthog_organization"."for_internal_metrics",
+ "posthog_organization"."default_anonymize_ips",
+ "posthog_organization"."is_pending_deletion",
+ "posthog_organization"."customer_id",
+ "posthog_organization"."available_product_features",
+ "posthog_organization"."usage",
+ "posthog_organization"."never_drop_data",
+ "posthog_organization"."customer_trust_scores",
+ "posthog_organization"."has_active_subscription",
+ "posthog_organization"."setup_section_2_completed",
+ "posthog_organization"."personalization",
+ "posthog_organization"."domain_whitelist",
+ "posthog_organization"."is_platform"
+ FROM "posthog_team"
+ INNER JOIN "posthog_organization" ON ("posthog_team"."organization_id" = "posthog_organization"."id")
+ WHERE "posthog_team"."id" = 99999
+ LIMIT 21
+ '''
+# ---
diff --git a/posthog/api/test/test_cohort.py b/posthog/api/test/test_cohort.py
index 19b49a66f903..1234f05041c5 100644
--- a/posthog/api/test/test_cohort.py
+++ b/posthog/api/test/test_cohort.py
@@ -485,7 +485,7 @@ class TestCohort(TestExportMixin, ClickhouseTestMixin, APIBaseTest, QueryMatchin
)
self.assertEqual(response.status_code, 201, response.content)
- with self.assertNumQueries(11):
+ with self.assertNumQueries(10):
response = self.client.get(f"/api/projects/{self.team.id}/cohorts")
assert len(response.json()["results"]) == 1
@@ -500,7 +500,7 @@ class TestCohort(TestExportMixin, ClickhouseTestMixin, APIBaseTest, QueryMatchin
)
self.assertEqual(response.status_code, 201, response.content)
- with self.assertNumQueries(11):
+ with self.assertNumQueries(10):
response = self.client.get(f"/api/projects/{self.team.id}/cohorts")
assert len(response.json()["results"]) == 3
diff --git a/posthog/api/test/test_my_notifications.py b/posthog/api/test/test_my_notifications.py
index c55a17017142..34320b3e62a7 100644
--- a/posthog/api/test/test_my_notifications.py
+++ b/posthog/api/test/test_my_notifications.py
@@ -349,7 +349,7 @@ class TestMyNotifications(APIBaseTest, QueryMatchingTest):
user=user, defaults={"last_viewed_activity_date": f"2023-0{i}-17T04:36:50Z"}
)
- with self.assertNumQueries(FuzzyInt(17, 17)):
+ with self.assertNumQueries(FuzzyInt(16, 16)):
self.client.get(f"/api/projects/{self.team.id}/my_notifications")
def test_microsecond_precision_mismatch(self):
diff --git a/posthog/api/test/test_routing.py b/posthog/api/test/test_routing.py
index 0d7d35fbf204..4e6a7150fde6 100644
--- a/posthog/api/test/test_routing.py
+++ b/posthog/api/test/test_routing.py
@@ -1,3 +1,4 @@
+import re
import random
import pathlib
import importlib
@@ -10,9 +11,11 @@ import pytest
from posthog.test.base import APIBaseTest
from django.core.exceptions import ImproperlyConfigured
+from django.db import connection
from django.db.models import Count
from django.db.models.functions import Lower
from django.test import override_settings
+from django.test.utils import CaptureQueriesContext
from django.urls import include, path
from django.utils import timezone
@@ -34,6 +37,7 @@ from posthog.models.personal_api_key import PersonalAPIKey
from posthog.models.project import Project
from posthog.models.scoping import get_current_team_id
from posthog.models.team.team import Team
+from posthog.models.user_facet_settings import UserFacetSettings
from posthog.models.utils import generate_random_token_personal, hash_key_value
from posthog.permissions import APIScopePermission
from posthog.products import product_app_names
@@ -52,6 +56,11 @@ class FooViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
def current_scope(self, request, **kwargs):
return Response({"team_id": get_current_team_id()})
+ @action(detail=False, methods=["get"])
+ def facet_products(self, request: Request, **kwargs: Any) -> Response:
+ facet_settings = UserFacetSettings.objects.for_team(self.team_id)
+ return Response({"products": list(facet_settings.values_list("product", flat=True))})
+
class ScopedFooViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
scope_object = "annotation"
@@ -236,6 +245,47 @@ class TestTeamAndOrgViewSetMixin(APIBaseTest):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["count"], 2) # Both current_team_annotation and other_team_annotation
+ @parameterized.expand(
+ [
+ ("project_view", "projects", False),
+ ("team_view", "team_nested", False),
+ ("child_environment", "team_nested", True),
+ ]
+ )
+ def test_scoped_request_reads_the_team_row_once(self, _name: str, prefix: str, child_environment: bool) -> None:
+ UserFacetSettings.objects.unscoped().create(
+ team=self.team, user=self.user, product=UserFacetSettings.Product.LOGS
+ )
+ url_team = (
+ Team.objects.create(organization=self.organization, project=self.project, parent_team=self.team)
+ if child_environment
+ else self.team
+ )
+
+ with CaptureQueriesContext(connection) as queries:
+ response = self.client.get(f"/api/{prefix}/{url_team.id}/foos/facet_products/")
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.json(), {"products": [UserFacetSettings.Product.LOGS]})
+ tables_read = [match.group(1) for q in queries if (match := re.search(r'FROM "(\w+)"', q["sql"]))]
+ self.assertEqual(tables_read.count("posthog_team"), 1, tables_read)
+ self.assertNotIn("posthog_project", tables_read)
+
+ @parameterized.expand([("missing_project", False), ("child_environment_id", True)])
+ def test_project_view_for_an_id_that_names_no_project_is_not_found(
+ self, _name: str, child_environment: bool
+ ) -> None:
+ url_id = (
+ Team.objects.create(organization=self.organization, project=self.project, parent_team=self.team).id
+ if child_environment
+ else self.team.id + 10_000
+ )
+
+ response = self.client.get(f"/api/projects/{url_id}/foos/")
+
+ self.assertEqual(response.status_code, 404)
+ self.assertEqual(response.json()["detail"], "Project not found.")
+
def test_organization_nested_filtering(self):
response = self.client.get(f"/api/organizations/{self.organization.id}/foos/")
self.assertEqual(response.status_code, 200)
diff --git a/posthog/models/scoping/README.md b/posthog/models/scoping/README.md
index be5e25c39d8e..33485f90ee64 100644
--- a/posthog/models/scoping/README.md
+++ b/posthog/models/scoping/README.md
@@ -15,6 +15,7 @@ A ContextVar holds the **canonical** team_id — the parent team's id when the t
## Where context gets set
- **DRF nested views** — `TeamAndOrgViewSetMixin.initial()` sets it from the URL `self.team_id` (the team being acted on, not the user's "current" team). Reuses `self.team.parent_team_id` if cached by permission checks for free.
+ The scope then remembers that it was resolved from the URL team id, so `resolve_effective_team_id()` returns the canonical id for that id without a query. `for_team()`, `team_scope()` and `ProductTeamModel.save()` all go through it.
- **Celery tasks** — `@with_team_scope()` decorator extracts a `team_id` parameter from the task signature.
- **Management commands / ad-hoc scripts** — `with team_scope(team_id):` block.
diff --git a/posthog/models/scoping/__init__.py b/posthog/models/scoping/__init__.py
index 08b1034e4d64..27e675f8e8d1 100644
--- a/posthog/models/scoping/__init__.py
+++ b/posthog/models/scoping/__init__.py
@@ -29,30 +29,47 @@ from contextlib import contextmanager
from contextvars import ContextVar, Token
from typing import ParamSpec, TypeVar
+from posthog.dataclasses import frozen
+
P = ParamSpec("P")
R = TypeVar("R")
+@frozen
+class _TeamScope:
+ team_id: int
+ resolved_from_team_id: int | None
+
+
# The current canonical team_id, or None if no scope is set.
-_current_team_id: ContextVar[int | None] = ContextVar("current_team_id", default=None)
+_current_team_scope: ContextVar[_TeamScope | None] = ContextVar("current_team_scope", default=None)
def get_current_team_id() -> int | None:
"""Get the current canonical team_id, or None if no scope is set."""
- return _current_team_id.get()
+ scope = _current_team_scope.get()
+ return scope.team_id if scope else None
-def set_current_team_id(team_id: int | None) -> Token[int | None]:
+def get_resolved_team_id(team_id: int) -> int | None:
+ """The current canonical team_id if the scope was resolved from `team_id`, otherwise None."""
+ scope = _current_team_scope.get()
+ return scope.team_id if scope and scope.resolved_from_team_id == team_id else None
+
+
+def set_current_team_id(team_id: int | None, *, resolved_from_team_id: int | None = None) -> Token[_TeamScope | None]:
"""Set the current canonical team_id. Returns a token to reset it later.
Pass `None` to clear context (used by `unscoped()`).
+ Pass `resolved_from_team_id` only when `team_id` was computed from that team's `parent_team_id`.
"""
- return _current_team_id.set(team_id)
+ scope = None if team_id is None else _TeamScope(team_id=team_id, resolved_from_team_id=resolved_from_team_id)
+ return _current_team_scope.set(scope)
-def reset_current_team_id(token: Token[int | None]) -> None:
+def reset_current_team_id(token: Token[_TeamScope | None]) -> None:
"""Reset the current team_id to its previous value."""
- _current_team_id.reset(token)
+ _current_team_scope.reset(token)
@contextmanager
@@ -70,6 +87,8 @@ def team_scope(team_id: int, *, canonical: bool = False) -> Generator[None]:
to whatever id was passed; if it isn't actually canonical, reads will
return zero rows from the wrong scope.
+ The lookup is skipped for the team that the current scope was resolved from.
+
Example:
# Production: caller may have a child id, framework resolves
with team_scope(team_id):
@@ -181,6 +200,7 @@ def with_team_scope(
__all__ = [
"get_current_team_id",
+ "get_resolved_team_id",
"set_current_team_id",
"reset_current_team_id",
"team_scope",
diff --git a/posthog/models/scoping/manager.py b/posthog/models/scoping/manager.py
index cf40bf0713e1..dc05cc65b250 100644
--- a/posthog/models/scoping/manager.py
+++ b/posthog/models/scoping/manager.py
@@ -27,7 +27,7 @@ from django.db import models
from django.db.models.fetch_modes import FetchMode, FetchPeers
from django.db.models.query_utils import DeferredAttribute
-from posthog.models.scoping import get_current_team_id, peer_lazy_loads
+from posthog.models.scoping import get_current_team_id, get_resolved_team_id, peer_lazy_loads
T = TypeVar("T", bound=models.Model)
@@ -132,6 +132,9 @@ def resolve_effective_team_id(team_id: int) -> int:
matches the rest of the fail-closed posture (silent fallback would
let `team_scope(typo)` quietly scope queries to a non-existent team).
"""
+ if (resolved_team_id := get_resolved_team_id(team_id)) is not None:
+ return resolved_team_id
+
from posthog.models.team import Team
try:
@@ -230,6 +233,8 @@ class TeamScopedManager(models.Manager[T]):
Pass `canonical=True` only when the caller has independently
verified the id is canonical (or is using a synthetic id in tests).
+
+ The lookup is skipped for the team that the current scope was resolved from.
"""
if not canonical:
team_id = resolve_effective_team_id(team_id)
diff --git a/posthog/models/scoping/test_manager.py b/posthog/models/scoping/test_manager.py
index 63c1960f3097..ebf4c66d2478 100644
--- a/posthog/models/scoping/test_manager.py
+++ b/posthog/models/scoping/test_manager.py
@@ -9,7 +9,7 @@ from django.core.exceptions import FieldFetchBlocked
from parameterized import parameterized
-from posthog.models.scoping import peer_lazy_loads, team_scope, unscoped
+from posthog.models.scoping import peer_lazy_loads, reset_current_team_id, set_current_team_id, team_scope, unscoped
from posthog.models.scoping.manager import (
FETCH_RAISE_ON_PEERS,
FETCH_RELATION_PEERS,
@@ -76,6 +76,19 @@ class TestTeamScopedManager(BaseTest):
assert flag is not None
assert flag.key == "flag-2"
+ def test_for_team_resolves_another_team_inside_a_scope_resolved_from_this_one(self) -> None:
+ other_team = Team.objects.create(organization=self.organization, name="Other Team")
+ FeatureFlag.objects.create(team=self.team, key="flag-1", created_by=self.user)
+ FeatureFlag.objects.create(team=other_team, key="flag-2", created_by=self.user)
+
+ token = set_current_team_id(self.team.id, resolved_from_team_id=self.team.id)
+ try:
+ keys = list(self._make_manager().for_team(other_team.id).values_list("key", flat=True))
+ finally:
+ reset_current_team_id(token)
+
+ assert keys == ["flag-2"]
+
def test_queryset_chaining_preserves_team_filter(self):
other_team = Team.objects.create(organization=self.organization, name="Other Team")
diff --git a/posthog/models/scoping/test_scoping.py b/posthog/models/scoping/test_scoping.py
index 0d4c6d916765..04763d1efdb8 100644
--- a/posthog/models/scoping/test_scoping.py
+++ b/posthog/models/scoping/test_scoping.py
@@ -1,3 +1,6 @@
+from collections.abc import Callable
+from contextlib import AbstractContextManager, nullcontext
+
import pytest
from django.test import SimpleTestCase
@@ -6,6 +9,7 @@ from parameterized import parameterized
from posthog.models.scoping import (
get_current_team_id,
+ get_resolved_team_id,
reset_current_team_id,
set_current_team_id,
team_scope,
@@ -40,6 +44,24 @@ class TestContextVar(SimpleTestCase):
reset_current_team_id(token)
assert get_current_team_id() is None
+ @parameterized.expand(
+ [
+ ("resolved_scope", nullcontext, 1),
+ ("nested_unscoped", unscoped, None),
+ ("nested_team_scope", lambda: team_scope(5, canonical=True), None),
+ ]
+ )
+ def test_resolved_team_id_answers_only_for_the_id_the_scope_was_resolved_from(
+ self, _name: str, inner_scope: Callable[[], AbstractContextManager[None]], expected: int | None
+ ) -> None:
+ token = set_current_team_id(1, resolved_from_team_id=2)
+ try:
+ with inner_scope():
+ assert get_resolved_team_id(2) == expected
+ assert get_resolved_team_id(3) is None
+ finally:
+ reset_current_team_id(token)
+
class TestTeamScopeContextManager(SimpleTestCase):
@parameterized.expand(
diff --git a/products/actions/backend/tests/api/__snapshots__/test_action.ambr b/products/actions/backend/tests/api/__snapshots__/test_action.ambr
index 9b97bec86e05..679cc2bb0dca 100644
--- a/products/actions/backend/tests/api/__snapshots__/test_action.ambr
+++ b/products/actions/backend/tests/api/__snapshots__/test_action.ambr
@@ -224,84 +224,6 @@
'''
# ---
# name: TestActionApi.test_listing_actions_is_not_nplus1.10
- '''
- SELECT "posthog_organizationmembership"."id",
- "posthog_organizationmembership"."organization_id",
- "posthog_organizationmembership"."user_id",
- "posthog_organizationmembership"."level",
- "posthog_organizationmembership"."joined_at",
- "posthog_organizationmembership"."updated_at",
- "posthog_organizationmembership"."invited_by_id",
- "posthog_organization"."id",
- "posthog_organization"."name",
- "posthog_organization"."slug",
- "posthog_organization"."logo_media_id",
- "posthog_organization"."created_at",
- "posthog_organization"."updated_at",
- "posthog_organization"."is_active",
- "posthog_organization"."is_not_active_reason",
- "posthog_organization"."session_cookie_age",
- "posthog_organization"."is_member_join_email_enabled",
- "posthog_organization"."is_ai_data_processing_approved",
- "posthog_organization"."is_ai_training_opted_in",
- "posthog_organization"."is_ai_training_locked",
- "posthog_organization"."is_ai_training_cta_shown",
- "posthog_organization"."enforce_2fa",
- "posthog_organization"."enforce_verified_domains",
- "posthog_organization"."members_can_invite",
- "posthog_organization"."members_can_create_projects",
- "posthog_organization"."members_can_use_personal_api_keys",
- "posthog_organization"."members_can_see_org_members",
- "posthog_organization"."uses_most_specific_access_resolution",
- "posthog_organization"."allow_publicly_shared_resources",
- "posthog_organization"."member_notice",
- "posthog_organization"."read_only_mcp_access",
- "posthog_organization"."default_role_id",
- "posthog_organization"."plugins_access_level",
- "posthog_organization"."for_internal_metrics",
- "posthog_organization"."default_anonymize_ips",
- "posthog_organization"."is_pending_deletion",
- "posthog_organization"."customer_id",
- "posthog_organization"."available_product_features",
- "posthog_organization"."usage",
- "posthog_organization"."never_drop_data",
- "posthog_organization"."customer_trust_scores",
- "posthog_organization"."has_active_subscription",
- "posthog_organization"."setup_section_2_completed",
- "posthog_organization"."personalization",
- "posthog_organization"."domain_whitelist",
- "posthog_organization"."is_platform"
- FROM "posthog_organizationmembership"
- INNER JOIN "posthog_organization" ON ("posthog_organizationmembership"."organization_id" = "posthog_organization"."id")
- WHERE ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
- AND "posthog_organizationmembership"."user_id" = 99999)
- LIMIT 21
- '''
-# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.11
- '''
- SELECT "ee_accesscontrol"."id",
- "ee_accesscontrol"."team_id",
- "ee_accesscontrol"."access_level",
- "ee_accesscontrol"."resource",
- "ee_accesscontrol"."resource_id",
- "ee_accesscontrol"."organization_member_id",
- "ee_accesscontrol"."role_id",
- "ee_accesscontrol"."created_by_id",
- "ee_accesscontrol"."created_at",
- "ee_accesscontrol"."updated_at"
- FROM "ee_accesscontrol"
- LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id")
- WHERE (("ee_accesscontrol"."organization_member_id" IS NULL
- AND "ee_accesscontrol"."role_id" IS NULL
- AND "ee_accesscontrol"."team_id" = 99999)
- OR ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
- AND "posthog_organizationmembership"."user_id" = 99999
- AND "ee_accesscontrol"."role_id" IS NULL
- AND "ee_accesscontrol"."team_id" = 99999))
- '''
-# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.12
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -354,7 +276,7 @@
WHERE "posthog_organizationmembership"."user_id" = 99999
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.13
+# name: TestActionApi.test_listing_actions_is_not_nplus1.11
'''
SELECT "posthog_action"."id",
"posthog_action"."name",
@@ -437,7 +359,7 @@
"posthog_action"."name" ASC
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.14
+# name: TestActionApi.test_listing_actions_is_not_nplus1.12
'''
SELECT "posthog_taggeditem"."id",
"posthog_taggeditem"."tag_id",
@@ -458,7 +380,7 @@
5 /* ... */))
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.15
+# name: TestActionApi.test_listing_actions_is_not_nplus1.13
'''
SELECT "posthog_user"."id",
"posthog_user"."password",
@@ -544,7 +466,7 @@
AND "posthog_user"."id" = 99999)
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.16
+# name: TestActionApi.test_listing_actions_is_not_nplus1.14
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -682,23 +604,7 @@
LIMIT 21
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.17
- '''
- SELECT "posthog_project"."updated_at",
- "posthog_project"."id",
- "posthog_project"."organization_id",
- "posthog_project"."name",
- "posthog_project"."logo_media_id",
- "posthog_project"."created_at",
- "posthog_project"."product_description",
- "posthog_project"."is_pending_deletion",
- "posthog_project"."deletion_scheduled_at"
- FROM "posthog_project"
- WHERE "posthog_project"."id" = 99999
- LIMIT 21
- '''
-# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.18
+# name: TestActionApi.test_listing_actions_is_not_nplus1.15
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -753,7 +659,7 @@
LIMIT 21
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.19
+# name: TestActionApi.test_listing_actions_is_not_nplus1.16
'''
SELECT "ee_accesscontrol"."id",
"ee_accesscontrol"."team_id",
@@ -776,23 +682,7 @@
AND "ee_accesscontrol"."team_id" = 99999))
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.2
- '''
- SELECT "posthog_project"."updated_at",
- "posthog_project"."id",
- "posthog_project"."organization_id",
- "posthog_project"."name",
- "posthog_project"."logo_media_id",
- "posthog_project"."created_at",
- "posthog_project"."product_description",
- "posthog_project"."is_pending_deletion",
- "posthog_project"."deletion_scheduled_at"
- FROM "posthog_project"
- WHERE "posthog_project"."id" = 99999
- LIMIT 21
- '''
-# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.20
+# name: TestActionApi.test_listing_actions_is_not_nplus1.17
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -845,7 +735,7 @@
WHERE "posthog_organizationmembership"."user_id" = 99999
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.21
+# name: TestActionApi.test_listing_actions_is_not_nplus1.18
'''
SELECT "posthog_action"."id",
"posthog_action"."name",
@@ -928,7 +818,7 @@
"posthog_action"."name" ASC
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.22
+# name: TestActionApi.test_listing_actions_is_not_nplus1.19
'''
SELECT "posthog_taggeditem"."id",
"posthog_taggeditem"."tag_id",
@@ -949,7 +839,7 @@
5 /* ... */))
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.3
+# name: TestActionApi.test_listing_actions_is_not_nplus1.2
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -1004,7 +894,7 @@
LIMIT 21
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.4
+# name: TestActionApi.test_listing_actions_is_not_nplus1.3
'''
SELECT "ee_accesscontrol"."id",
"ee_accesscontrol"."team_id",
@@ -1027,7 +917,7 @@
AND "ee_accesscontrol"."team_id" = 99999))
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.5
+# name: TestActionApi.test_listing_actions_is_not_nplus1.4
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -1080,7 +970,7 @@
WHERE "posthog_organizationmembership"."user_id" = 99999
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.6
+# name: TestActionApi.test_listing_actions_is_not_nplus1.5
'''
SELECT "posthog_action"."id",
"posthog_action"."name",
@@ -1163,7 +1053,7 @@
"posthog_action"."name" ASC
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.7
+# name: TestActionApi.test_listing_actions_is_not_nplus1.6
'''
SELECT "posthog_user"."id",
"posthog_user"."password",
@@ -1249,7 +1139,7 @@
AND "posthog_user"."id" = 99999)
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.8
+# name: TestActionApi.test_listing_actions_is_not_nplus1.7
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -1387,19 +1277,81 @@
LIMIT 21
'''
# ---
-# name: TestActionApi.test_listing_actions_is_not_nplus1.9
+# name: TestActionApi.test_listing_actions_is_not_nplus1.8
'''
- SELECT "posthog_project"."updated_at",
- "posthog_project"."id",
- "posthog_project"."organization_id",
- "posthog_project"."name",
- "posthog_project"."logo_media_id",
- "posthog_project"."created_at",
- "posthog_project"."product_description",
- "posthog_project"."is_pending_deletion",
- "posthog_project"."deletion_scheduled_at"
- FROM "posthog_project"
- WHERE "posthog_project"."id" = 99999
+ SELECT "posthog_organizationmembership"."id",
+ "posthog_organizationmembership"."organization_id",
+ "posthog_organizationmembership"."user_id",
+ "posthog_organizationmembership"."level",
+ "posthog_organizationmembership"."joined_at",
+ "posthog_organizationmembership"."updated_at",
+ "posthog_organizationmembership"."invited_by_id",
+ "posthog_organization"."id",
+ "posthog_organization"."name",
+ "posthog_organization"."slug",
+ "posthog_organization"."logo_media_id",
+ "posthog_organization"."created_at",
+ "posthog_organization"."updated_at",
+ "posthog_organization"."is_active",
+ "posthog_organization"."is_not_active_reason",
+ "posthog_organization"."session_cookie_age",
+ "posthog_organization"."is_member_join_email_enabled",
+ "posthog_organization"."is_ai_data_processing_approved",
+ "posthog_organization"."is_ai_training_opted_in",
+ "posthog_organization"."is_ai_training_locked",
+ "posthog_organization"."is_ai_training_cta_shown",
+ "posthog_organization"."enforce_2fa",
+ "posthog_organization"."enforce_verified_domains",
+ "posthog_organization"."members_can_invite",
+ "posthog_organization"."members_can_create_projects",
+ "posthog_organization"."members_can_use_personal_api_keys",
+ "posthog_organization"."members_can_see_org_members",
+ "posthog_organization"."uses_most_specific_access_resolution",
+ "posthog_organization"."allow_publicly_shared_resources",
+ "posthog_organization"."member_notice",
+ "posthog_organization"."read_only_mcp_access",
+ "posthog_organization"."default_role_id",
+ "posthog_organization"."plugins_access_level",
+ "posthog_organization"."for_internal_metrics",
+ "posthog_organization"."default_anonymize_ips",
+ "posthog_organization"."is_pending_deletion",
+ "posthog_organization"."customer_id",
+ "posthog_organization"."available_product_features",
+ "posthog_organization"."usage",
+ "posthog_organization"."never_drop_data",
+ "posthog_organization"."customer_trust_scores",
+ "posthog_organization"."has_active_subscription",
+ "posthog_organization"."setup_section_2_completed",
+ "posthog_organization"."personalization",
+ "posthog_organization"."domain_whitelist",
+ "posthog_organization"."is_platform"
+ FROM "posthog_organizationmembership"
+ INNER JOIN "posthog_organization" ON ("posthog_organizationmembership"."organization_id" = "posthog_organization"."id")
+ WHERE ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
+ AND "posthog_organizationmembership"."user_id" = 99999)
LIMIT 21
'''
# ---
+# name: TestActionApi.test_listing_actions_is_not_nplus1.9
+ '''
+ SELECT "ee_accesscontrol"."id",
+ "ee_accesscontrol"."team_id",
+ "ee_accesscontrol"."access_level",
+ "ee_accesscontrol"."resource",
+ "ee_accesscontrol"."resource_id",
+ "ee_accesscontrol"."organization_member_id",
+ "ee_accesscontrol"."role_id",
+ "ee_accesscontrol"."created_by_id",
+ "ee_accesscontrol"."created_at",
+ "ee_accesscontrol"."updated_at"
+ FROM "ee_accesscontrol"
+ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id")
+ WHERE (("ee_accesscontrol"."organization_member_id" IS NULL
+ AND "ee_accesscontrol"."role_id" IS NULL
+ AND "ee_accesscontrol"."team_id" = 99999)
+ OR ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
+ AND "posthog_organizationmembership"."user_id" = 99999
+ AND "ee_accesscontrol"."role_id" IS NULL
+ AND "ee_accesscontrol"."team_id" = 99999))
+ '''
+# ---
diff --git a/products/actions/backend/tests/api/test_action.py b/products/actions/backend/tests/api/test_action.py
index 3e983464e97a..b9bd203ef4e7 100644
--- a/products/actions/backend/tests/api/test_action.py
+++ b/products/actions/backend/tests/api/test_action.py
@@ -335,7 +335,7 @@ class TestActionApi(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
)
# test queries
- with self.assertNumQueries(FuzzyInt(9, 11)):
+ with self.assertNumQueries(FuzzyInt(8, 10)):
# Django session, user, team, org membership, instance setting, org,
# count, action
self.client.get(f"/api/projects/{self.team.id}/actions/")
@@ -509,7 +509,7 @@ class TestActionApi(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
self.client.get(f"/api/projects/{self.team.id}/actions/")
# No actions yet, so no tags prefetch query
- with self.assertNumQueries(8), snapshot_postgres_queries_context(self):
+ with self.assertNumQueries(7), snapshot_postgres_queries_context(self):
self.client.get(f"/api/projects/{self.team.id}/actions/")
Action.objects.create(
@@ -519,7 +519,7 @@ class TestActionApi(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
)
# With actions, there's an extra tags prefetch query
- with self.assertNumQueries(9), snapshot_postgres_queries_context(self):
+ with self.assertNumQueries(8), snapshot_postgres_queries_context(self):
self.client.get(f"/api/projects/{self.team.id}/actions/")
Action.objects.create(
@@ -528,7 +528,7 @@ class TestActionApi(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
created_by=User.objects.create_and_join(self.organization, "b", ""),
)
- with self.assertNumQueries(9), snapshot_postgres_queries_context(self):
+ with self.assertNumQueries(8), snapshot_postgres_queries_context(self):
self.client.get(f"/api/projects/{self.team.id}/actions/")
@parameterized.expand(
diff --git a/products/annotations/backend/api/test/__snapshots__/test_annotation.ambr b/products/annotations/backend/api/test/__snapshots__/test_annotation.ambr
index ed3170b72a65..8060ab5effd0 100644
--- a/products/annotations/backend/api/test/__snapshots__/test_annotation.ambr
+++ b/products/annotations/backend/api/test/__snapshots__/test_annotation.ambr
@@ -224,84 +224,6 @@
'''
# ---
# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.10
- '''
- SELECT "posthog_organizationmembership"."id",
- "posthog_organizationmembership"."organization_id",
- "posthog_organizationmembership"."user_id",
- "posthog_organizationmembership"."level",
- "posthog_organizationmembership"."joined_at",
- "posthog_organizationmembership"."updated_at",
- "posthog_organizationmembership"."invited_by_id",
- "posthog_organization"."id",
- "posthog_organization"."name",
- "posthog_organization"."slug",
- "posthog_organization"."logo_media_id",
- "posthog_organization"."created_at",
- "posthog_organization"."updated_at",
- "posthog_organization"."is_active",
- "posthog_organization"."is_not_active_reason",
- "posthog_organization"."session_cookie_age",
- "posthog_organization"."is_member_join_email_enabled",
- "posthog_organization"."is_ai_data_processing_approved",
- "posthog_organization"."is_ai_training_opted_in",
- "posthog_organization"."is_ai_training_locked",
- "posthog_organization"."is_ai_training_cta_shown",
- "posthog_organization"."enforce_2fa",
- "posthog_organization"."enforce_verified_domains",
- "posthog_organization"."members_can_invite",
- "posthog_organization"."members_can_create_projects",
- "posthog_organization"."members_can_use_personal_api_keys",
- "posthog_organization"."members_can_see_org_members",
- "posthog_organization"."uses_most_specific_access_resolution",
- "posthog_organization"."allow_publicly_shared_resources",
- "posthog_organization"."member_notice",
- "posthog_organization"."read_only_mcp_access",
- "posthog_organization"."default_role_id",
- "posthog_organization"."plugins_access_level",
- "posthog_organization"."for_internal_metrics",
- "posthog_organization"."default_anonymize_ips",
- "posthog_organization"."is_pending_deletion",
- "posthog_organization"."customer_id",
- "posthog_organization"."available_product_features",
- "posthog_organization"."usage",
- "posthog_organization"."never_drop_data",
- "posthog_organization"."customer_trust_scores",
- "posthog_organization"."has_active_subscription",
- "posthog_organization"."setup_section_2_completed",
- "posthog_organization"."personalization",
- "posthog_organization"."domain_whitelist",
- "posthog_organization"."is_platform"
- FROM "posthog_organizationmembership"
- INNER JOIN "posthog_organization" ON ("posthog_organizationmembership"."organization_id" = "posthog_organization"."id")
- WHERE ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
- AND "posthog_organizationmembership"."user_id" = 99999)
- LIMIT 21
- '''
-# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.11
- '''
- SELECT "ee_accesscontrol"."id",
- "ee_accesscontrol"."team_id",
- "ee_accesscontrol"."access_level",
- "ee_accesscontrol"."resource",
- "ee_accesscontrol"."resource_id",
- "ee_accesscontrol"."organization_member_id",
- "ee_accesscontrol"."role_id",
- "ee_accesscontrol"."created_by_id",
- "ee_accesscontrol"."created_at",
- "ee_accesscontrol"."updated_at"
- FROM "ee_accesscontrol"
- LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id")
- WHERE (("ee_accesscontrol"."organization_member_id" IS NULL
- AND "ee_accesscontrol"."role_id" IS NULL
- AND "ee_accesscontrol"."team_id" = 99999)
- OR ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
- AND "posthog_organizationmembership"."user_id" = 99999
- AND "ee_accesscontrol"."role_id" IS NULL
- AND "ee_accesscontrol"."team_id" = 99999))
- '''
-# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.12
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -354,7 +276,7 @@
WHERE "posthog_organizationmembership"."user_id" = 99999
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.13
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.11
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_annotation"
@@ -372,7 +294,7 @@
OR "posthog_annotation"."team_id" = 99999))
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.14
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.12
'''
SELECT "posthog_annotation"."id",
"posthog_annotation"."content",
@@ -499,7 +421,7 @@
LIMIT 1000
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.15
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.13
'''
SELECT "posthog_user"."id",
"posthog_user"."password",
@@ -585,7 +507,7 @@
AND "posthog_user"."id" = 99999)
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.16
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.14
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -723,23 +645,7 @@
LIMIT 21
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.17
- '''
- SELECT "posthog_project"."updated_at",
- "posthog_project"."id",
- "posthog_project"."organization_id",
- "posthog_project"."name",
- "posthog_project"."logo_media_id",
- "posthog_project"."created_at",
- "posthog_project"."product_description",
- "posthog_project"."is_pending_deletion",
- "posthog_project"."deletion_scheduled_at"
- FROM "posthog_project"
- WHERE "posthog_project"."id" = 99999
- LIMIT 21
- '''
-# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.18
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.15
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -794,7 +700,7 @@
LIMIT 21
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.19
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.16
'''
SELECT "ee_accesscontrol"."id",
"ee_accesscontrol"."team_id",
@@ -817,23 +723,7 @@
AND "ee_accesscontrol"."team_id" = 99999))
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.2
- '''
- SELECT "posthog_project"."updated_at",
- "posthog_project"."id",
- "posthog_project"."organization_id",
- "posthog_project"."name",
- "posthog_project"."logo_media_id",
- "posthog_project"."created_at",
- "posthog_project"."product_description",
- "posthog_project"."is_pending_deletion",
- "posthog_project"."deletion_scheduled_at"
- FROM "posthog_project"
- WHERE "posthog_project"."id" = 99999
- LIMIT 21
- '''
-# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.20
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.17
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -886,7 +776,7 @@
WHERE "posthog_organizationmembership"."user_id" = 99999
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.21
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.18
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_annotation"
@@ -904,7 +794,7 @@
OR "posthog_annotation"."team_id" = 99999))
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.22
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.19
'''
SELECT "posthog_annotation"."id",
"posthog_annotation"."content",
@@ -1031,7 +921,62 @@
LIMIT 1000
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.23
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.2
+ '''
+ SELECT "posthog_organizationmembership"."id",
+ "posthog_organizationmembership"."organization_id",
+ "posthog_organizationmembership"."user_id",
+ "posthog_organizationmembership"."level",
+ "posthog_organizationmembership"."joined_at",
+ "posthog_organizationmembership"."updated_at",
+ "posthog_organizationmembership"."invited_by_id",
+ "posthog_organization"."id",
+ "posthog_organization"."name",
+ "posthog_organization"."slug",
+ "posthog_organization"."logo_media_id",
+ "posthog_organization"."created_at",
+ "posthog_organization"."updated_at",
+ "posthog_organization"."is_active",
+ "posthog_organization"."is_not_active_reason",
+ "posthog_organization"."session_cookie_age",
+ "posthog_organization"."is_member_join_email_enabled",
+ "posthog_organization"."is_ai_data_processing_approved",
+ "posthog_organization"."is_ai_training_opted_in",
+ "posthog_organization"."is_ai_training_locked",
+ "posthog_organization"."is_ai_training_cta_shown",
+ "posthog_organization"."enforce_2fa",
+ "posthog_organization"."enforce_verified_domains",
+ "posthog_organization"."members_can_invite",
+ "posthog_organization"."members_can_create_projects",
+ "posthog_organization"."members_can_use_personal_api_keys",
+ "posthog_organization"."members_can_see_org_members",
+ "posthog_organization"."uses_most_specific_access_resolution",
+ "posthog_organization"."allow_publicly_shared_resources",
+ "posthog_organization"."member_notice",
+ "posthog_organization"."read_only_mcp_access",
+ "posthog_organization"."default_role_id",
+ "posthog_organization"."plugins_access_level",
+ "posthog_organization"."for_internal_metrics",
+ "posthog_organization"."default_anonymize_ips",
+ "posthog_organization"."is_pending_deletion",
+ "posthog_organization"."customer_id",
+ "posthog_organization"."available_product_features",
+ "posthog_organization"."usage",
+ "posthog_organization"."never_drop_data",
+ "posthog_organization"."customer_trust_scores",
+ "posthog_organization"."has_active_subscription",
+ "posthog_organization"."setup_section_2_completed",
+ "posthog_organization"."personalization",
+ "posthog_organization"."domain_whitelist",
+ "posthog_organization"."is_platform"
+ FROM "posthog_organizationmembership"
+ INNER JOIN "posthog_organization" ON ("posthog_organizationmembership"."organization_id" = "posthog_organization"."id")
+ WHERE ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
+ AND "posthog_organizationmembership"."user_id" = 99999)
+ LIMIT 21
+ '''
+# ---
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.20
'''
SELECT "posthog_user"."id",
"posthog_user"."password",
@@ -1117,7 +1062,7 @@
AND "posthog_user"."id" = 99999)
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.24
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.21
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -1255,23 +1200,7 @@
LIMIT 21
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.25
- '''
- SELECT "posthog_project"."updated_at",
- "posthog_project"."id",
- "posthog_project"."organization_id",
- "posthog_project"."name",
- "posthog_project"."logo_media_id",
- "posthog_project"."created_at",
- "posthog_project"."product_description",
- "posthog_project"."is_pending_deletion",
- "posthog_project"."deletion_scheduled_at"
- FROM "posthog_project"
- WHERE "posthog_project"."id" = 99999
- LIMIT 21
- '''
-# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.26
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.22
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -1326,7 +1255,7 @@
LIMIT 21
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.27
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.23
'''
SELECT "ee_accesscontrol"."id",
"ee_accesscontrol"."team_id",
@@ -1349,7 +1278,7 @@
AND "ee_accesscontrol"."team_id" = 99999))
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.28
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.24
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -1402,7 +1331,7 @@
WHERE "posthog_organizationmembership"."user_id" = 99999
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.29
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.25
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_annotation"
@@ -1420,62 +1349,7 @@
OR "posthog_annotation"."team_id" = 99999))
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.3
- '''
- SELECT "posthog_organizationmembership"."id",
- "posthog_organizationmembership"."organization_id",
- "posthog_organizationmembership"."user_id",
- "posthog_organizationmembership"."level",
- "posthog_organizationmembership"."joined_at",
- "posthog_organizationmembership"."updated_at",
- "posthog_organizationmembership"."invited_by_id",
- "posthog_organization"."id",
- "posthog_organization"."name",
- "posthog_organization"."slug",
- "posthog_organization"."logo_media_id",
- "posthog_organization"."created_at",
- "posthog_organization"."updated_at",
- "posthog_organization"."is_active",
- "posthog_organization"."is_not_active_reason",
- "posthog_organization"."session_cookie_age",
- "posthog_organization"."is_member_join_email_enabled",
- "posthog_organization"."is_ai_data_processing_approved",
- "posthog_organization"."is_ai_training_opted_in",
- "posthog_organization"."is_ai_training_locked",
- "posthog_organization"."is_ai_training_cta_shown",
- "posthog_organization"."enforce_2fa",
- "posthog_organization"."enforce_verified_domains",
- "posthog_organization"."members_can_invite",
- "posthog_organization"."members_can_create_projects",
- "posthog_organization"."members_can_use_personal_api_keys",
- "posthog_organization"."members_can_see_org_members",
- "posthog_organization"."uses_most_specific_access_resolution",
- "posthog_organization"."allow_publicly_shared_resources",
- "posthog_organization"."member_notice",
- "posthog_organization"."read_only_mcp_access",
- "posthog_organization"."default_role_id",
- "posthog_organization"."plugins_access_level",
- "posthog_organization"."for_internal_metrics",
- "posthog_organization"."default_anonymize_ips",
- "posthog_organization"."is_pending_deletion",
- "posthog_organization"."customer_id",
- "posthog_organization"."available_product_features",
- "posthog_organization"."usage",
- "posthog_organization"."never_drop_data",
- "posthog_organization"."customer_trust_scores",
- "posthog_organization"."has_active_subscription",
- "posthog_organization"."setup_section_2_completed",
- "posthog_organization"."personalization",
- "posthog_organization"."domain_whitelist",
- "posthog_organization"."is_platform"
- FROM "posthog_organizationmembership"
- INNER JOIN "posthog_organization" ON ("posthog_organizationmembership"."organization_id" = "posthog_organization"."id")
- WHERE ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
- AND "posthog_organizationmembership"."user_id" = 99999)
- LIMIT 21
- '''
-# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.30
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.26
'''
SELECT "posthog_annotation"."id",
"posthog_annotation"."content",
@@ -1602,7 +1476,7 @@
LIMIT 1000
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.4
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.3
'''
SELECT "ee_accesscontrol"."id",
"ee_accesscontrol"."team_id",
@@ -1625,7 +1499,7 @@
AND "ee_accesscontrol"."team_id" = 99999))
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.5
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.4
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -1678,7 +1552,7 @@
WHERE "posthog_organizationmembership"."user_id" = 99999
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.6
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.5
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_annotation"
@@ -1696,7 +1570,7 @@
OR "posthog_annotation"."team_id" = 99999))
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.7
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.6
'''
SELECT "posthog_user"."id",
"posthog_user"."password",
@@ -1782,7 +1656,7 @@
AND "posthog_user"."id" = 99999)
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.8
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.7
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -1920,19 +1794,81 @@
LIMIT 21
'''
# ---
-# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.9
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.8
'''
- SELECT "posthog_project"."updated_at",
- "posthog_project"."id",
- "posthog_project"."organization_id",
- "posthog_project"."name",
- "posthog_project"."logo_media_id",
- "posthog_project"."created_at",
- "posthog_project"."product_description",
- "posthog_project"."is_pending_deletion",
- "posthog_project"."deletion_scheduled_at"
- FROM "posthog_project"
- WHERE "posthog_project"."id" = 99999
+ SELECT "posthog_organizationmembership"."id",
+ "posthog_organizationmembership"."organization_id",
+ "posthog_organizationmembership"."user_id",
+ "posthog_organizationmembership"."level",
+ "posthog_organizationmembership"."joined_at",
+ "posthog_organizationmembership"."updated_at",
+ "posthog_organizationmembership"."invited_by_id",
+ "posthog_organization"."id",
+ "posthog_organization"."name",
+ "posthog_organization"."slug",
+ "posthog_organization"."logo_media_id",
+ "posthog_organization"."created_at",
+ "posthog_organization"."updated_at",
+ "posthog_organization"."is_active",
+ "posthog_organization"."is_not_active_reason",
+ "posthog_organization"."session_cookie_age",
+ "posthog_organization"."is_member_join_email_enabled",
+ "posthog_organization"."is_ai_data_processing_approved",
+ "posthog_organization"."is_ai_training_opted_in",
+ "posthog_organization"."is_ai_training_locked",
+ "posthog_organization"."is_ai_training_cta_shown",
+ "posthog_organization"."enforce_2fa",
+ "posthog_organization"."enforce_verified_domains",
+ "posthog_organization"."members_can_invite",
+ "posthog_organization"."members_can_create_projects",
+ "posthog_organization"."members_can_use_personal_api_keys",
+ "posthog_organization"."members_can_see_org_members",
+ "posthog_organization"."uses_most_specific_access_resolution",
+ "posthog_organization"."allow_publicly_shared_resources",
+ "posthog_organization"."member_notice",
+ "posthog_organization"."read_only_mcp_access",
+ "posthog_organization"."default_role_id",
+ "posthog_organization"."plugins_access_level",
+ "posthog_organization"."for_internal_metrics",
+ "posthog_organization"."default_anonymize_ips",
+ "posthog_organization"."is_pending_deletion",
+ "posthog_organization"."customer_id",
+ "posthog_organization"."available_product_features",
+ "posthog_organization"."usage",
+ "posthog_organization"."never_drop_data",
+ "posthog_organization"."customer_trust_scores",
+ "posthog_organization"."has_active_subscription",
+ "posthog_organization"."setup_section_2_completed",
+ "posthog_organization"."personalization",
+ "posthog_organization"."domain_whitelist",
+ "posthog_organization"."is_platform"
+ FROM "posthog_organizationmembership"
+ INNER JOIN "posthog_organization" ON ("posthog_organizationmembership"."organization_id" = "posthog_organization"."id")
+ WHERE ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
+ AND "posthog_organizationmembership"."user_id" = 99999)
LIMIT 21
'''
# ---
+# name: TestAnnotation.test_retrieving_annotation_is_not_n_plus_1.9
+ '''
+ SELECT "ee_accesscontrol"."id",
+ "ee_accesscontrol"."team_id",
+ "ee_accesscontrol"."access_level",
+ "ee_accesscontrol"."resource",
+ "ee_accesscontrol"."resource_id",
+ "ee_accesscontrol"."organization_member_id",
+ "ee_accesscontrol"."role_id",
+ "ee_accesscontrol"."created_by_id",
+ "ee_accesscontrol"."created_at",
+ "ee_accesscontrol"."updated_at"
+ FROM "ee_accesscontrol"
+ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id")
+ WHERE (("ee_accesscontrol"."organization_member_id" IS NULL
+ AND "ee_accesscontrol"."role_id" IS NULL
+ AND "ee_accesscontrol"."team_id" = 99999)
+ OR ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
+ AND "posthog_organizationmembership"."user_id" = 99999
+ AND "ee_accesscontrol"."role_id" IS NULL
+ AND "ee_accesscontrol"."team_id" = 99999))
+ '''
+# ---
diff --git a/products/annotations/backend/api/test/test_annotation.py b/products/annotations/backend/api/test/test_annotation.py
index 02dcb337b086..da97716cc1f5 100644
--- a/products/annotations/backend/api/test/test_annotation.py
+++ b/products/annotations/backend/api/test/test_annotation.py
@@ -68,7 +68,7 @@ class TestAnnotation(APIBaseTest, QueryMatchingTest):
@patch("products.annotations.backend.activity_logging.report_user_action")
def test_retrieving_annotation_is_not_n_plus_1(self, _mock_capture: MagicMock) -> None:
- with self.assertNumQueries(FuzzyInt(9, 10)), snapshot_postgres_queries_context(self):
+ with self.assertNumQueries(FuzzyInt(8, 9)), snapshot_postgres_queries_context(self):
response = self.client.get(f"/api/projects/{self.team.id}/annotations/").json()
assert len(response["results"]) == 0
@@ -80,7 +80,7 @@ class TestAnnotation(APIBaseTest, QueryMatchingTest):
content=now().isoformat(),
)
- with self.assertNumQueries(FuzzyInt(9, 10)), snapshot_postgres_queries_context(self):
+ with self.assertNumQueries(FuzzyInt(8, 9)), snapshot_postgres_queries_context(self):
response = self.client.get(f"/api/projects/{self.team.id}/annotations/").json()
assert len(response["results"]) == 1
@@ -92,7 +92,7 @@ class TestAnnotation(APIBaseTest, QueryMatchingTest):
content=now().isoformat(),
)
- with self.assertNumQueries(FuzzyInt(9, 10)), snapshot_postgres_queries_context(self):
+ with self.assertNumQueries(FuzzyInt(8, 9)), snapshot_postgres_queries_context(self):
response = self.client.get(f"/api/projects/{self.team.id}/annotations/").json()
assert len(response["results"]) == 2
@@ -110,7 +110,7 @@ class TestAnnotation(APIBaseTest, QueryMatchingTest):
scope=Annotation.Scope.PROJECT,
)
- with self.assertNumQueries(FuzzyInt(9, 10)), snapshot_postgres_queries_context(self):
+ with self.assertNumQueries(FuzzyInt(8, 9)), snapshot_postgres_queries_context(self):
response = self.client.get(f"/api/projects/{self.team.id}/annotations/").json()
assert len(response["results"]) == 4
diff --git a/products/experiments/backend/test/test_presentation_api.py b/products/experiments/backend/test/test_presentation_api.py
index 191c13cd7cb8..9ec44925248f 100644
--- a/products/experiments/backend/test/test_presentation_api.py
+++ b/products/experiments/backend/test/test_presentation_api.py
@@ -550,7 +550,7 @@ class TestExperimentCRUD(_HoistFlagConfigClientMixin, APILicensedTest):
# Query count must stay flat as rows grow — five experiments must not cost more than one.
self.assertLessEqual(len(five_rows.captured_queries), len(single_row.captured_queries))
# The list serializer stamps no calculation keys, so the saved metrics cost no extra query.
- self.assertEqual(len(five_rows.captured_queries), 14)
+ self.assertEqual(len(five_rows.captured_queries), 13)
def test_experiment_detail_with_saved_metrics_reads_each_relation_once(self) -> None:
experiment = self._create_fully_populated_experiment(0)
@@ -567,7 +567,7 @@ class TestExperimentCRUD(_HoistFlagConfigClientMixin, APILicensedTest):
# The saved-metric fingerprints reuse the prefetched links and add one read of the team's
# experiment settings, whatever the number of saved metrics.
- with self.assertNumQueries(16):
+ with self.assertNumQueries(15):
response = self.client.get(f"/api/projects/{self.team.id}/experiments/{experiment.id}/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -3913,7 +3913,7 @@ class TestExperimentCRUD(_HoistFlagConfigClientMixin, APILicensedTest):
# TODO: Make sure permission bool doesn't cause n + 1
# +1 query for survey internal flag IDs lookup, +1 for the project's replay gates
- with self.assertNumQueries(20):
+ with self.assertNumQueries(19):
response = self.client.get(f"/api/projects/{self.team.id}/feature_flags")
self.assertEqual(response.status_code, status.HTTP_200_OK)
result = response.json()
diff --git a/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr b/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr
index fb001e05a5f6..974bdc0cdedf 100644
--- a/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr
+++ b/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr
@@ -896,22 +896,6 @@
'''
# ---
# name: TestFeatureFlag.test_creating_static_cohort.10
- '''
- SELECT "posthog_experiment"."id",
- "posthog_experiment"."name",
- "posthog_experiment"."feature_flag_id",
- "posthog_experiment"."start_date",
- "posthog_experiment"."end_date"
- FROM "posthog_experiment"
- WHERE (NOT "posthog_experiment"."deleted"
- AND "posthog_experiment"."feature_flag_id" IN (1,
- 2,
- 3,
- 4,
- 5 /* ... */))
- '''
-# ---
-# name: TestFeatureFlag.test_creating_static_cohort.11
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -1009,7 +993,7 @@
LIMIT 21
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.12
+# name: TestFeatureFlag.test_creating_static_cohort.11
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
@@ -1034,7 +1018,7 @@
AND "posthog_filesystem"."shortcut" IS NOT NULL))
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.13
+# name: TestFeatureFlag.test_creating_static_cohort.12
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
@@ -1059,7 +1043,7 @@
AND "posthog_filesystem"."shortcut" IS NOT NULL))
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.14
+# name: TestFeatureFlag.test_creating_static_cohort.13
'''
SELECT "posthog_filesystemshortcut"."id",
"posthog_filesystemshortcut"."team_id",
@@ -1082,7 +1066,7 @@
AND "posthog_filesystemshortcut"."href" IS NOT NULL))
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.15
+# name: TestFeatureFlag.test_creating_static_cohort.14
'''
SELECT "posthog_team"."id",
"posthog_team"."project_id"
@@ -1091,7 +1075,7 @@
LIMIT 21
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.16
+# name: TestFeatureFlag.test_creating_static_cohort.15
'''
SELECT "posthog_cohort"."id",
"posthog_cohort"."name",
@@ -1130,7 +1114,7 @@
LIMIT 21
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.17
+# name: TestFeatureFlag.test_creating_static_cohort.16
'''
SELECT "posthog_featureflag"."id",
"posthog_featureflag"."key",
@@ -1161,7 +1145,7 @@
LIMIT 21
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.18
+# name: TestFeatureFlag.test_creating_static_cohort.17
'''
SELECT "feature_flags_teamfeatureflagsconfig"."property_matching_version" AS "property_matching_version"
FROM "feature_flags_teamfeatureflagsconfig"
@@ -1170,7 +1154,7 @@
LIMIT 1
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.19
+# name: TestFeatureFlag.test_creating_static_cohort.18
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -1268,23 +1252,7 @@
LIMIT 21
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.2
- '''
- SELECT "posthog_project"."updated_at",
- "posthog_project"."id",
- "posthog_project"."organization_id",
- "posthog_project"."name",
- "posthog_project"."logo_media_id",
- "posthog_project"."created_at",
- "posthog_project"."product_description",
- "posthog_project"."is_pending_deletion",
- "posthog_project"."deletion_scheduled_at"
- FROM "posthog_project"
- WHERE "posthog_project"."id" = 99999
- LIMIT 21
- '''
-# ---
-# name: TestFeatureFlag.test_creating_static_cohort.20
+# name: TestFeatureFlag.test_creating_static_cohort.19
'''
SELECT 1 AS "a"
FROM "posthog_cohort"
@@ -1300,7 +1268,62 @@
LIMIT 1
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.21
+# name: TestFeatureFlag.test_creating_static_cohort.2
+ '''
+ SELECT "posthog_organizationmembership"."id",
+ "posthog_organizationmembership"."organization_id",
+ "posthog_organizationmembership"."user_id",
+ "posthog_organizationmembership"."level",
+ "posthog_organizationmembership"."joined_at",
+ "posthog_organizationmembership"."updated_at",
+ "posthog_organizationmembership"."invited_by_id",
+ "posthog_organization"."id",
+ "posthog_organization"."name",
+ "posthog_organization"."slug",
+ "posthog_organization"."logo_media_id",
+ "posthog_organization"."created_at",
+ "posthog_organization"."updated_at",
+ "posthog_organization"."is_active",
+ "posthog_organization"."is_not_active_reason",
+ "posthog_organization"."session_cookie_age",
+ "posthog_organization"."is_member_join_email_enabled",
+ "posthog_organization"."is_ai_data_processing_approved",
+ "posthog_organization"."is_ai_training_opted_in",
+ "posthog_organization"."is_ai_training_locked",
+ "posthog_organization"."is_ai_training_cta_shown",
+ "posthog_organization"."enforce_2fa",
+ "posthog_organization"."enforce_verified_domains",
+ "posthog_organization"."members_can_invite",
+ "posthog_organization"."members_can_create_projects",
+ "posthog_organization"."members_can_use_personal_api_keys",
+ "posthog_organization"."members_can_see_org_members",
+ "posthog_organization"."uses_most_specific_access_resolution",
+ "posthog_organization"."allow_publicly_shared_resources",
+ "posthog_organization"."member_notice",
+ "posthog_organization"."read_only_mcp_access",
+ "posthog_organization"."default_role_id",
+ "posthog_organization"."plugins_access_level",
+ "posthog_organization"."for_internal_metrics",
+ "posthog_organization"."default_anonymize_ips",
+ "posthog_organization"."is_pending_deletion",
+ "posthog_organization"."customer_id",
+ "posthog_organization"."available_product_features",
+ "posthog_organization"."usage",
+ "posthog_organization"."never_drop_data",
+ "posthog_organization"."customer_trust_scores",
+ "posthog_organization"."has_active_subscription",
+ "posthog_organization"."setup_section_2_completed",
+ "posthog_organization"."personalization",
+ "posthog_organization"."domain_whitelist",
+ "posthog_organization"."is_platform"
+ FROM "posthog_organizationmembership"
+ INNER JOIN "posthog_organization" ON ("posthog_organizationmembership"."organization_id" = "posthog_organization"."id")
+ WHERE ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
+ AND "posthog_organizationmembership"."user_id" = 99999)
+ LIMIT 21
+ '''
+# ---
+# name: TestFeatureFlag.test_creating_static_cohort.20
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -1398,7 +1421,7 @@
LIMIT 21
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.22
+# name: TestFeatureFlag.test_creating_static_cohort.21
'''
SELECT "posthog_filesystem"."team_id",
"posthog_filesystem"."id",
@@ -1423,7 +1446,7 @@
AND "posthog_filesystem"."shortcut" IS NOT NULL))
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.23
+# name: TestFeatureFlag.test_creating_static_cohort.22
'''
SELECT "posthog_filesystemshortcut"."id",
"posthog_filesystemshortcut"."team_id",
@@ -1446,7 +1469,7 @@
AND "posthog_filesystemshortcut"."href" IS NOT NULL))
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.24
+# name: TestFeatureFlag.test_creating_static_cohort.23
'''
SELECT "posthog_cohort"."id",
"posthog_cohort"."name",
@@ -1483,7 +1506,7 @@
LIMIT 21
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.25
+# name: TestFeatureFlag.test_creating_static_cohort.24
'''
SELECT "cohort_realtime_team_access"."team_id",
"cohort_realtime_team_access"."level",
@@ -1495,7 +1518,7 @@
FROM "cohort_realtime_team_access"
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.26
+# name: TestFeatureFlag.test_creating_static_cohort.25
'''
SELECT "posthog_team"."id",
"posthog_team"."uuid",
@@ -1593,7 +1616,7 @@
LIMIT 21
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.27
+# name: TestFeatureFlag.test_creating_static_cohort.26
'''
SELECT "posthog_user"."id",
"posthog_user"."password",
@@ -1640,7 +1663,7 @@
LIMIT 21
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.28
+# name: TestFeatureFlag.test_creating_static_cohort.27
'''
SELECT "posthog_cohortcalculationhistory"."id",
"posthog_cohortcalculationhistory"."team_id",
@@ -1660,7 +1683,7 @@
LIMIT 1
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.29
+# name: TestFeatureFlag.test_creating_static_cohort.28
'''
SELECT "cohort_realtime_team_access"."team_id",
"cohort_realtime_team_access"."level",
@@ -1672,62 +1695,7 @@
FROM "cohort_realtime_team_access"
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.3
- '''
- SELECT "posthog_organizationmembership"."id",
- "posthog_organizationmembership"."organization_id",
- "posthog_organizationmembership"."user_id",
- "posthog_organizationmembership"."level",
- "posthog_organizationmembership"."joined_at",
- "posthog_organizationmembership"."updated_at",
- "posthog_organizationmembership"."invited_by_id",
- "posthog_organization"."id",
- "posthog_organization"."name",
- "posthog_organization"."slug",
- "posthog_organization"."logo_media_id",
- "posthog_organization"."created_at",
- "posthog_organization"."updated_at",
- "posthog_organization"."is_active",
- "posthog_organization"."is_not_active_reason",
- "posthog_organization"."session_cookie_age",
- "posthog_organization"."is_member_join_email_enabled",
- "posthog_organization"."is_ai_data_processing_approved",
- "posthog_organization"."is_ai_training_opted_in",
- "posthog_organization"."is_ai_training_locked",
- "posthog_organization"."is_ai_training_cta_shown",
- "posthog_organization"."enforce_2fa",
- "posthog_organization"."enforce_verified_domains",
- "posthog_organization"."members_can_invite",
- "posthog_organization"."members_can_create_projects",
- "posthog_organization"."members_can_use_personal_api_keys",
- "posthog_organization"."members_can_see_org_members",
- "posthog_organization"."uses_most_specific_access_resolution",
- "posthog_organization"."allow_publicly_shared_resources",
- "posthog_organization"."member_notice",
- "posthog_organization"."read_only_mcp_access",
- "posthog_organization"."default_role_id",
- "posthog_organization"."plugins_access_level",
- "posthog_organization"."for_internal_metrics",
- "posthog_organization"."default_anonymize_ips",
- "posthog_organization"."is_pending_deletion",
- "posthog_organization"."customer_id",
- "posthog_organization"."available_product_features",
- "posthog_organization"."usage",
- "posthog_organization"."never_drop_data",
- "posthog_organization"."customer_trust_scores",
- "posthog_organization"."has_active_subscription",
- "posthog_organization"."setup_section_2_completed",
- "posthog_organization"."personalization",
- "posthog_organization"."domain_whitelist",
- "posthog_organization"."is_platform"
- FROM "posthog_organizationmembership"
- INNER JOIN "posthog_organization" ON ("posthog_organizationmembership"."organization_id" = "posthog_organization"."id")
- WHERE ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
- AND "posthog_organizationmembership"."user_id" = 99999)
- LIMIT 21
- '''
-# ---
-# name: TestFeatureFlag.test_creating_static_cohort.30
+# name: TestFeatureFlag.test_creating_static_cohort.29
'''
SELECT "posthog_experiment"."id",
"posthog_experiment"."name",
@@ -1771,7 +1739,30 @@
WHERE "posthog_experiment"."exposure_cohort_id" = 99999
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.31
+# name: TestFeatureFlag.test_creating_static_cohort.3
+ '''
+ SELECT "ee_accesscontrol"."id",
+ "ee_accesscontrol"."team_id",
+ "ee_accesscontrol"."access_level",
+ "ee_accesscontrol"."resource",
+ "ee_accesscontrol"."resource_id",
+ "ee_accesscontrol"."organization_member_id",
+ "ee_accesscontrol"."role_id",
+ "ee_accesscontrol"."created_by_id",
+ "ee_accesscontrol"."created_at",
+ "ee_accesscontrol"."updated_at"
+ FROM "ee_accesscontrol"
+ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id")
+ WHERE (("ee_accesscontrol"."organization_member_id" IS NULL
+ AND "ee_accesscontrol"."role_id" IS NULL
+ AND "ee_accesscontrol"."team_id" = 99999)
+ OR ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
+ AND "posthog_organizationmembership"."user_id" = 99999
+ AND "ee_accesscontrol"."role_id" IS NULL
+ AND "ee_accesscontrol"."team_id" = 99999))
+ '''
+# ---
+# name: TestFeatureFlag.test_creating_static_cohort.30
'''
/* user_id:0 celery:posthog.tasks.calculate_cohort.insert_cohort_from_feature_flag */
SELECT person_id
@@ -1782,7 +1773,7 @@
GROUP BY person_id
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.32
+# name: TestFeatureFlag.test_creating_static_cohort.31
'''
/* user_id:0 request:_snapshot_ */
SELECT persons.id AS id
@@ -1823,29 +1814,6 @@
'''
# ---
# name: TestFeatureFlag.test_creating_static_cohort.4
- '''
- SELECT "ee_accesscontrol"."id",
- "ee_accesscontrol"."team_id",
- "ee_accesscontrol"."access_level",
- "ee_accesscontrol"."resource",
- "ee_accesscontrol"."resource_id",
- "ee_accesscontrol"."organization_member_id",
- "ee_accesscontrol"."role_id",
- "ee_accesscontrol"."created_by_id",
- "ee_accesscontrol"."created_at",
- "ee_accesscontrol"."updated_at"
- FROM "ee_accesscontrol"
- LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id")
- WHERE (("ee_accesscontrol"."organization_member_id" IS NULL
- AND "ee_accesscontrol"."role_id" IS NULL
- AND "ee_accesscontrol"."team_id" = 99999)
- OR ("posthog_organizationmembership"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
- AND "posthog_organizationmembership"."user_id" = 99999
- AND "ee_accesscontrol"."role_id" IS NULL
- AND "ee_accesscontrol"."team_id" = 99999))
- '''
-# ---
-# name: TestFeatureFlag.test_creating_static_cohort.5
'''
SELECT "posthog_organizationmembership"."id",
"posthog_organizationmembership"."organization_id",
@@ -1898,7 +1866,7 @@
WHERE "posthog_organizationmembership"."user_id" = 99999
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.6
+# name: TestFeatureFlag.test_creating_static_cohort.5
'''
SELECT "posthog_featureflag"."id",
"posthog_featureflag"."key",
@@ -2010,7 +1978,7 @@
LIMIT 21
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.7
+# name: TestFeatureFlag.test_creating_static_cohort.6
'''
SELECT "posthog_earlyaccessfeature"."id",
"posthog_earlyaccessfeature"."team_id",
@@ -2080,7 +2048,7 @@
5 /* ... */)
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.8
+# name: TestFeatureFlag.test_creating_static_cohort.7
'''
SELECT "posthog_featureflagevaluationcontext"."id",
"posthog_featureflagevaluationcontext"."feature_flag_id",
@@ -2100,7 +2068,7 @@
5 /* ... */)
'''
# ---
-# name: TestFeatureFlag.test_creating_static_cohort.9
+# name: TestFeatureFlag.test_creating_static_cohort.8
'''
SELECT "posthog_taggeditem"."id",
"posthog_taggeditem"."tag_id",
@@ -2121,3 +2089,19 @@
5 /* ... */))
'''
# ---
+# name: TestFeatureFlag.test_creating_static_cohort.9
+ '''
+ SELECT "posthog_experiment"."id",
+ "posthog_experiment"."name",
+ "posthog_experiment"."feature_flag_id",
+ "posthog_experiment"."start_date",
+ "posthog_experiment"."end_date"
+ FROM "posthog_experiment"
+ WHERE (NOT "posthog_experiment"."deleted"
+ AND "posthog_experiment"."feature_flag_id" IN (1,
+ 2,
+ 3,
+ 4,
+ 5 /* ... */))
+ '''
+# ---
diff --git a/products/feature_flags/backend/api/test/test_feature_flag.py b/products/feature_flags/backend/api/test/test_feature_flag.py
index 9b3cf4cdc3bb..f6482ec09913 100644
--- a/products/feature_flags/backend/api/test/test_feature_flag.py
+++ b/products/feature_flags/backend/api/test/test_feature_flag.py
@@ -4673,7 +4673,7 @@ class TestFeatureFlag(APIBaseTest, ClickhouseTestMixin):
create_flag_targeting_a_new_cohort(0)
- with self.assertNumQueries(FuzzyInt(18, 19)):
+ with self.assertNumQueries(FuzzyInt(17, 18)):
response = self.client.get(f"/api/projects/{self.team.id}/feature_flags")
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -4681,7 +4681,7 @@ class TestFeatureFlag(APIBaseTest, ClickhouseTestMixin):
create_flag_targeting_a_new_cohort(i)
# Query count should stay constant regardless of flag and cohort count (no N+1)
- with self.assertNumQueries(FuzzyInt(18, 19)):
+ with self.assertNumQueries(FuzzyInt(17, 18)):
response = self.client.get(f"/api/projects/{self.team.id}/feature_flags")
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -4705,7 +4705,7 @@ class TestFeatureFlag(APIBaseTest, ClickhouseTestMixin):
name="Flag role access",
)
- with self.assertNumQueries(FuzzyInt(17, 18)):
+ with self.assertNumQueries(FuzzyInt(16, 17)):
response = self.client.get(f"/api/projects/{self.team.id}/feature_flags")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.json()["results"]), 2)
diff --git a/products/signals/backend/test/test_personal_ranking.py b/products/signals/backend/test/test_personal_ranking.py
index 08ce6046f8f7..2638c8265679 100644
--- a/products/signals/backend/test/test_personal_ranking.py
+++ b/products/signals/backend/test/test_personal_ranking.py
@@ -980,7 +980,7 @@ class TestForMeEndpoint(APIBaseTest):
self._claimed_report("Not in the stored list")
with (
patch.object(personal_ranking_serve, "gather_candidates") as gather,
- self.assertNumQueries(30),
+ self.assertNumQueries(26),
):
body = self._get(max_age_minutes=60)
assert (self._state(body), self.queued, gather.call_count) == ((True, True, False), [], 0)
diff --git a/products/surveys/backend/api/test/test_survey.py b/products/surveys/backend/api/test/test_survey.py
index 6bdfa9183daa..a99f1bc0cb4d 100644
--- a/products/surveys/backend/api/test/test_survey.py
+++ b/products/surveys/backend/api/test/test_survey.py
@@ -1603,7 +1603,7 @@ class TestSurvey(APIBaseTest):
).json()
# Includes one query for the project's replay gates
- with self.assertNumQueries(18):
+ with self.assertNumQueries(17):
response = self.client.get(f"/api/projects/{self.team.id}/feature_flags")
self.assertEqual(response.status_code, status.HTTP_200_OK)
result = response.json()