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: extract domain-id fields to idp-config (#77534)

Reece Jones3726a6159315
diff --git a/posthog/migrations/1289_identityproviderconfig_relay_state_and_scim_slug.py b/posthog/migrations/1289_identityproviderconfig_relay_state_and_scim_slug.py
new file mode 100644
index 000000000000..f1b208b898fb
--- /dev/null
+++ b/posthog/migrations/1289_identityproviderconfig_relay_state_and_scim_slug.py
@@ -0,0 +1,22 @@
+# Generated by Django 5.2.14 on 2026-08-04 13:54
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1288_backfill_linked_identity_provider_configs"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="identityproviderconfig",
+ name="saml_relay_state",
+ field=models.CharField(blank=True, max_length=36, null=True),
+ ),
+ migrations.AddField(
+ model_name="identityproviderconfig",
+ name="scim_slug",
+ field=models.CharField(blank=True, max_length=36, null=True, unique=True),
+ ),
+ ]
diff --git a/posthog/migrations/1290_backfill_identity_provider_config_identifiers.py b/posthog/migrations/1290_backfill_identity_provider_config_identifiers.py
new file mode 100644
index 000000000000..211226bcf54d
--- /dev/null
+++ b/posthog/migrations/1290_backfill_identity_provider_config_identifiers.py
@@ -0,0 +1,58 @@
+# Generated by Django 5.2.14 on 2026-08-04 13:54
+
+from typing import Any
+
+from django.db import migrations
+
+import structlog
+
+logger = structlog.get_logger(__name__)
+
+CHUNK_SIZE = 200
+
+
+def backfill_identity_provider_config_identifiers(apps: Any, schema_editor: Any) -> None:
+ IdentityProviderConfig = apps.get_model("posthog", "IdentityProviderConfig")
+ OrganizationDomain = apps.get_model("posthog", "OrganizationDomain")
+ db_alias = schema_editor.connection.alias
+
+ configs = IdentityProviderConfig.objects.using(db_alias).filter(
+ saml_relay_state__isnull=True,
+ scim_slug__isnull=True,
+ )
+ for config in configs.iterator(chunk_size=CHUNK_SIZE):
+ domain_ids = list(
+ OrganizationDomain.objects.using(db_alias)
+ .filter(identity_provider_config_id=config.pk)
+ .values_list("id", flat=True)[:2]
+ )
+ if len(domain_ids) > 1:
+ logger.warning(
+ "identity_provider_config_has_multiple_domains",
+ identity_provider_config_id=str(config.pk),
+ )
+ continue
+ if not domain_ids:
+ logger.warning(
+ "identity_provider_config_has_no_domains",
+ identity_provider_config_id=str(config.pk),
+ )
+ continue
+
+ IdentityProviderConfig.objects.using(db_alias).filter(pk=config.pk).update(
+ saml_relay_state=str(domain_ids[0]),
+ scim_slug=str(domain_ids[0]),
+ )
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1289_identityproviderconfig_relay_state_and_scim_slug"),
+ ]
+
+ operations = [
+ migrations.RunPython(
+ backfill_identity_provider_config_identifiers,
+ reverse_code=migrations.RunPython.noop,
+ ),
+ ]
diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt
index 6db0f3a2bb7f..f2d211f6cee0 100644
--- a/posthog/migrations/max_migration.txt
+++ b/posthog/migrations/max_migration.txt
@@ -1 +1 @@
-1288_backfill_linked_identity_provider_configs
+1290_backfill_identity_provider_config_identifiers
diff --git a/posthog/models/identity_provider_config.py b/posthog/models/identity_provider_config.py
index 9df03722a4b2..0d16e00399a6 100644
--- a/posthog/models/identity_provider_config.py
+++ b/posthog/models/identity_provider_config.py
@@ -53,8 +53,10 @@ class IdentityProviderConfig(ModelActivityMixin, UUIDModel):
saml_entity_id = models.CharField(max_length=512, blank=True, null=True)
saml_acs_url = models.CharField(max_length=512, blank=True, null=True)
saml_x509_cert = models.TextField(blank=True, null=True)
+ saml_relay_state = models.CharField(max_length=36, blank=True, null=True)
# ---- SCIM attributes ----
+ scim_slug = models.CharField(max_length=36, blank=True, null=True, unique=True)
scim_enabled = models.BooleanField(default=False)
scim_bearer_token = models.CharField(
max_length=255, blank=True, null=True, help_text="Hashed bearer token for SCIM authentication"
diff --git a/posthog/models/organization_domain.py b/posthog/models/organization_domain.py
index 1d08d03e103e..5c59ca7288e1 100644
--- a/posthog/models/organization_domain.py
+++ b/posthog/models/organization_domain.py
@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, Any, Optional
from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError
-from django.db import models
+from django.db import models, transaction
from django.db.models.signals import post_delete
from django.dispatch import receiver
from django.utils import timezone
@@ -319,13 +319,25 @@ class OrganizationDomain(ModelActivityMixin, UUIDTModel):
errors = self._validate_identity_provider_config_organization()
if errors:
raise ValidationError(errors)
- super().save(*args, **kwargs)
- if self.identity_provider_config_id is not None:
+
+ with transaction.atomic():
+ super().save(*args, **kwargs)
+ if self.identity_provider_config_id is None:
+ return
+
LinkedIdentityProviderConfig.objects.get_or_create(
organization_domain=self,
identity_provider_config_id=self.identity_provider_config_id,
)
+ config = IdentityProviderConfig.objects.select_for_update().get(pk=self.identity_provider_config_id)
+ identifier = str(self.pk)
+ updates = {field: identifier for field in ("saml_relay_state", "scim_slug") if not getattr(config, field)}
+ if updates:
+ IdentityProviderConfig.objects.filter(pk=config.pk).update(**updates)
+ for field, value in updates.items():
+ setattr(config, field, value)
+
@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 920e288e09ca..83c45fbdf781 100644
--- a/posthog/test/test_identity_provider_config.py
+++ b/posthog/test/test_identity_provider_config.py
@@ -43,6 +43,9 @@ class TestIdentityProviderConfig(BaseTest):
assert LinkedIdentityProviderConfig.objects.filter(
organization_domain=domain, identity_provider_config=config
).exists()
+ config.refresh_from_db()
+ assert config.saml_relay_state == str(domain.pk)
+ assert config.scim_slug == str(domain.pk)
def test_updating_domain_idp_config_creates_link(self):
domain = self._create_domain()
@@ -53,6 +56,9 @@ class TestIdentityProviderConfig(BaseTest):
assert LinkedIdentityProviderConfig.objects.filter(
organization_domain=domain, identity_provider_config=config
).exists()
+ config.refresh_from_db()
+ assert config.saml_relay_state == str(domain.pk)
+ assert config.scim_slug == str(domain.pk)
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