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.

2.6 KB
Raw Blame
# Generated by Django 5.2.14 on 2026-06-08 10:07

from django.db import migrations

MODELS_TO_MOVE = ("sessiongroupsummary", "singlesessionsummary", "teamsessionsummariesconfig")


def _move_content_types(apps, schema_editor, from_label, to_label):
    """Move ContentTypes between apps, handling duplicates idempotently.

    If the target (app_label, model) already exists (e.g. Django auto-created it,
    or a prior failed attempt left one behind), merge permissions onto the target
    and delete the source to avoid a unique-constraint violation. Uses the
    migration's own connection alias for multi-DB safety.
    """
    db_alias = schema_editor.connection.alias
    ContentType = apps.get_model("contenttypes", "ContentType")
    Permission = apps.get_model("auth", "Permission")

    for model_name in MODELS_TO_MOVE:
        source = ContentType.objects.using(db_alias).filter(app_label=from_label, model=model_name).first()
        target = ContentType.objects.using(db_alias).filter(app_label=to_label, model=model_name).first()

        if source and target:
            Permission.objects.using(db_alias).filter(content_type=source).update(content_type=target)
            source.delete(using=db_alias)
        elif source:
            source.app_label = to_label
            source.save(using=db_alias)


def update_content_types(apps, schema_editor):
    _move_content_types(apps, schema_editor, "ee", "replay")


def reverse_content_types(apps, schema_editor):
    _move_content_types(apps, schema_editor, "replay", "ee")


class Migration(migrations.Migration):
    dependencies = [
        ("ee", "0049_migrate_posthog_ai_models"),
    ]

    operations = [
        migrations.SeparateDatabaseAndState(
            state_operations=[
                migrations.RemoveField(
                    model_name="singlesessionsummary",
                    name="created_by",
                ),
                migrations.RemoveField(
                    model_name="singlesessionsummary",
                    name="team",
                ),
                migrations.RemoveField(
                    model_name="teamsessionsummariesconfig",
                    name="team",
                ),
                migrations.DeleteModel(
                    name="SessionGroupSummary",
                ),
                migrations.DeleteModel(
                    name="SingleSessionSummary",
                ),
                migrations.DeleteModel(
                    name="TeamSessionSummariesConfig",
                ),
            ],
            database_operations=[],
        ),
        migrations.RunPython(update_content_types, reverse_content_types),
    ]