:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(data-warehouse): log access rule, table and join changes (#116593)
Daniel RCe6fdd8845d4d
diff --git a/frontend/src/layout/navigation-3000/sidepanel/panels/access_control/accessControlActivityDescriber.test.tsx b/frontend/src/layout/navigation-3000/sidepanel/panels/access_control/accessControlActivityDescriber.test.tsxnew file mode 100644index 000000000000..84c507ea8ae5--- /dev/null+++ b/frontend/src/layout/navigation-3000/sidepanel/panels/access_control/accessControlActivityDescriber.test.tsx@@ -0,0 +1,74 @@+import { render } from '@testing-library/react'++import { ActivityChange, ActivityLogItem } from 'lib/components/ActivityLog/humanizeActivity'++import { ActivityScope } from '~/types'++import { accessControlActivityDescriber } from './accessControlActivityDescriber'++describe('accessControlActivityDescriber', () => {+ const makeLogItem = (+ activity: string,+ context: Record<string, any>,+ changes: ActivityChange[] | null = null+ ): ActivityLogItem => ({+ user: { first_name: 'Grace', last_name: 'Hopper', email: '[email protected]' },+ activity,+ created_at: '2026-10-23T00:00:00Z',+ scope: ActivityScope.ACCESS_CONTROL,+ detail: {+ merge: null,+ trigger: null,+ changes,+ name: null,+ context: {+ resource: 'warehouse_table',+ resource_id: '0190a1b2',+ access_level: 'viewer',+ role_id: null,+ organization_member_id: null,+ ...context,+ },+ },+ })++ it.each([+ [+ 'a grant to a role',+ makeLogItem('created', { role_id: 'role-1' }),+ 'Grace Hopper set access for a role to viewer on warehouse table 0190a1b2',+ ],+ [+ 'a level change for a member',+ makeLogItem('updated', { organization_member_id: 'member-1', access_level: 'editor' }, [+ {+ type: ActivityScope.ACCESS_CONTROL,+ action: 'changed',+ field: 'access_level',+ before: 'viewer',+ after: 'editor',+ },+ ]),+ 'Grace Hopper changed access for a member from viewer to editor on warehouse table 0190a1b2',+ ],+ [+ 'a removed default for every object of a resource',+ makeLogItem('deleted', { resource: 'dashboard', resource_id: null, access_level: 'none' }),+ 'Grace Hopper removed the access rule for everyone on every dashboard',+ ],+ [+ 'a project rule',+ makeLogItem('created', {+ resource: 'project',+ resource_id: '2',+ access_level: 'admin',+ organization_member_id: 'member-1',+ }),+ 'Grace Hopper set access for a member to admin on this project',+ ],+ ])('describes %s', (_name, logItem, expected) => {+ const { description } = accessControlActivityDescriber(logItem)+ const { container } = render(<>{description}</>)+ expect(container.textContent).toEqual(expected)+ })+})diff --git a/frontend/src/layout/navigation-3000/sidepanel/panels/access_control/accessControlActivityDescriber.tsx b/frontend/src/layout/navigation-3000/sidepanel/panels/access_control/accessControlActivityDescriber.tsxnew file mode 100644index 000000000000..a0a3464087fc--- /dev/null+++ b/frontend/src/layout/navigation-3000/sidepanel/panels/access_control/accessControlActivityDescriber.tsx@@ -0,0 +1,72 @@+import {+ ActivityLogItem,+ ActivityLogUserName,+ HumanizedChange,+ activityLogSummary,+ defaultDescriber,+} from 'lib/components/ActivityLog/humanizeActivity'++// The backend stores ids only, because every activity-log reader sees these rows.+interface AccessControlActivityContext {+ resource: string+ resource_id: string | null+ access_level: string+ role_id: string | null+ organization_member_id: string | null+}++function subjectFor(context: AccessControlActivityContext): string {+ if (context.role_id) {+ return 'a role'+ }+ if (context.organization_member_id) {+ return 'a member'+ }+ return 'everyone'+}++function targetFor(context: AccessControlActivityContext): JSX.Element {+ const resource = context.resource.replace(/_/g, ' ')+ if (context.resource === 'project') {+ return <strong>this project</strong>+ }+ if (!context.resource_id) {+ return <strong>every {resource}</strong>+ }+ return (+ <strong>+ {resource} {context.resource_id}+ </strong>+ )+}++export function accessControlActivityDescriber(logItem: ActivityLogItem, asNotification?: boolean): HumanizedChange {+ const context = logItem.detail.context as AccessControlActivityContext | null | undefined+ if (!context?.resource) {+ return defaultDescriber(logItem, asNotification)+ }++ const subject = subjectFor(context)+ const target = targetFor(context)+ const levelChange = logItem.detail.changes?.find((change) => change.field === 'access_level')++ let action: string+ if (logItem.activity === 'created') {+ action = `set access for ${subject} to ${context.access_level} on`+ } else if (logItem.activity === 'deleted') {+ action = `removed the access rule for ${subject} on`+ } else if (levelChange) {+ action = `changed access for ${subject} from ${levelChange.before} to ${levelChange.after} on`+ } else {+ return defaultDescriber(logItem, asNotification, target)+ }++ return {+ summary: activityLogSummary(logItem, action, target),+ description: (+ <>+ <ActivityLogUserName logItem={logItem} /> {action} {target}+ </>+ ),+ }+}diff --git a/frontend/src/lib/components/ActivityLog/describers.tsx b/frontend/src/lib/components/ActivityLog/describers.tsxindex 2be8a99cfcc9..a1c6942f2459 100644--- a/frontend/src/lib/components/ActivityLog/describers.tsx+++ b/frontend/src/lib/components/ActivityLog/describers.tsx@@ -31,6 +31,7 @@ import {import { surveyActivityDescriber } from 'scenes/surveys/surveyActivityDescriber'import { teamActivityDescriber } from 'scenes/team-activity/teamActivityDescriber'+import { accessControlActivityDescriber } from '~/layout/navigation-3000/sidepanel/panels/access_control/accessControlActivityDescriber'import { ActivityScope } from '~/types'import { actionActivityDescriber } from 'products/actions/frontend/actionActivityDescriber'@@ -53,6 +54,8 @@ import { workflowActivityDescriber } from 'products/workflows/frontend/Workflowsexport const describerFor = (logItem?: ActivityLogItem): Describer | undefined => {switch (logItem?.scope) {+ case ActivityScope.ACCESS_CONTROL:+ return accessControlActivityDescribercase ActivityScope.ACTION:return actionActivityDescribercase ActivityScope.ALERT_CONFIGURATION:diff --git a/frontend/src/types.ts b/frontend/src/types.tsindex 8c44d396892a..c0bc5b3a944e 100644--- a/frontend/src/types.ts+++ b/frontend/src/types.ts@@ -6175,6 +6175,9 @@ export enum ActivityScope {ERROR_TRACKING_ISSUE = 'ErrorTrackingIssue',DATA_WAREHOUSE_EXPRESSION = 'DataWarehouseExpression',DATA_WAREHOUSE_SAVED_QUERY = 'DataWarehouseSavedQuery',+ DATA_WAREHOUSE_TABLE = 'DataWarehouseTable',+ DATA_WAREHOUSE_JOIN = 'DataWarehouseJoin',+ ACCESS_CONTROL = 'AccessControl',DATA_QUALITY_CHECK = 'DataQualityCheck',TAG = 'Tag',TAGGED_ITEM = 'TaggedItem',diff --git a/posthog/models/activity_logging/activity_log.py b/posthog/models/activity_logging/activity_log.pyindex 1d2565ba2919..f3f0f635455c 100644--- a/posthog/models/activity_logging/activity_log.py+++ b/posthog/models/activity_logging/activity_log.py@@ -138,6 +138,9 @@ ActivityScope = Literal["DataQualityCheck","DataQualityCheckSchedule","WarehouseSuggestion",+ "AccessControl",+ "DataWarehouseTable",+ "DataWarehouseJoin","Billing","Loop","StamphogRepoConfig",@@ -376,6 +379,15 @@ common_field_exclusions = [field_with_masked_contents: dict[AuditableScope, list[str]] = {+ # Any activity-log reader sees these rows, including readers denied the tables they name.+ "DataWarehouseJoin": [+ "source_table_name",+ "source_table_key",+ "joining_table_name",+ "joining_table_key",+ "field_name",+ "configuration",+ ],"SignalTeamConfig": ["onboarding"],"AccountView": ["name", "content", "text_content"],"WarehouseSuggestion": ["dismissal_note"],@@ -935,6 +947,10 @@ field_exclusions: dict[AuditableScope, list[str]] = {],"Project": ["id", "created_at"],"DataWarehouseExpression": ["deleted_at"],+ "DataWarehouseJoin": ["deleted_at"],+ # The subject is the identity of a rule and never changes. The receiver names it in the+ # context, so the diff does not load the role and the membership for both sides.+ "AccessControl": ["organization_member", "role"],"DataWarehouseSavedQuery": ["name","columns",@@ -1570,6 +1586,7 @@ def log_activity_with_soft_delete(user: "User | None",was_impersonated: bool,name: str | None = None,+ organization_id: UUID | None = None,) -> None:instance = current or previousif instance is None:@@ -1581,7 +1598,7 @@ def log_activity_with_soft_delete(if deleted_change:activity = "deleted" if deleted_change.after else "restored"log_activity(- organization_id=None,+ organization_id=organization_id,team_id=instance.serializable_value("team"),user=user,was_impersonated=was_impersonated,diff --git a/posthog/models/organization_invite.py b/posthog/models/organization_invite.pyindex 81bfb1ffb25e..1ac6a658239a 100644--- a/posthog/models/organization_invite.py+++ b/posthog/models/organization_invite.py@@ -13,7 +13,7 @@ from rest_framework import exceptionsfrom posthog.constants import INVITE_DAYS_VALIDITYfrom posthog.email import is_email_availablefrom posthog.helpers.email_utils import EmailNormalizer, EmailValidationHelper-from posthog.models.activity_logging.model_activity import ModelActivityMixin+from posthog.models.activity_logging.model_activity import ActingUserContext, ModelActivityMixinfrom posthog.models.onboarding_delegation import mark_delegators_acceptedfrom posthog.models.organization import OrganizationMembershipfrom posthog.models.team import Team@@ -191,13 +191,15 @@ class OrganizationInvite(ModelActivityMixin, UUIDTModel):# If the team doesn't exist, it was probably deleted. We can still continue with the invite.continue- AccessControl.objects.create(- team=team,- resource="project",- resource_id=str(team.id),- organization_member=parent_membership,- access_level=item["level"],- )+ # The inviter chose this access, so the audit row names them rather than the invitee.+ with ActingUserContext(self.created_by):+ AccessControl.objects.create(+ team=team,+ resource="project",+ resource_id=str(team.id),+ organization_member=parent_membership,+ access_level=item["level"],+ )# Sibling-invite sweep: clean up any other pending invites for the same email in this# org so the invitee doesn't accumulate stale rows. Other DELEGATION invites havediff --git a/posthog/models/test/test_organization_invite_model.py b/posthog/models/test/test_organization_invite_model.pyindex 0d269ee9f21e..c0bb0f11b836 100644--- a/posthog/models/test/test_organization_invite_model.py+++ b/posthog/models/test/test_organization_invite_model.py@@ -6,6 +6,7 @@ from unittest.mock import patchfrom django.utils import timezonefrom posthog.models import OrganizationInvite+from posthog.models.activity_logging.activity_log import ActivityLogfrom posthog.models.organization import OrganizationMembershipfrom posthog.models.team.team import Teamfrom posthog.models.user import User@@ -42,6 +43,7 @@ class TestOrganizationInvite(BaseTest):organization=self.organization,target_email="[email protected]",private_project_access=[{"id": team.id, "level": "admin"}],+ created_by=self.user,)# Use the invite@@ -59,6 +61,8 @@ class TestOrganizationInvite(BaseTest):raise Exception("Access control not found")self.assertEqual(access_control.access_level, "admin")+ grant_log = ActivityLog.objects.get(scope="AccessControl", item_id=str(access_control.id))+ self.assertEqual((grant_log.activity, grant_log.user), ("created", self.user))# Verify the invite has been deletedself.assertFalse(OrganizationInvite.objects.filter(target_email="[email protected]").exists())diff --git a/posthog/test/repo_invariants/setup_receivers_baseline.txt b/posthog/test/repo_invariants/setup_receivers_baseline.txtindex e40c097b22f6..a04e4b1be97e 100644--- a/posthog/test/repo_invariants/setup_receivers_baseline.txt+++ b/posthog/test/repo_invariants/setup_receivers_baseline.txt@@ -17,6 +17,7 @@ model_activity:posthog.models.activity_logging.signal_handlers.handle_project_semodel_activity:posthog.models.activity_logging.signal_handlers.handle_tag_changemodel_activity:posthog.models.activity_logging.signal_handlers.handle_tagged_item_changemodel_activity:posthog.models.activity_logging.signal_handlers.log_user_change_activity+model_activity:products.access_control.backend.activity_logging.handle_access_control_changemodel_activity:products.actions.backend.activity_logging.handle_action_changemodel_activity:products.ai_observability.backend.activity_logging.handle_evaluation_changemodel_activity:products.ai_observability.backend.activity_logging.handle_evaluation_directory_change@@ -36,6 +37,7 @@ model_activity:products.dashboards.backend.activity_logging.handle_dashboard_widmodel_activity:products.data_catalog.backend.activity_logging.handle_metric_activitymodel_activity:products.data_quality.backend.activity_logging.handle_data_quality_check_activitymodel_activity:products.data_tools.backend.activity_logging.handle_expression_activity+model_activity:products.data_tools.backend.activity_logging.handle_join_activitymodel_activity:products.experiments.backend.activity_logging.handle_experiment_changemodel_activity:products.experiments.backend.activity_logging.handle_team_experiments_config_changemodel_activity:products.feature_flags.backend.activity_logging.handle_feature_flag_changediff --git a/products/access_control/backend/activity_logging.py b/products/access_control/backend/activity_logging.pynew file mode 100644index 000000000000..14d50c446edc--- /dev/null+++ b/products/access_control/backend/activity_logging.py@@ -0,0 +1,74 @@+"""Activity-log receiver for access control rules.++``ModelActivityMixin`` emits ``model_activity_signal`` when a rule is saved or deleted. This+persists who granted, changed, or removed access. Registered from ``apps.ready()``.+"""++import dataclasses+from typing import Any, Optional++from posthog.models import User+from posthog.models.activity_logging.activity_log import (+ ActivityContextBase,+ Change,+ Detail,+ changes_between,+ log_activity,+)+from posthog.models.signals import model_activity_signal, mutable_receiver++from products.access_control.backend.models.access_control import AccessControl++[email protected](frozen=True)+class AccessControlContext(ActivityContextBase):+ resource: str+ resource_id: str | None+ access_level: str+ role_id: str | None+ organization_member_id: str | None+++@mutable_receiver(model_activity_signal, sender=AccessControl)+def handle_access_control_change(+ sender: type,+ scope: str,+ before_update: Optional[AccessControl],+ after_update: Optional[AccessControl],+ activity: str,+ user: Optional[User],+ was_impersonated: bool = False,+ **kwargs: Any,+) -> None:+ rule = after_update or before_update+ if rule is None:+ return++ if activity == "created":+ changes = [Change(type="AccessControl", field="access_level", action="created", after=rule.access_level)]+ elif activity == "deleted":+ changes = [Change(type="AccessControl", field="access_level", action="deleted", before=rule.access_level)]+ else:+ changes = changes_between("AccessControl", previous=before_update, current=after_update)++ log_activity(+ organization_id=rule.team.organization_id,+ team_id=rule.team_id,+ user=user,+ was_impersonated=was_impersonated,+ item_id=rule.id,+ scope=scope,+ activity=activity,+ detail=Detail(+ name=f"{rule.resource} {rule.resource_id}" if rule.resource_id else rule.resource,+ changes=changes,+ context=AccessControlContext(+ resource=rule.resource,+ resource_id=rule.resource_id,+ access_level=rule.access_level,+ # Ids only: any activity-log reader sees this row, so it carries no names or emails.+ role_id=str(rule.role_id) if rule.role_id else None,+ organization_member_id=str(rule.organization_member_id) if rule.organization_member_id else None,+ ),+ ),+ )diff --git a/products/access_control/backend/apps.py b/products/access_control/backend/apps.pyindex 6e5a3a6c7c2e..dd77ba125329 100644--- a/products/access_control/backend/apps.py+++ b/products/access_control/backend/apps.py@@ -12,4 +12,9 @@ class AccessControlConfig(AppConfig):# wired as a side effect of the HogQL printer importing this module at django.setup();# that import is now deferred to compile time, so the wiring must happen here or the# receivers connect in no process and cache invalidation silently stops.- from products.access_control.backend import property_access_control # noqa: F401, PLC0415+ # Rules are written from the API and from product code in workers, so the audit+ # receiver must connect in every process.+ from products.access_control.backend import (+ activity_logging, # noqa: F401, PLC0415+ property_access_control, # noqa: F401, PLC0415+ )diff --git a/products/access_control/backend/facade/api.py b/products/access_control/backend/facade/api.pyindex 4ac98417474e..ad9a7fd8c511 100644--- a/products/access_control/backend/facade/api.py+++ b/products/access_control/backend/facade/api.py@@ -610,13 +610,15 @@ def set_object_access_control(team = Team.objects.only("id", "organization_id").get(id=team_id)resource = _validate_object_access_control(organization_id=team.organization_id, input=input)if input.access_level is None:- AccessControl.objects.filter(+ # Deleted one by one because a queryset delete writes no activity log row.+ for rule in AccessControl.objects.filter(team_id=team_id,resource=resource,resource_id=input.resource_id,organization_member_id=input.organization_member_id,role_id=cast(UUID | str, input.role_id),- ).delete()+ ):+ rule.delete()return Nonerule, _created = AccessControl.objects.update_or_create(diff --git a/products/access_control/backend/models/access_control.py b/products/access_control/backend/models/access_control.pyindex 0abc24433bdb..991586a9dd34 100644--- a/products/access_control/backend/models/access_control.py+++ b/products/access_control/backend/models/access_control.py@@ -1,9 +1,13 @@from django.db import models+from posthog.models.activity_logging.model_activity import ModelActivityMixinfrom posthog.models.utils import UUIDTModel-class AccessControl(UUIDTModel):+# nosemgrep: no-new-uuidt-models -- an existing model, and a new primary key default would need a migration.+class AccessControl(ModelActivityMixin, UUIDTModel):+ activity_logging_on_delete = True+class Meta:app_label = "ee"constraints = [diff --git a/products/access_control/backend/tests/test_access_control.py b/products/access_control/backend/tests/test_access_control.pyindex 1b4b3ad947fe..9428bbea0de4 100644--- a/products/access_control/backend/tests/test_access_control.py+++ b/products/access_control/backend/tests/test_access_control.py@@ -14,6 +14,7 @@ from rest_framework import statusfrom posthog.constants import AvailableFeaturefrom posthog.models import PropertyDefinition+from posthog.models.activity_logging.activity_log import ActivityLogfrom posthog.models.organization import Organization, OrganizationMembershipfrom posthog.models.personal_api_key import PersonalAPIKeyfrom posthog.models.tagged_item_registry import content_type_for@@ -100,6 +101,28 @@ class TestAccessControlProjectLevelAPI(BaseAccessControlTest):res = self._put_project_access_control({"access_level": None})assert res.status_code == status.HTTP_204_NO_CONTENT+ def test_rule_changes_are_written_to_the_activity_log(self):+ self._org_membership(OrganizationMembership.Level.OWNER)+ subject = {"organization_member": str(self.organization_membership.id)}+ for level in ("member", "admin", None):+ self._put_project_access_control({**subject, "access_level": level})++ logs = list(ActivityLog.objects.filter(scope="AccessControl").order_by("created_at"))+ details = [log.detail or {} for log in logs]+ assert [(log.activity, detail["changes"][0]["after"]) for log, detail in zip(logs, details)] == [+ ("created", "member"),+ ("updated", "admin"),+ ("deleted", None),+ ]+ assert {log.user for log in logs} == {self.user}+ assert details[0]["context"] == {+ "resource": "project",+ "resource_id": str(self.team.id),+ "access_level": "member",+ "role_id": None,+ "organization_member_id": str(self.organization_membership.id),+ }+def test_project_change_if_in_access_control(self):self._org_membership(OrganizationMembership.Level.ADMIN)# Add ourselves to accessdiff --git a/products/access_control/backend/tests/test_api.py b/products/access_control/backend/tests/test_api.pyindex be4a69d5ce3e..7b8009a0e1f8 100644--- a/products/access_control/backend/tests/test_api.py+++ b/products/access_control/backend/tests/test_api.py@@ -5,6 +5,7 @@ from parameterized import parameterizedfrom posthog.constants import AvailableFeaturefrom posthog.models import User+from posthog.models.activity_logging.activity_log import ActivityLogfrom posthog.models.organization import Organization, OrganizationMembershipfrom products.access_control.backend.facade import contracts@@ -67,6 +68,9 @@ class TestSetObjectAccessControl(BaseTest):assert self._other_user_level() == "none"assert self._grant(None) is None+ logged = ActivityLog.objects.filter(scope="AccessControl", item_id=str(rule.id)).order_by("created_at")+ assert [log.activity for log in logged] == ["created", "updated", "deleted"]+def test_role_rule_grants_role_members(self) -> None:role = Role.objects.create(name="Sales", organization=self.organization)RoleMembership.objects.create(user=self.other_user, role=role)diff --git a/products/data_tools/backend/activity_logging.py b/products/data_tools/backend/activity_logging.pyindex f4c709f8e5b8..96e2e5ca6f23 100644--- a/products/data_tools/backend/activity_logging.py+++ b/products/data_tools/backend/activity_logging.py@@ -1,6 +1,6 @@-"""Activity-log receiver for warehouse expressions.+"""Activity-log receivers for warehouse expressions and joins.-``ModelActivityMixin`` emits ``model_activity_signal`` on every expression save; this persists the+``ModelActivityMixin`` emits ``model_activity_signal`` on every save; these persist theaudit trail (create, edit, soft delete, restore). Registered from ``apps.ready()``."""@@ -11,6 +11,7 @@ from posthog.models.activity_logging.activity_log import log_activity_with_soft_from posthog.models.signals import model_activity_signal, mutable_receiverfrom .models.expression import DataWarehouseExpression+from .models.join import DataWarehouseJoin@mutable_receiver(model_activity_signal, sender=DataWarehouseExpression)@@ -37,3 +38,31 @@ def handle_expression_activity(activity=activity,name=f"{instance.table_name}.{instance.field_name}",)+++@mutable_receiver(model_activity_signal, sender=DataWarehouseJoin)+def handle_join_activity(+ sender: type,+ scope: str,+ before_update: Optional[DataWarehouseJoin],+ after_update: Optional[DataWarehouseJoin],+ activity: str,+ user: Optional[User],+ was_impersonated: bool = False,+ **kwargs: Any,+) -> None:+ join = after_update or before_update+ if join is None:+ return++ log_activity_with_soft_delete(+ previous=before_update,+ current=after_update,+ user=user,+ was_impersonated=was_impersonated,+ scope=scope,+ activity=activity,+ name=f"{join.source_table_name}.{join.field_name}",+ # The org-wide activity route filters on organization_id.+ organization_id=join.team.organization_id,+ )diff --git a/products/data_tools/backend/models/join.py b/products/data_tools/backend/models/join.pyindex 8af40e4bb039..be7bbf756312 100644--- a/products/data_tools/backend/models/join.py+++ b/products/data_tools/backend/models/join.py@@ -4,6 +4,7 @@ from warnings import warnfrom django.db import modelsfrom django.utils import timezone+from posthog.models.activity_logging.model_activity import ModelActivityMixinfrom posthog.models.utils import CreatedMetaFields, DeletedMetaFields, UUIDTModel@@ -30,7 +31,11 @@ class DataWarehouseViewLink(CreatedMetaFields, UUIDTModel, DeletedMetaFields):to_join_key = models.CharField(max_length=400)-class DataWarehouseJoin(CreatedMetaFields, UUIDTModel, DeletedMetaFields):+# nosemgrep: no-new-uuidt-models -- an existing model, and a new primary key default would need a migration.+class DataWarehouseJoin(ModelActivityMixin, CreatedMetaFields, UUIDTModel, DeletedMetaFields):+ # The join API hard deletes. Other paths soft delete through save().+ activity_logging_on_delete = True+team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, related_name="+")source_table_name = models.CharField(max_length=400)source_table_key = models.CharField(max_length=400)diff --git a/products/data_warehouse/backend/direct_postgres.py b/products/data_warehouse/backend/direct_postgres.pyindex 922f5b28729e..ef8858e565ac 100644--- a/products/data_warehouse/backend/direct_postgres.py+++ b/products/data_warehouse/backend/direct_postgres.py@@ -100,5 +100,8 @@ def rename_direct_postgres_join_references(*, team_id: int, old_name: str, new_nreturnfrom products.data_tools.backend.models.join import DataWarehouseJoin- DataWarehouseJoin.objects.filter(team_id=team_id, source_table_name=old_name).update(source_table_name=new_name)- DataWarehouseJoin.objects.filter(team_id=team_id, joining_table_name=old_name).update(joining_table_name=new_name)+ # Saved one by one because a queryset update writes no activity log row.+ for field in ("source_table_name", "joining_table_name"):+ for join in DataWarehouseJoin.objects.filter(team_id=team_id, **{field: old_name}):+ setattr(join, field, new_name)+ join.save(update_fields=[field])diff --git a/products/data_warehouse/backend/presentation/table_activity.py b/products/data_warehouse/backend/presentation/table_activity.pynew file mode 100644index 000000000000..39eca845812b--- /dev/null+++ b/products/data_warehouse/backend/presentation/table_activity.py@@ -0,0 +1,70 @@+"""Activity log rows for the edits a person makes to a warehouse table.++The table model does not carry ``ModelActivityMixin``. Every sync saves the table, and the mixin+reads the row before each save through a manager that adds two joins and a prefetch. The API+write paths call ``log_table_activity`` instead.+"""++from typing import TYPE_CHECKING, Any, Literal++from posthog.models.activity_logging.activity_log import Change, Detail, dict_changes_between, log_activity+from posthog.models.activity_logging.model_activity import get_current_user, get_was_impersonated++if TYPE_CHECKING:+ from products.warehouse_sources.backend.facade.models import DataWarehouseTable+++def snapshot_table(table: "DataWarehouseTable") -> dict[str, Any]:+ """The user-editable state of a table, taken before an edit so the log can show the diff."""+ return {+ "name": table.name,+ "format": table.format,+ "url_pattern": table.url_pattern,+ "deleted": bool(table.deleted),+ "options": dict(table.options or {}),+ # A background task rewrites the `valid` flag of each column, so only the type is compared.+ "columns": {+ name: column.get("clickhouse") if isinstance(column, dict) else column+ for name, column in (table.columns or {}).items()+ },+ }+++def log_table_activity(+ table: "DataWarehouseTable",+ activity: Literal["created", "updated", "deleted"],+ *,+ before: dict[str, Any] | None = None,+ credential_changed: bool = False,+ force_save: bool = False,+) -> None:+ diff = dict_changes_between("DataWarehouseTable", before, snapshot_table(table)) if before is not None else []+ if any(change.field == "deleted" and change.after for change in diff):+ activity = "deleted"+ # Any activity-log reader sees this row, including readers denied the table, so it records+ # which fields changed and never their values.+ changes = [+ Change(+ type="DataWarehouseTable",+ field=change.field,+ action=change.action,+ before="masked" if change.before is not None else None,+ after="masked" if change.after is not None else None,+ )+ for change in diff+ if change.field != "deleted"+ ]+ if credential_changed:+ changes.append(Change(type="DataWarehouseTable", field="credential", action="changed"))++ log_activity(+ organization_id=table.team.organization_id,+ team_id=table.team_id,+ user=get_current_user(),+ was_impersonated=get_was_impersonated(),+ item_id=table.id,+ scope="DataWarehouseTable",+ activity=activity,+ detail=Detail(name=table.name, changes=changes),+ force_save=force_save,+ )diff --git a/products/data_warehouse/backend/presentation/views/table.py b/products/data_warehouse/backend/presentation/views/table.pyindex 97d9ce2aef64..2fd76a80e45c 100644--- a/products/data_warehouse/backend/presentation/views/table.py+++ b/products/data_warehouse/backend/presentation/views/table.py@@ -34,6 +34,7 @@ from products.access_control.backend.presentation.access_control import (UserAccessControlSerializerMixin,)from products.data_warehouse.backend.facade.api import get_s3_client+from products.data_warehouse.backend.presentation.table_activity import log_table_activity, snapshot_tablefrom products.warehouse_sources.backend.facade.api import (FILE_FORMAT_READ_HINTS,FILE_FORMAT_TO_TABLE_FORMAT,@@ -423,6 +424,7 @@ class TableSerializer(UserAccessControlSerializerMixin, serializers.ModelSerialitable.save()except Exception as err:raise serializers.ValidationError(str(err))+ log_table_activity(table, "created")validate_data_warehouse_table_columns.delay(self.context["team_id"], str(table.id))@@ -624,6 +626,7 @@ class TableViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.M)instance.soft_delete()+ log_table_activity(instance, "deleted")_delete_hosted_upload_file(instance)return response.Response(status=status.HTTP_204_NO_CONTENT)@@ -631,6 +634,7 @@ class TableViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.Mdef perform_update(self, serializer):instance = serializer.instancevalidated_data = serializer.validated_data+ before = snapshot_table(instance)credential_data = validated_data.pop("credential", None)if credential_data:@@ -671,6 +675,7 @@ class TableViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.Minstance.save()except Exception as err:raise serializers.ValidationError(str(err))+ log_table_activity(instance, "updated", before=before, credential_changed=bool(credential_data))@action(methods=["POST"], detail=True)def update_schema(self, request: request.Request, *args: Any, **kwargs: Any) -> response.Response:@@ -685,6 +690,7 @@ class TableViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.Mstatus=status.HTTP_400_BAD_REQUEST, data={"message": "The table must be a manually linked table"})+ before = snapshot_table(table)columns = table.columns or {}column_keys = list(columns.keys())for key in updates.keys():@@ -720,6 +726,7 @@ class TableViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.Mtable.columns = columnstable.save()+ log_table_activity(table, "updated", before=before)return response.Response(status=status.HTTP_200_OK)@@ -738,6 +745,7 @@ class TableViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.M@action(methods=["POST"], detail=True, required_scopes=["warehouse_table:write"])def refresh_schema(self, request: request.Request, *args: Any, **kwargs: Any) -> response.Response:table: DataWarehouseTable = self.get_object()+ before = snapshot_table(table)# get_columns surfaces read failures as a plain Exception carrying a user-facing message.# Without this it reaches the handler as a 500, the same way TableSerializer.create avoids.@@ -746,6 +754,7 @@ class TableViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.Mexcept Exception as err:raise serializers.ValidationError(str(err))table.save()+ log_table_activity(table, "updated", before=before)return response.Response(status=status.HTTP_200_OK)@@ -922,6 +931,7 @@ class TableViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.M)table.save()+ log_table_activity(table, "created")if file_upload_sources_enabled(self.team_id):# The table reads the file in place until the source's first sync imports it.create_file_upload_source(self.team_id, table.pk, upload, read_settings)@@ -1112,6 +1122,7 @@ class TableViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.Mreturn self._replace_file_from_legacy_upload(table, file, safe_filename, file_format)created_table = table is None+ before = snapshot_table(table) if table is not None else None# Create the table recordtry:@@ -1169,6 +1180,8 @@ class TableViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.M# the URL above is always scoped to this team's own managed/ prefix, never taken# verbatim from request input the way the PATCH endpoint's url_pattern field is.table.save(internally_computed_url_pattern=True)+ # A re-upload replaces the data even when no metadata changes, so it is always logged.+ log_table_activity(table, "created" if created_table else "updated", before=before, force_save=True)# Validate columns in backgroundvalidate_data_warehouse_table_columns.delay(team_id, str(table.id))diff --git a/products/data_warehouse/backend/tests/api/test_table.py b/products/data_warehouse/backend/tests/api/test_table.pyindex 5fe460747319..1caec0c37956 100644--- a/products/data_warehouse/backend/tests/api/test_table.py+++ b/products/data_warehouse/backend/tests/api/test_table.py@@ -14,6 +14,8 @@ from parameterized import parameterizedfrom rest_framework.exceptions import ValidationErrorfrom rest_framework.test import APIRequestFactory+from posthog.models.activity_logging.activity_log import ActivityLog+from products.data_warehouse.backend.direct_postgres import DIRECT_POSTGRES_URL_PATTERNfrom products.data_warehouse.backend.presentation.views.table import (CreateTableFromUploadSerializer,@@ -336,6 +338,19 @@ class TestTable(APIBaseTest):assert columns is not Noneassert columns["id"] == {"clickhouse": "Nullable(Float64)", "hogql": "FloatDatabaseField", "valid": True}+ log = ActivityLog.objects.get(scope="DataWarehouseTable", item_id=str(table.id))+ assert (log.activity, log.user) == ("updated", self.user)+ assert log.detail is not None+ assert log.detail["changes"] == [+ {+ "type": "DataWarehouseTable",+ "field": "columns",+ "action": "changed",+ "before": "masked",+ "after": "masked",+ }+ ]+def test_update_schema_200_no_updates(self):columns = {"id": {"clickhouse": "Nullable(Int64)", "hogql": "IntegerDatabaseField"}}table = DataWarehouseTable.objects.create(@@ -472,17 +487,22 @@ class TestTable(APIBaseTest):assert response.status_code == 400assert DataWarehouseTable.objects.count() == 1- def test_delete_table(self):+ @parameterized.expand([("delete_endpoint", "delete", 204), ("patch_deleted_flag", "patch", 200)])+ def test_delete_table(self, _name: str, method: str, expected_status: int):table = DataWarehouseTable.objects.create(name="test_table", format="Parquet", team=self.team, team_id=self.team.pk, columns={})- response = self.client.delete(f"/api/projects/{self.team.pk}/warehouse_tables/{table.id}")+ url = f"/api/projects/{self.team.pk}/warehouse_tables/{table.id}"+ response = self.client.delete(url) if method == "delete" else self.client.patch(url, {"deleted": True})- assert response.status_code == 204+ assert response.status_code == expected_statustable.refresh_from_db()assert table.deleted is True+ assert ActivityLog.objects.filter(+ scope="DataWarehouseTable", item_id=str(table.id), activity="deleted", user=self.user+ ).exists()def test_delete_table_with_source(self):source = ExternalDataSource.objects.create(team=self.team, team_id=self.team.pk)@@ -1170,13 +1190,15 @@ class TestTable(APIBaseTest):DATAWAREHOUSE_BUCKET_DOMAIN="test-bucket.s3.amazonaws.com",DATAWAREHOUSE_BUCKET="test-warehouse-bucket",):- response = self.client.post(- f"/api/projects/{self.team.id}/warehouse_tables/file/",- {"file": test_file, "name": "existing_table", "format": "CSVWithNames"},- format="multipart",- )-- assert response.status_code == 201+ # The second upload changes no metadata and must still be logged.+ for _ in range(2):+ test_file.seek(0)+ response = self.client.post(+ f"/api/projects/{self.team.id}/warehouse_tables/file/",+ {"file": test_file, "name": "existing_table", "format": "CSVWithNames"},+ format="multipart",+ )+ assert response.status_code == 201# Verify the table was updatedexisting_table.refresh_from_db()@@ -1193,10 +1215,17 @@ class TestTable(APIBaseTest):}# Verify S3 client was called to upload the file- mock_s3.upload_fileobj.assert_called_once_with(+ mock_s3.upload_fileobj.assert_called_with(ANY, "test-warehouse-bucket", f"managed/team_{self.team.id}/updated_file.csv")+ assert (+ ActivityLog.objects.filter(+ scope="DataWarehouseTable", item_id=str(existing_table.id), activity="updated", user=self.user+ ).count()+ == 2+ )+@parameterized.expand([("no_url", "", False, False),diff --git a/products/data_warehouse/backend/tests/api/test_view_link.py b/products/data_warehouse/backend/tests/api/test_view_link.pyindex 99a560971a99..e2c522e0e257 100644--- a/products/data_warehouse/backend/tests/api/test_view_link.py+++ b/products/data_warehouse/backend/tests/api/test_view_link.py@@ -11,10 +11,12 @@ from posthog.schema import HogQLQueryResponsefrom posthog.hogql.database.database import Databasefrom posthog.hogql.query import HogQLQueryExecutor+from posthog.models.activity_logging.activity_log import ActivityLogfrom posthog.models.team.extensions import get_or_create_team_extensionfrom posthog.models.team.team_revenue_analytics_config import TeamRevenueAnalyticsConfigfrom products.data_tools.backend.models.join import DataWarehouseJoin+from products.data_warehouse.backend.direct_postgres import rename_direct_postgres_join_referencesfrom products.data_warehouse.backend.presentation.views.view_link import NO_MATCHING_ROWS_MSGfrom products.warehouse_sources.backend.facade.models import (DataWarehouseCredential,@@ -247,6 +249,36 @@ class TestViewLinkQuery(APIBaseTest):self.assertEqual(DataWarehouseJoin.objects.all().count(), 0)+ logs = ActivityLog.objects.filter(scope="DataWarehouseJoin", item_id=view_link["id"]).order_by("created_at")+ assert [(log.activity, log.user, (log.detail or {}).get("name")) for log in logs] == [+ ("created", self.user, "events.some_field"),+ ("deleted", self.user, "events.some_field"),+ ]++ def test_table_rename_is_written_to_the_activity_log_of_each_join(self):+ join = DataWarehouseJoin.objects.create(+ team=self.team,+ source_table_name="events",+ source_table_key="uuid",+ joining_table_name="old_name",+ joining_table_key="id",+ field_name="some_field",+ )++ rename_direct_postgres_join_references(team_id=self.team.id, old_name="old_name", new_name="new_name")++ log = ActivityLog.objects.get(scope="DataWarehouseJoin", item_id=str(join.id), activity="updated")+ assert log.detail is not None+ assert log.detail["changes"] == [+ {+ "type": "DataWarehouseJoin",+ "field": "joining_table_name",+ "action": "changed",+ "before": "masked",+ "after": "masked",+ }+ ]+def test_list_empty_does_not_build_database(self):# An empty list must not eagerly build the full HogQL database, which is a heavy# chain of serial Postgres queries that previously ran on every single request.diff --git a/products/platform_features/frontend/generated/api.schemas.ts b/products/platform_features/frontend/generated/api.schemas.tsindex f58f809ba9e0..584b6c35d89e 100644--- a/products/platform_features/frontend/generated/api.schemas.ts+++ b/products/platform_features/frontend/generated/api.schemas.ts@@ -1483,6 +1483,9 @@ export type ActivityLogListParams = {* * `DataQualityCheck` - DataQualityCheck* * `DataQualityCheckSchedule` - DataQualityCheckSchedule* * `WarehouseSuggestion` - WarehouseSuggestion+ * * `AccessControl` - AccessControl+ * * `DataWarehouseTable` - DataWarehouseTable+ * * `DataWarehouseJoin` - DataWarehouseJoin* * `Billing` - Billing* * `Loop` - Loop* * `StamphogRepoConfig` - StamphogRepoConfig@@ -1596,6 +1599,9 @@ export const ActivityLogListScope = {DataQualityCheck: 'DataQualityCheck',DataQualityCheckSchedule: 'DataQualityCheckSchedule',WarehouseSuggestion: 'WarehouseSuggestion',+ AccessControl: 'AccessControl',+ DataWarehouseTable: 'DataWarehouseTable',+ DataWarehouseJoin: 'DataWarehouseJoin',Billing: 'Billing',Loop: 'Loop',StamphogRepoConfig: 'StamphogRepoConfig',@@ -1696,6 +1702,9 @@ export const ActivityLogListScope = {* * `DataQualityCheck` - DataQualityCheck* * `DataQualityCheckSchedule` - DataQualityCheckSchedule* * `WarehouseSuggestion` - WarehouseSuggestion+ * * `AccessControl` - AccessControl+ * * `DataWarehouseTable` - DataWarehouseTable+ * * `DataWarehouseJoin` - DataWarehouseJoin* * `Billing` - Billing* * `Loop` - Loop* * `StamphogRepoConfig` - StamphogRepoConfig@@ -1797,6 +1806,9 @@ export const ActivityLogListScopesItem = {DataQualityCheck: 'DataQualityCheck',DataQualityCheckSchedule: 'DataQualityCheckSchedule',WarehouseSuggestion: 'WarehouseSuggestion',+ AccessControl: 'AccessControl',+ DataWarehouseTable: 'DataWarehouseTable',+ DataWarehouseJoin: 'DataWarehouseJoin',Billing: 'Billing',Loop: 'Loop',StamphogRepoConfig: 'StamphogRepoConfig',diff --git a/products/warehouse_sources/backend/tests/api/test_external_data_schema.py b/products/warehouse_sources/backend/tests/api/test_external_data_schema.pyindex 03f3c842952b..6f2da38fdba0 100644--- a/products/warehouse_sources/backend/tests/api/test_external_data_schema.py+++ b/products/warehouse_sources/backend/tests/api/test_external_data_schema.py@@ -4844,6 +4844,27 @@ class TestExternalDataSchemaRowFilters(APIBaseTest):schema.refresh_from_db()assert schema.row_filters == filters+ @parameterized.expand(+ [+ ("enabled_columns", ["id", "name"]),+ ("row_filters", [{"column": "id", "operator": ">", "value": 10}]),+ ]+ )+ def test_selection_change_is_written_to_the_activity_log(self, field: str, value: Any):+ schema = self._create()++ response = self.client.patch(+ f"/api/environments/{self.team.pk}/external_data_schemas/{schema.id}", data={field: value}+ )+ assert response.status_code == 200, response.json()++ log = ActivityLog.objects.get(scope="ExternalDataSchema", item_id=str(schema.id), activity="updated")+ assert log.user == self.user+ assert log.detail is not None+ assert [c for c in log.detail["changes"] if c["field"] == field] == [+ {"type": "ExternalDataSchema", "field": field, "action": "created", "before": None, "after": value}+ ]+def test_null_clears_row_filters(self):schema = self._create()schema.row_filters = [{"column": "id", "operator": ">", "value": 1}]diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 5049f5902508..c87501a84852 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -124847,6 +124847,9 @@ export namespace Schemas {* * `DataQualityCheck` - DataQualityCheck* * `DataQualityCheckSchedule` - DataQualityCheckSchedule* * `WarehouseSuggestion` - WarehouseSuggestion+ * * `AccessControl` - AccessControl+ * * `DataWarehouseTable` - DataWarehouseTable+ * * `DataWarehouseJoin` - DataWarehouseJoin* * `Billing` - Billing* * `Loop` - Loop* * `StamphogRepoConfig` - StamphogRepoConfig@@ -124961,6 +124964,9 @@ export namespace Schemas {DataQualityCheck: 'DataQualityCheck',DataQualityCheckSchedule: 'DataQualityCheckSchedule',WarehouseSuggestion: 'WarehouseSuggestion',+ AccessControl: 'AccessControl',+ DataWarehouseTable: 'DataWarehouseTable',+ DataWarehouseJoin: 'DataWarehouseJoin',Billing: 'Billing',Loop: 'Loop',StamphogRepoConfig: 'StamphogRepoConfig',@@ -125061,6 +125067,9 @@ export namespace Schemas {* * `DataQualityCheck` - DataQualityCheck* * `DataQualityCheckSchedule` - DataQualityCheckSchedule* * `WarehouseSuggestion` - WarehouseSuggestion+ * * `AccessControl` - AccessControl+ * * `DataWarehouseTable` - DataWarehouseTable+ * * `DataWarehouseJoin` - DataWarehouseJoin* * `Billing` - Billing* * `Loop` - Loop* * `StamphogRepoConfig` - StamphogRepoConfig@@ -125163,6 +125172,9 @@ export namespace Schemas {DataQualityCheck: 'DataQualityCheck',DataQualityCheckSchedule: 'DataQualityCheckSchedule',WarehouseSuggestion: 'WarehouseSuggestion',+ AccessControl: 'AccessControl',+ DataWarehouseTable: 'DataWarehouseTable',+ DataWarehouseJoin: 'DataWarehouseJoin',Billing: 'Billing',Loop: 'Loop',StamphogRepoConfig: 'StamphogRepoConfig',