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.

1.5 KB
Raw Blame
from django.db import migrations

CHUNK_SIZE = 200


def backfill_llm_playground_access_control(apps, schema_editor):
    # The playground scene used to be gated on llm_analytics; now that it's an independent resource,
    # mirror every resource-wide llm_analytics grant onto a matching playground row so existing
    # permissions keep working instead of silently defaulting to editor. Rows with a resource_id are
    # per-object grants on other llm_analytics sub-resources (datasets, evaluations, ...) and aren't
    # playground-related, so they're excluded.
    # Reads the same resource-wide llm_analytics rows as ee/migrations/0053, so this is similarly near-instant.
    AccessControl = apps.get_model("ee", "AccessControl")

    for row in AccessControl.objects.filter(resource="llm_analytics", resource_id__isnull=True).iterator(
        chunk_size=CHUNK_SIZE
    ):
        AccessControl.objects.get_or_create(
            resource="llm_playground",
            resource_id=None,
            team_id=row.team_id,
            organization_member_id=row.organization_member_id,
            role_id=row.role_id,
            defaults={"access_level": row.access_level, "created_by_id": row.created_by_id},
        )


def reverse_func(apps, schema_editor):
    pass


class Migration(migrations.Migration):
    dependencies = [
        ("ee", "0053_backfill_tagger_access_control"),
    ]

    operations = [
        migrations.RunPython(backfill_llm_playground_access_control, reverse_func),
    ]