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.5 KB
Raw Blame
# Generated by Django 5.2.14 on 2026-06-03 09:18

from django.db import migrations

MODELS_TO_MOVE = (
    "agentartifact",
    "conversation",
    "conversationcheckpoint",
    "conversationcheckpointblob",
    "conversationcheckpointwrite",
    "corememory",
)


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", "posthog_ai")


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


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

    operations = [
        migrations.SeparateDatabaseAndState(
            state_operations=[
                migrations.DeleteModel(
                    name="Conversation",
                ),
                migrations.DeleteModel(
                    name="ConversationCheckpointWrite",
                ),
                migrations.DeleteModel(
                    name="ConversationCheckpoint",
                ),
                migrations.DeleteModel(
                    name="ConversationCheckpointBlob",
                ),
                migrations.DeleteModel(
                    name="CoreMemory",
                ),
            ],
            database_operations=[],
        ),
        migrations.RunPython(update_content_types, reverse_content_types),
    ]