Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(insights): add migration to convert filters to query #24527

Merged
merged 9 commits into from
Jan 16, 2025
48 changes: 48 additions & 0 deletions posthog/migrations/0545_insight_filters_to_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 4.2.14 on 2024-08-22 12:23
import logging

from datetime import datetime
from django.db import migrations, connection
from django.db.models import Q

from posthog.hogql_queries.legacy_compatibility.filter_to_query import filter_to_query
from posthog.models import Insight
from posthog.schema import InsightVizNode

logger = logging.getLogger(__name__)


def migrate_insight_filters_to_query(apps, schema_editor):
insights = Insight.objects.filter(Q(filters__insight__isnull=False) & Q(query__kind__isnull=True))
migrated_at = datetime.now()

for insight in insights.iterator(chunk_size=100):
try:
source = filter_to_query(insight.filters)
query = InsightVizNode(source=source)
insight.query = query.model_dump(exclude_none=True)

# add a migrated_at as filter, so that we can find migrated insights for rolling back
insight.filters["migrated_at"] = str(migrated_at)
insight.save()
except Exception:
logger.error(f"Error converting insight with id {insight.pk}") # noqa: TRY400


def rollback_insight_filters_to_query(apps, schema_editor):
with connection.cursor() as cursor:
cursor.execute(
"""
UPDATE posthog_dashboarditem
SET query = NULL, filters = filters - 'migrated_at'
WHERE filters->>'migrated_at' IS NOT NULL
"""
)


class Migration(migrations.Migration):
dependencies = [("posthog", "0544_team_flags_persistence_default")]

operations = [
migrations.RunPython(migrate_insight_filters_to_query, rollback_insight_filters_to_query),
]
2 changes: 1 addition & 1 deletion posthog/migrations/max_migration.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0544_team_flags_persistence_default
0545_insight_filters_to_query
Loading