:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(clickhouse): sync team event retention months (#93918)
Rory Shanks92c9d50ab374
diff --git a/posthog/dags/postgres_to_clickhouse_etl.py b/posthog/dags/postgres_to_clickhouse_etl.pyindex ba6dd3d0bfe2..799f8a2f85f7 100644--- a/posthog/dags/postgres_to_clickhouse_etl.py+++ b/posthog/dags/postgres_to_clickhouse_etl.py@@ -316,6 +316,7 @@ _TEAM_COLS = ["modifiers","correlation_config","session_recording_retention_period_days",+ "event_retention_months","plugins_opt_in","opt_out_capture","event_names",@@ -462,6 +463,7 @@ def _team_ddl() -> str:modifiers Nullable(String), -- JSON stored as Stringcorrelation_config Nullable(String), -- JSON stored as Stringsession_recording_retention_period_days Nullable(Int32),+ event_retention_months UInt16 DEFAULT 84,plugins_opt_in UInt8,opt_out_capture UInt8,event_names String, -- JSON stored as Stringdiff --git a/posthog/dags/tests/test_postgres_to_clickhouse_etl.py b/posthog/dags/tests/test_postgres_to_clickhouse_etl.pyindex b9f73d483c3c..200229c94e80 100644--- a/posthog/dags/tests/test_postgres_to_clickhouse_etl.py+++ b/posthog/dags/tests/test_postgres_to_clickhouse_etl.py@@ -277,6 +277,8 @@ class TestTableDdl:sql = TABLE_CONFIGS["posthog_team"].ddl()assert "models.posthog_team" in sqlassert "ReplicatedReplacingMergeTree" in sql+ assert "event_retention_months UInt16 DEFAULT 84" in sql+ assert "event_retention_months" in TABLE_CONFIGS["posthog_team"].select_columnsdef test_feature_flag_ddl_shape(self):sql = TABLE_CONFIGS["posthog_featureflag"].ddl()diff --git a/posthog/models/team/event_retention.py b/posthog/models/team/event_retention.pyindex 52f1b0a4a929..1c6729d1e869 100644--- a/posthog/models/team/event_retention.py+++ b/posthog/models/team/event_retention.py@@ -1,6 +1,7 @@from typing import TYPE_CHECKING, Optionalfrom django.conf import settings+from django.utils import timezoneimport posthoganalytics@@ -108,5 +109,5 @@ def reconcile_organization_events_retention(organization: "Organization") -> intreturn (Team.objects.filter(organization=organization).exclude(event_retention_months=target_months)- .update(event_retention_months=target_months)+ .update(event_retention_months=target_months, updated_at=timezone.now()))diff --git a/posthog/models/team/test/test_event_retention.py b/posthog/models/team/test/test_event_retention.pyindex 940e63dad3ff..600a098f422a 100644--- a/posthog/models/team/test/test_event_retention.py+++ b/posthog/models/team/test/test_event_retention.py@@ -1,7 +1,10 @@+from datetime import timedeltafrom typing import castfrom posthog.test.base import BaseTest+from django.utils import timezone+from parameterized import parameterizedfrom posthog.models.organization import Organization, ProductFeature@@ -42,7 +45,10 @@ class TestReconcileOrganizationEventsRetention(BaseTest):def test_updates_all_mismatched_teams(self) -> None:other_team = Team.objects.create(organization=self.organization, name="other")- Team.objects.filter(pk__in=[self.team.pk, other_team.pk]).update(event_retention_months=84)+ previous_updated_at = timezone.now() - timedelta(days=1)+ Team.objects.filter(pk__in=[self.team.pk, other_team.pk]).update(+ event_retention_months=84, updated_at=previous_updated_at+ )self._set_retention_feature(1, "year")assert reconcile_organization_events_retention(self.organization) == 2@@ -51,6 +57,8 @@ class TestReconcileOrganizationEventsRetention(BaseTest):other_team.refresh_from_db()assert self.team.event_retention_months == 12assert other_team.event_retention_months == 12+ assert self.team.updated_at > previous_updated_at+ assert other_team.updated_at > previous_updated_atdef test_no_write_when_already_aligned(self) -> None:self._set_retention_feature(7, "years")diff --git a/posthog/temporal/sync_events_retention/activities.py b/posthog/temporal/sync_events_retention/activities.pyindex 3c4d5c2a01b1..fd14e208bf65 100644--- a/posthog/temporal/sync_events_retention/activities.py+++ b/posthog/temporal/sync_events_retention/activities.py@@ -1,6 +1,7 @@import asynciofrom django.conf import settings+from django.utils import timezonefrom temporalio import activity@@ -79,7 +80,10 @@ async def sync_events_retention(input: SyncEventsRetentionInput) -> SyncEventsReteams_to_update.append(team)if teams_to_update and not input.dry_run:- await Team.objects.abulk_update(teams_to_update, ["event_retention_months"])+ updated_at = timezone.now()+ for team in teams_to_update:+ team.updated_at = updated_at+ await Team.objects.abulk_update(teams_to_update, ["event_retention_months", "updated_at"])# Per batch and off-thread so a mass change can't stall heartbeats or overflow the client queue.await asyncio.to_thread(_capture_retention_changes, changes)diff --git a/posthog/temporal/tests/sync_events_retention/test_activities.py b/posthog/temporal/tests/sync_events_retention/test_activities.pyindex 1779b92819a1..0ab16b888cc2 100644--- a/posthog/temporal/tests/sync_events_retention/test_activities.py+++ b/posthog/temporal/tests/sync_events_retention/test_activities.py@@ -1,8 +1,11 @@from contextlib import contextmanager+from datetime import timedeltaimport pytestfrom unittest.mock import patch+from django.utils import timezone+from asgiref.sync import sync_to_asyncfrom temporalio.testing import ActivityEnvironment@@ -42,11 +45,14 @@ async def _team_with_features(features: list[dict], *, current_months: int) -> Tasync def test_syncs_event_retention_months_from_billing(features: list[dict], expected_months: int):# Start at a sentinel that differs from every expected value, so the assertion proves a real write.team = await _team_with_features(features, current_months=999)+ previous_updated_at = timezone.now() - timedelta(days=1)+ await sync_to_async(Team.objects.filter(pk=team.pk).update)(updated_at=previous_updated_at)await ActivityEnvironment().run(sync_events_retention, SyncEventsRetentionInput(dry_run=False))await sync_to_async(team.refresh_from_db)()assert team.event_retention_months == expected_months+ assert team.updated_at > previous_updated_at@pytest.mark.django_db(transaction=True)