: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(insights): discover migrations before building upgrade query (#68312)
posthog[bot]40972b8c425b
diff --git a/posthog/temporal/product_analytics/upgrade_queries_activities.py b/posthog/temporal/product_analytics/upgrade_queries_activities.pyindex 1c62c4a7655c..28521176a687 100644--- a/posthog/temporal/product_analytics/upgrade_queries_activities.py+++ b/posthog/temporal/product_analytics/upgrade_queries_activities.py@@ -7,7 +7,7 @@ from django.db import connectionfrom structlog import get_loggerfrom temporalio import activity-from posthog.schema_migrations import LATEST_VERSIONS+from posthog.schema_migrations import LATEST_VERSIONS, _discover_migrationsfrom posthog.schema_migrations.upgrade import upgradefrom products.product_analytics.backend.models.insight import Insight@@ -42,7 +42,13 @@ class GetInsightsToMigrateActivityResult:@activity.defndef get_insights_to_migrate(inputs: GetInsightsToMigrateActivityInputs) -> GetInsightsToMigrateActivityResult:+ _discover_migrations() # Populate LATEST_VERSIONS; this is the first activity in the workflow+clauses = [_clause(k, v) for k, v in sorted(LATEST_VERSIONS.items())]+ if not clauses:+ # No migrations registered — guard against emitting `WHERE ()`, which Postgres rejects+ return GetInsightsToMigrateActivityResult(insight_ids=[], last_id=inputs.after_id)+after_clause = "" if inputs.after_id is None else f"\nAND id > {inputs.after_id}"where_body = ("\n OR ").join(clauses)sql = f"""diff --git a/posthog/temporal/tests/product_analytics/test_upgrade_queries_workflow.py b/posthog/temporal/tests/product_analytics/test_upgrade_queries_workflow.pyindex 5546abd087fc..4305054aad67 100644--- a/posthog/temporal/tests/product_analytics/test_upgrade_queries_workflow.py+++ b/posthog/temporal/tests/product_analytics/test_upgrade_queries_workflow.py@@ -213,6 +213,18 @@ class TestUpgradeQueriesWorkflow(QueryMatchingTest):assert sorted(result.insight_ids) == expected_idsassert result.last_id == i8.id+ @pytest.mark.django_db+ def test_get_insights_to_migrate_activity_with_no_migrations(self, activity_environment, team):+ # On a fresh worker LATEST_VERSIONS can be empty; the activity must not+ # emit `WHERE ()` (a Postgres syntax error) and should return no ids.+ setup_insights(team)+ LATEST_VERSIONS.clear()++ result = activity_environment.run(get_insights_to_migrate, GetInsightsToMigrateActivityInputs())++ assert result.insight_ids == []+ assert result.last_id is None+@pytest.mark.django_dbdef test_migrate_insights_batch_activity(self, activity_environment, team):i1, i2, i3, i4, i5, i6, i7, i8 = setup_insights(team)