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.

chore: add link idp config model (#75769)

Reece Jonesd5799572174f
diff --git a/.github/scripts/check-idor-model-coverage.py b/.github/scripts/check-idor-model-coverage.py
index 71e160fe7f54..2d25416b26df 100644
--- a/.github/scripts/check-idor-model-coverage.py
+++ b/.github/scripts/check-idor-model-coverage.py
@@ -301,6 +301,7 @@ def get_scoped_models() -> tuple[dict[str, set[str]], set[str], set[str], set[st
"ProxyRecord",
"Role",
"RoleMembership",
+ "LinkedIdentityProviderConfig",
# --- User-scoped (cross-tenant by design) ---
"NotificationViewed",
"SCIMProvisionedUser",
diff --git a/posthog/migrations/1287_linked_identity_provider_configs.py b/posthog/migrations/1287_linked_identity_provider_configs.py
new file mode 100644
index 000000000000..dcb6c06cefed
--- /dev/null
+++ b/posthog/migrations/1287_linked_identity_provider_configs.py
@@ -0,0 +1,49 @@
+import django.db.models.deletion
+from django.db import migrations, models
+
+import posthog.models.utils
+
+
+class Migration(migrations.Migration):
+ dependencies = [("posthog", "1286_cleanup_orphaned_identity_provider_configs")]
+
+ operations = [
+ migrations.CreateModel(
+ name="LinkedIdentityProviderConfig",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.models.utils.uuid7,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ (
+ "identity_provider_config",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="linked_identity_provider_configs",
+ to="posthog.identityproviderconfig",
+ ),
+ ),
+ (
+ "organization_domain",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="linked_identity_provider_configs",
+ to="posthog.organizationdomain",
+ ),
+ ),
+ ],
+ options={
+ "constraints": [
+ models.UniqueConstraint(
+ fields=("organization_domain", "identity_provider_config"),
+ name="unique_linked_identity_provider_config",
+ )
+ ],
+ },
+ ),
+ ]
diff --git a/posthog/migrations/1288_backfill_linked_identity_provider_configs.py b/posthog/migrations/1288_backfill_linked_identity_provider_configs.py
new file mode 100644
index 000000000000..8b6b3415ecff
--- /dev/null
+++ b/posthog/migrations/1288_backfill_linked_identity_provider_configs.py
@@ -0,0 +1,48 @@
+from django.db import migrations
+
+CHUNK_SIZE = 200
+
+
+def backfill_linked_identity_provider_configs(apps, schema_editor):
+ OrganizationDomain = apps.get_model("posthog", "OrganizationDomain")
+ LinkedIdentityProviderConfig = apps.get_model("posthog", "LinkedIdentityProviderConfig")
+
+ links = (
+ OrganizationDomain.objects.filter(identity_provider_config__isnull=False)
+ .values_list("id", "identity_provider_config_id")
+ .iterator(chunk_size=CHUNK_SIZE)
+ )
+
+ chunk = []
+ for domain_id, config_id in links:
+ chunk.append(
+ LinkedIdentityProviderConfig(
+ organization_domain_id=domain_id,
+ identity_provider_config_id=config_id,
+ )
+ )
+ if len(chunk) == CHUNK_SIZE:
+ LinkedIdentityProviderConfig.objects.bulk_create(
+ chunk,
+ ignore_conflicts=True,
+ batch_size=CHUNK_SIZE,
+ )
+ chunk = []
+
+ if chunk:
+ LinkedIdentityProviderConfig.objects.bulk_create(
+ chunk,
+ ignore_conflicts=True,
+ batch_size=CHUNK_SIZE,
+ )
+
+
+class Migration(migrations.Migration):
+ dependencies = [("posthog", "1287_linked_identity_provider_configs")]
+
+ operations = [
+ migrations.RunPython(
+ backfill_linked_identity_provider_configs,
+ reverse_code=migrations.RunPython.noop,
+ ),
+ ]
diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt
index 0d28bac85434..6db0f3a2bb7f 100644
--- a/posthog/migrations/max_migration.txt
+++ b/posthog/migrations/max_migration.txt
@@ -1 +1 @@
-1286_cleanup_orphaned_identity_provider_configs
+1288_backfill_linked_identity_provider_configs
diff --git a/posthog/models/__init__.py b/posthog/models/__init__.py
index b0354024eedd..0a67ab5cce99 100644
--- a/posthog/models/__init__.py
+++ b/posthog/models/__init__.py
@@ -37,6 +37,7 @@ from .group_type_mapping import GroupTypeMapping
from .host_definition import HostDefinition
from .health_issue import HealthIssue
from .identity_provider_config import IdentityProviderConfig
+from .linked_identity_provider_config import LinkedIdentityProviderConfig # noqa: F401
from .instance_setting import InstanceSetting
from .integration import Integration
from .integration_repository_cache import IntegrationRepositoryCacheEntry
diff --git a/posthog/models/linked_identity_provider_config.py b/posthog/models/linked_identity_provider_config.py
new file mode 100644
index 000000000000..2aacab301ee4
--- /dev/null
+++ b/posthog/models/linked_identity_provider_config.py
@@ -0,0 +1,20 @@
+from django.db import models
+
+from posthog.models.utils import UUIDModel
+
+
+class LinkedIdentityProviderConfig(UUIDModel):
+ organization_domain = models.ForeignKey(
+ "posthog.OrganizationDomain", on_delete=models.CASCADE, related_name="linked_identity_provider_configs"
+ )
+ identity_provider_config = models.ForeignKey(
+ "posthog.IdentityProviderConfig", on_delete=models.CASCADE, related_name="linked_identity_provider_configs"
+ )
+
+ class Meta:
+ constraints = [
+ models.UniqueConstraint(
+ fields=("organization_domain", "identity_provider_config"),
+ name="unique_linked_identity_provider_config",
+ )
+ ]
diff --git a/posthog/models/organization_domain.py b/posthog/models/organization_domain.py
index 76937120922e..1d08d03e103e 100644
--- a/posthog/models/organization_domain.py
+++ b/posthog/models/organization_domain.py
@@ -15,6 +15,7 @@ from posthog.constants import AvailableFeature
from posthog.models import Organization
from posthog.models.activity_logging.model_activity import ModelActivityMixin
from posthog.models.identity_provider_config import IdentityProviderConfig
+from posthog.models.linked_identity_provider_config import LinkedIdentityProviderConfig
from posthog.models.utils import UUIDTModel
from posthog.utils import get_instance_available_sso_providers
@@ -319,6 +320,11 @@ class OrganizationDomain(ModelActivityMixin, UUIDTModel):
if errors:
raise ValidationError(errors)
super().save(*args, **kwargs)
+ if self.identity_provider_config_id is not None:
+ LinkedIdentityProviderConfig.objects.get_or_create(
+ organization_domain=self,
+ identity_provider_config_id=self.identity_provider_config_id,
+ )
@property
def is_verified(self) -> bool:
diff --git a/posthog/test/test_identity_provider_config.py b/posthog/test/test_identity_provider_config.py
index 5aa5fc7f46da..920e288e09ca 100644
--- a/posthog/test/test_identity_provider_config.py
+++ b/posthog/test/test_identity_provider_config.py
@@ -3,7 +3,7 @@ from posthog.test.base import BaseTest
from django.core.exceptions import ValidationError
-from posthog.models import IdentityProviderConfig, Organization, OrganizationDomain
+from posthog.models import IdentityProviderConfig, LinkedIdentityProviderConfig, Organization, OrganizationDomain
# Legacy `OrganizationDomain` columns that mirror fields on `IdentityProviderConfig`. Test-only:
# used to build underscore-prefixed kwargs and to guard the two models' field shapes against drift.
@@ -36,6 +36,24 @@ class TestIdentityProviderConfig(BaseTest):
domain.save()
return config
+ def test_creating_domain_with_idp_config_creates_link(self):
+ config = IdentityProviderConfig.objects.create(organization=self.organization)
+ domain = self._create_domain(identity_provider_config=config)
+
+ assert LinkedIdentityProviderConfig.objects.filter(
+ organization_domain=domain, identity_provider_config=config
+ ).exists()
+
+ def test_updating_domain_idp_config_creates_link(self):
+ domain = self._create_domain()
+ config = IdentityProviderConfig.objects.create(organization=self.organization)
+ domain.identity_provider_config = config
+ domain.save()
+
+ assert LinkedIdentityProviderConfig.objects.filter(
+ organization_domain=domain, identity_provider_config=config
+ ).exists()
+
def test_saving_legacy_idp_columns_does_not_create_or_link_config(self):
# The domain<->config dual-write mirror has been removed: writing the legacy underscore
# columns must no longer auto-create or link an IdentityProviderConfig.
diff --git a/posthog/test/test_migration_1279.py b/posthog/test/test_migration_1279.py
new file mode 100644
index 000000000000..0ec46ff93858
--- /dev/null
+++ b/posthog/test/test_migration_1279.py
@@ -0,0 +1,49 @@
+from importlib import import_module
+
+from posthog.test.base import BaseTest
+
+from django.apps import apps
+
+from posthog.models import IdentityProviderConfig, LinkedIdentityProviderConfig, OrganizationDomain
+
+migration = import_module("posthog.migrations.1288_backfill_linked_identity_provider_configs")
+
+
+class TestLinkedIdentityProviderConfigBackfill(BaseTest):
+ def test_backfills_existing_domain_config_links_and_skips_unlinked_domains(self) -> None:
+ linked_config = IdentityProviderConfig.objects.create(organization=self.organization)
+ orphan_config = IdentityProviderConfig.objects.create(organization=self.organization)
+ linked_domain = OrganizationDomain.objects.create(
+ organization=self.organization,
+ domain="linked.example.com",
+ identity_provider_config=linked_config,
+ )
+ OrganizationDomain.objects.create(
+ organization=self.organization,
+ domain="unlinked.example.com",
+ )
+
+ migration.backfill_linked_identity_provider_configs(apps, None)
+
+ assert list(
+ LinkedIdentityProviderConfig.objects.values_list("organization_domain_id", "identity_provider_config_id")
+ ) == [(linked_domain.id, linked_config.id)]
+ assert not LinkedIdentityProviderConfig.objects.filter(identity_provider_config_id=orphan_config.id).exists()
+
+ def test_backfill_is_safe_to_run_again(self) -> None:
+ config = IdentityProviderConfig.objects.create(organization=self.organization)
+ domain = OrganizationDomain.objects.create(
+ organization=self.organization,
+ domain="linked.example.com",
+ identity_provider_config=config,
+ )
+
+ migration.backfill_linked_identity_provider_configs(apps, None)
+ migration.backfill_linked_identity_provider_configs(apps, None)
+
+ assert (
+ LinkedIdentityProviderConfig.objects.filter(
+ organization_domain_id=domain.id, identity_provider_config_id=config.id
+ ).count()
+ == 1
+ )