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.

feat(provisioning): add organization provisioning model (#107869)

Matt Brooker0cc8de49aeb5
diff --git a/.github/scripts/check-idor-model-coverage.py b/.github/scripts/check-idor-model-coverage.py
index 695e1b076907..5c47ee2a525c 100644
--- a/.github/scripts/check-idor-model-coverage.py
+++ b/.github/scripts/check-idor-model-coverage.py
@@ -170,6 +170,11 @@ def get_scoped_models() -> tuple[dict[str, set[str]], set[str], set[str], set[st
# OneToOne extension of Organization, read via the org relation
# (enrichment_record), never looked up by user-supplied ID.
"OrganizationEnrichment",
+ # Write-once OneToOne record of the partner that created an Organization, written by the
+ # partner account-creation paths; no API endpoint, never looked up by user-supplied ID.
+ # Remove this exemption the moment an endpoint exposes it, or the org_scoped rule stops
+ # protecting it silently.
+ "OrganizationProvisioning",
# Write-once idempotency guard keyed on the org, claimed via get_or_create from an
# internal enrichment write-back path, never looked up by user-supplied ID.
"EnrichmentSignupSnapshot",
diff --git a/posthog/migrations/1391_organization_provisioning.py b/posthog/migrations/1391_organization_provisioning.py
new file mode 100644
index 000000000000..1eea5a68531d
--- /dev/null
+++ b/posthog/migrations/1391_organization_provisioning.py
@@ -0,0 +1,68 @@
+# Generated by Django 5.2.17 on 2026-09-29 17:27
+
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [("posthog", "1390_rename_desktop_canvas_comment_scope")]
+
+ operations = [
+ migrations.CreateModel(
+ name="OrganizationProvisioning",
+ fields=[
+ (
+ "organization",
+ models.OneToOneField(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ primary_key=True,
+ related_name="partner_provisioning",
+ serialize=False,
+ to="posthog.organization",
+ ),
+ ),
+ (
+ "partner",
+ models.CharField(
+ choices=[
+ ("provisioning_api", "Provisioning Api"),
+ ("stripe_projects", "Stripe Projects"),
+ ("vercel", "Vercel"),
+ ],
+ max_length=32,
+ ),
+ ),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ (
+ "application",
+ models.ForeignKey(
+ blank=True,
+ null=True,
+ on_delete=django.db.models.deletion.PROTECT,
+ related_name="provisioned_organizations",
+ to=settings.OAUTH2_PROVIDER_APPLICATION_MODEL,
+ ),
+ ),
+ ],
+ options={
+ "constraints": [
+ models.CheckConstraint(
+ condition=models.Q(
+ models.Q(("application__isnull", True), ("partner", "vercel")),
+ models.Q(
+ ("application__isnull", False),
+ (
+ "partner__in",
+ ["provisioning_api", "stripe_projects"],
+ ),
+ ),
+ _connector="OR",
+ ),
+ name="org_provisioning_application_matches_partner",
+ )
+ ],
+ },
+ ),
+ ]
diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt
index edd202d38d78..8c4dc7162fe7 100644
--- a/posthog/migrations/max_migration.txt
+++ b/posthog/migrations/max_migration.txt
@@ -1 +1 @@
-1390_rename_desktop_canvas_comment_scope
+1391_organization_provisioning
diff --git a/posthog/models/__init__.py b/posthog/models/__init__.py
index 22cb88302bcf..5a2daf6f1543 100644
--- a/posthog/models/__init__.py
+++ b/posthog/models/__init__.py
@@ -48,6 +48,7 @@ from .organization_domain import OrganizationDomain
from .organization_notification_lock import OrganizationMemberNotificationLock
from .organization_integration import OrganizationIntegration
from .organization_invite import OrganizationInvite, InviteExpiredException
+from .organization_provisioning import OrganizationProvisioning
from .person import Person, PersonDistinctId, PersonOverride, PersonOverrideMapping
from .personal_api_key import PersonalAPIKey
from .project_secret_api_key import ProjectSecretAPIKey
@@ -136,6 +137,7 @@ __all__ = [
"OrganizationIntegration",
"OrganizationInvite",
"OrganizationMembership",
+ "OrganizationProvisioning",
"OAuthAccessToken",
"OAuthApplication",
"OAuthGrant",
diff --git a/posthog/models/organization_provisioning.py b/posthog/models/organization_provisioning.py
new file mode 100644
index 000000000000..476636e85e89
--- /dev/null
+++ b/posthog/models/organization_provisioning.py
@@ -0,0 +1,38 @@
+from django.db import models
+from django.db.models import Q
+
+
+class OrganizationProvisioning(models.Model):
+ class Partner(models.TextChoices):
+ PROVISIONING_API = "provisioning_api"
+ STRIPE_PROJECTS = "stripe_projects"
+ VERCEL = "vercel"
+
+ # db_constraint=False: posthog_organization is hot, and the FK constraint would lock it.
+ organization = models.OneToOneField(
+ "posthog.Organization",
+ on_delete=models.CASCADE,
+ primary_key=True,
+ related_name="partner_provisioning",
+ db_constraint=False,
+ )
+ partner = models.CharField(max_length=32, choices=Partner.choices)
+ # Vercel calls PostHog with Vercel-signed tokens, not through a PostHog OAuth application,
+ # so a Vercel row has no application to reference.
+ application = models.ForeignKey(
+ "posthog.OAuthApplication",
+ on_delete=models.PROTECT,
+ null=True,
+ blank=True,
+ related_name="provisioned_organizations",
+ )
+ created_at = models.DateTimeField(auto_now_add=True)
+
+ class Meta:
+ constraints = [
+ models.CheckConstraint(
+ condition=Q(partner="vercel", application__isnull=True)
+ | Q(partner__in=["provisioning_api", "stripe_projects"], application__isnull=False),
+ name="org_provisioning_application_matches_partner",
+ )
+ ]
diff --git a/posthog/models/test/test_organization_provisioning_model.py b/posthog/models/test/test_organization_provisioning_model.py
new file mode 100644
index 000000000000..533bc44d804a
--- /dev/null
+++ b/posthog/models/test/test_organization_provisioning_model.py
@@ -0,0 +1,50 @@
+from posthog.test.base import BaseTest
+
+from django.db import transaction
+from django.db.utils import IntegrityError
+
+from parameterized import parameterized
+
+from posthog.models.oauth import OAuthApplication
+from posthog.models.organization_provisioning import OrganizationProvisioning
+
+
+class TestOrganizationProvisioningModel(BaseTest):
+ @parameterized.expand(
+ [
+ ("provisioning_api_with_application", OrganizationProvisioning.Partner.PROVISIONING_API, True, True),
+ ("stripe_projects_with_application", OrganizationProvisioning.Partner.STRIPE_PROJECTS, True, True),
+ ("stripe_projects_without_application", OrganizationProvisioning.Partner.STRIPE_PROJECTS, False, False),
+ ("vercel_without_application", OrganizationProvisioning.Partner.VERCEL, False, True),
+ ("provisioning_api_without_application", OrganizationProvisioning.Partner.PROVISIONING_API, False, False),
+ ("vercel_with_application", OrganizationProvisioning.Partner.VERCEL, True, False),
+ ("unknown_partner_with_application", "verce", True, False),
+ ]
+ )
+ def test_application_must_match_partner(
+ self, _name: str, partner: OrganizationProvisioning.Partner | str, with_application: bool, allowed: bool
+ ) -> None:
+ application = (
+ OAuthApplication.objects.create(
+ client_id="partner",
+ name="partner",
+ client_secret="",
+ client_type=OAuthApplication.CLIENT_PUBLIC,
+ authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
+ redirect_uris="https://partner.example.com/callback",
+ algorithm="RS256",
+ )
+ if with_application
+ else None
+ )
+
+ def create() -> OrganizationProvisioning:
+ return OrganizationProvisioning.objects.create(
+ organization=self.organization, partner=partner, application=application
+ )
+
+ if allowed:
+ assert create().partner == partner
+ else:
+ with transaction.atomic(), self.assertRaises(IntegrityError):
+ create()