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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion latest_migrations.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ contenttypes: 0002_remove_content_type_name
ee: 0016_rolemembership_organization_member
otp_static: 0002_throttling
otp_totp: 0002_auto_20190420_0723
posthog: 0458_alter_insightviewed_team_alter_insightviewed_user
posthog: 0459_insight_filters_to_query
sessions: 0001_initial
social_django: 0010_uid_db_index
two_factor: 0007_auto_20201201_1019
50 changes: 50 additions & 0 deletions posthog/migrations/0459_insight_filters_to_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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()
thmsobrmlr marked this conversation as resolved.
Show resolved Hide resolved

# 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", "0458_alter_insightviewed_team_alter_insightviewed_user"),
thmsobrmlr marked this conversation as resolved.
Show resolved Hide resolved
]

operations = [
migrations.RunPython(migrate_insight_filters_to_query, rollback_insight_filters_to_query),
]
Loading