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.

feat(experiments): retire legacy single recalculation time field (#110127)

Juraj Majerik45db9c90802b
diff --git a/frontend/src/scenes/settings/environment/experimentsConfigLogic.ts b/frontend/src/scenes/settings/environment/experimentsConfigLogic.ts
index dcf472c67df5..4bbf687e5fe2 100644
--- a/frontend/src/scenes/settings/environment/experimentsConfigLogic.ts
+++ b/frontend/src/scenes/settings/environment/experimentsConfigLogic.ts
@@ -9,7 +9,6 @@ import { teamLogic } from '~/scenes/teamLogic'
import { DEFAULT_MDE } from 'products/experiments/frontend/constants'
export interface ExperimentsConfig {
- experiment_recalculation_time: string | null
experiment_recalculation_times: string[] | null
default_experiment_confidence_level: number | null
default_experiment_stats_method: string | null
diff --git a/frontend/src/scenes/settings/stories/SettingsEnvironment.stories.tsx b/frontend/src/scenes/settings/stories/SettingsEnvironment.stories.tsx
index 093558279591..339a7897663e 100644
--- a/frontend/src/scenes/settings/stories/SettingsEnvironment.stories.tsx
+++ b/frontend/src/scenes/settings/stories/SettingsEnvironment.stories.tsx
@@ -141,7 +141,6 @@ export const SettingsEnvironmentMarketingAnalytics: Story = {
export const SettingsEnvironmentWebAnalytics: Story = { args: { sectionId: 'environment-web-analytics' } }
const EXPERIMENTS_CONFIG_MOCK = {
- experiment_recalculation_time: '02:00:00',
experiment_recalculation_times: ['02:00:00'],
default_experiment_confidence_level: null,
default_experiment_stats_method: null,
diff --git a/posthog/admin/inlines/team_experiments_config_inline.py b/posthog/admin/inlines/team_experiments_config_inline.py
index 35cb40f1fb81..d02e5d5e7f66 100644
--- a/posthog/admin/inlines/team_experiments_config_inline.py
+++ b/posthog/admin/inlines/team_experiments_config_inline.py
@@ -1,11 +1,7 @@
from django import forms
from django.contrib import admin
-from products.experiments.backend.models.team_experiments_config import (
- TeamExperimentsConfig,
- legacy_from_recalculation_times,
- recalculation_times_from_legacy,
-)
+from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfig
class TeamExperimentsConfigInlineForm(forms.ModelForm):
@@ -14,17 +10,8 @@ class TeamExperimentsConfigInlineForm(forms.ModelForm):
# writes when precomputation_enabled_set_by is null or "auto".
if "experiment_precomputation_enabled" in self.changed_data:
self.instance.precomputation_enabled_set_by = TeamExperimentsConfig.PrecomputationEnabledSetBy.MANUAL
- # The two recalculation fields must stay coherent while both exist: writing one
- # syncs the other, matching the experiments_config serializer.
if "experiment_recalculation_times" in self.changed_data:
self.instance.experiment_recalculation_times = self.instance.experiment_recalculation_times or None
- self.instance.experiment_recalculation_time = legacy_from_recalculation_times(
- self.instance.experiment_recalculation_times
- )
- elif "experiment_recalculation_time" in self.changed_data:
- self.instance.experiment_recalculation_times = recalculation_times_from_legacy(
- self.instance.experiment_recalculation_time
- )
return super().save(commit)
@@ -40,7 +27,6 @@ class TeamExperimentsConfigInline(admin.StackedInline):
"Experiments",
{
"fields": [
- "experiment_recalculation_time",
"experiment_recalculation_times",
"default_experiment_confidence_level",
"default_experiment_stats_method",
diff --git a/posthog/admin/test_team_experiments_config_inline.py b/posthog/admin/test_team_experiments_config_inline.py
index efca5912be99..4497bb0ccf59 100644
--- a/posthog/admin/test_team_experiments_config_inline.py
+++ b/posthog/admin/test_team_experiments_config_inline.py
@@ -1,5 +1,3 @@
-from datetime import time
-
from posthog.test.base import BaseTest
from django.forms import ModelForm, modelform_factory
@@ -16,7 +14,7 @@ class TestTeamExperimentsConfigInlineForm(BaseTest):
form_class = modelform_factory(
TeamExperimentsConfig,
form=TeamExperimentsConfigInlineForm,
- fields=["experiment_recalculation_time", "experiment_recalculation_times"],
+ fields=["experiment_recalculation_times"],
)
config = get_or_create_team_extension(self.team, TeamExperimentsConfig)
return form_class(data=data, instance=config)
@@ -26,22 +24,8 @@ class TestTeamExperimentsConfigInlineForm(BaseTest):
self.assertFalse(form.is_valid())
self.assertIn("experiment_recalculation_times", form.errors)
- def test_admin_saves_keep_recalculation_fields_in_sync(self):
- # The hourly workflows read the legacy column; an admin edit that skips the
- # sync leaves recalculations running at the old hour.
+ def test_admin_saves_recalculation_times(self):
form = self._form({"experiment_recalculation_times": '["14:00:00", "02:00:00"]'})
self.assertTrue(form.is_valid(), form.errors)
config = form.save()
- self.assertEqual(config.experiment_recalculation_time, time(hour=14))
-
- # The admin submits every rendered field, so an edit to the legacy time arrives
- # with the list field holding its unchanged value.
- form = self._form(
- {
- "experiment_recalculation_time": "08:00:00",
- "experiment_recalculation_times": '["14:00:00", "02:00:00"]',
- }
- )
- self.assertTrue(form.is_valid(), form.errors)
- config = form.save()
- self.assertEqual(config.experiment_recalculation_times, ["08:00:00"])
+ self.assertEqual(config.experiment_recalculation_times, ["14:00:00", "02:00:00"])
diff --git a/posthog/api/team.py b/posthog/api/team.py
index a64c90d76962..e6ec7462b42f 100644
--- a/posthog/api/team.py
+++ b/posthog/api/team.py
@@ -371,8 +371,6 @@ def handle_experiments_config(request: request.Request, team: Team) -> response.
MAX_RECALCULATION_TIMES,
MIN_RECALCULATION_GAP_HOURS,
TeamExperimentsConfig,
- legacy_from_recalculation_times,
- recalculation_times_from_legacy,
validate_recalculation_times,
)
@@ -389,15 +387,13 @@ def handle_experiments_config(request: request.Request, team: Team) -> response.
help_text=(
"Times of day (UTC) when experiment metrics are recalculated, as 'HH:00:00' strings "
f"on the hour. At most {MAX_RECALCULATION_TIMES} entries, at least "
- f"{MIN_RECALCULATION_GAP_HOURS} hours apart. Null means the default time (02:00 UTC). "
- "Takes precedence over experiment_recalculation_time."
+ f"{MIN_RECALCULATION_GAP_HOURS} hours apart. Null means the default time (02:00 UTC)."
),
)
class Meta:
model = TeamExperimentsConfig
fields = [
- "experiment_recalculation_time",
"experiment_recalculation_times",
"default_experiment_confidence_level",
"default_experiment_stats_method",
@@ -423,16 +419,6 @@ def handle_experiments_config(request: request.Request, team: Team) -> response.
# writes when precomputation_enabled_set_by is null or "auto".
if "experiment_precomputation_enabled" in validated_data:
instance.precomputation_enabled_set_by = TeamExperimentsConfig.PrecomputationEnabledSetBy.MANUAL
- # The two recalculation fields must stay coherent while both exist: writing one
- # syncs the other, so old clients and the workflow reader never disagree.
- if "experiment_recalculation_times" in validated_data:
- validated_data["experiment_recalculation_time"] = legacy_from_recalculation_times(
- validated_data["experiment_recalculation_times"]
- )
- elif "experiment_recalculation_time" in validated_data:
- validated_data["experiment_recalculation_times"] = recalculation_times_from_legacy(
- validated_data["experiment_recalculation_time"]
- )
return super().update(instance, validated_data)
def validate_flag_cleanup_repository(self, value: str | None) -> str | None:
diff --git a/posthog/api/test/test_project.py b/posthog/api/test/test_project.py
index d999e91621ec..218b37535735 100644
--- a/posthog/api/test/test_project.py
+++ b/posthog/api/test/test_project.py
@@ -1,4 +1,4 @@
-from datetime import time, timedelta
+from datetime import timedelta
from unittest.mock import MagicMock, patch
@@ -1259,9 +1259,7 @@ class TestProjectAPI(team_api_test_factory()): # type: ignore
config.refresh_from_db()
self.assertEqual(config.precomputation_enabled_set_by, TeamExperimentsConfig.PrecomputationEnabledSetBy.MANUAL)
- def test_experiments_config_recalculation_times_sync_with_legacy_field(self):
- # The hourly workflow and older clients read experiment_recalculation_time while
- # newer clients read the list; if the sync breaks, recalcs run at the wrong hour.
+ def test_experiments_config_recalculation_times_write_and_clear(self):
response = self.client.patch(
f"/api/projects/{self.project.id}/experiments_config/",
{"experiment_recalculation_times": ["14:00:00", "02:00:00"]},
@@ -1270,8 +1268,9 @@ class TestProjectAPI(team_api_test_factory()): # type: ignore
self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
config = TeamExperimentsConfig.objects.get(team_id=self.project.id)
self.assertEqual(config.experiment_recalculation_times, ["14:00:00", "02:00:00"])
- self.assertEqual(config.experiment_recalculation_time, time(hour=14))
+ # Old clients still PATCH the retired experiment_recalculation_time field;
+ # it must be ignored, not rejected.
response = self.client.patch(
f"/api/projects/{self.project.id}/experiments_config/",
{"experiment_recalculation_time": "08:00:00"},
@@ -1279,8 +1278,7 @@ class TestProjectAPI(team_api_test_factory()): # type: ignore
)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
config.refresh_from_db()
- self.assertEqual(config.experiment_recalculation_times, ["08:00:00"])
- self.assertEqual(config.experiment_recalculation_time, time(hour=8))
+ self.assertEqual(config.experiment_recalculation_times, ["14:00:00", "02:00:00"])
response = self.client.patch(
f"/api/projects/{self.project.id}/experiments_config/",
@@ -1290,7 +1288,6 @@ class TestProjectAPI(team_api_test_factory()): # type: ignore
self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
config.refresh_from_db()
self.assertIsNone(config.experiment_recalculation_times)
- self.assertIsNone(config.experiment_recalculation_time)
@parameterized.expand(
[
diff --git a/posthog/temporal/experiments/test_recalculation_time_filter.py b/posthog/temporal/experiments/test_recalculation_time_filter.py
index fb464417239b..f102e62fc266 100644
--- a/posthog/temporal/experiments/test_recalculation_time_filter.py
+++ b/posthog/temporal/experiments/test_recalculation_time_filter.py
@@ -1,5 +1,4 @@
import datetime
-from datetime import time
from zoneinfo import ZoneInfo
import pytest
@@ -53,7 +52,6 @@ class TestRecalculationTimeFilter:
user = User.objects.create(email="[email protected]")
config = get_or_create_team_extension(team, TeamExperimentsConfig)
- config.experiment_recalculation_time = time(5, 0, 0)
config.experiment_recalculation_times = ["05:00:00"]
config.save()
@@ -74,7 +72,6 @@ class TestRecalculationTimeFilter:
user = User.objects.create(email="[email protected]")
config = get_or_create_team_extension(team, TeamExperimentsConfig)
- config.experiment_recalculation_time = time(8, 0, 0)
config.experiment_recalculation_times = ["08:00:00", "20:00:00"]
config.save()
@@ -92,7 +89,6 @@ class TestRecalculationTimeFilter:
user = User.objects.create(email="[email protected]")
config = get_or_create_team_extension(team, TeamExperimentsConfig)
- config.experiment_recalculation_time = time(8, 0, 0)
config.experiment_recalculation_times = ["08:00:00", "20:00:00"]
config.save()
diff --git a/posthog/temporal/experiments/utils.py b/posthog/temporal/experiments/utils.py
index 74a8d5fe5470..c007744a5400 100644
--- a/posthog/temporal/experiments/utils.py
+++ b/posthog/temporal/experiments/utils.py
@@ -24,8 +24,7 @@ def recalculation_hour_filter(hour: int) -> Q:
The filter traverses Experiment -> Team -> TeamExperimentsConfig via Django's reverse
relation. experiment_recalculation_times holds "HH:00:00" strings, so hour membership
is a jsonb containment check. A null list or a missing config row means the default
- hour; the deprecated experiment_recalculation_time column is not consulted, because
- the API keeps the list in sync with it on every write.
+ hour.
"""
match = Q(team__teamexperimentsconfig__experiment_recalculation_times__contains=[f"{hour:02d}:00:00"])
if hour == DEFAULT_EXPERIMENT_RECALCULATION_HOUR:
diff --git a/products/experiments/backend/migrations/0046_untrack_legacy_recalculation_time.py b/products/experiments/backend/migrations/0046_untrack_legacy_recalculation_time.py
new file mode 100644
index 000000000000..0bfb10a3c4ff
--- /dev/null
+++ b/products/experiments/backend/migrations/0046_untrack_legacy_recalculation_time.py
@@ -0,0 +1,14 @@
+from django.db import migrations
+
+from posthog.migration_helpers import untrack_field
+
+
+class Migration(migrations.Migration):
+ dependencies = [("experiments", "0045_alter_experimentmetricsrecalculation_trigger")]
+
+ operations = [
+ # Phase 1 of retiring the legacy single-time column. The field leaves model state
+ # while the column stays, so pods on either release keep querying the table.
+ # The DROP COLUMN follows in its own migration one deploy cycle later.
+ untrack_field("teamexperimentsconfig", "experiment_recalculation_time"),
+ ]
diff --git a/products/experiments/backend/migrations/max_migration.txt b/products/experiments/backend/migrations/max_migration.txt
index f920827f1686..8d985d32bff5 100644
--- a/products/experiments/backend/migrations/max_migration.txt
+++ b/products/experiments/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0045_alter_experimentmetricsrecalculation_trigger
+0046_untrack_legacy_recalculation_time
diff --git a/products/experiments/backend/models/team_experiments_config.py b/products/experiments/backend/models/team_experiments_config.py
index d8faacc53c76..cfd857f53efe 100644
--- a/products/experiments/backend/models/team_experiments_config.py
+++ b/products/experiments/backend/models/team_experiments_config.py
@@ -1,5 +1,4 @@
import re
-from datetime import time
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
@@ -40,14 +39,6 @@ def validate_recalculation_times(value: object) -> None:
)
-def recalculation_times_from_legacy(legacy: time | None) -> list[str] | None:
- return [f"{legacy.hour:02d}:00:00"] if legacy is not None else None
-
-
-def legacy_from_recalculation_times(times: list[str] | None) -> time | None:
- return time(hour=int(times[0][:2])) if times else None
-
-
class TeamExperimentsConfig(models.Model):
class PrecomputationEnabledSetBy(models.TextChoices):
MANUAL = "manual", "Manual"
@@ -55,15 +46,6 @@ class TeamExperimentsConfig(models.Model):
team = models.OneToOneField(Team, on_delete=models.CASCADE, primary_key=True)
- experiment_recalculation_time = models.TimeField(
- null=True,
- blank=True,
- help_text=(
- "Deprecated in favor of experiment_recalculation_times, which takes precedence when set. "
- "Kept in sync with its first entry for older clients."
- ),
- )
-
experiment_recalculation_times = models.JSONField(
null=True,
blank=True,