: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(messaging): add provider import run model (#124425)
Harley Alexander3317e98a537e
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yamlindex 212b0afc496e..5070bd99772b 100644--- a/.semgrep/rules/security/idor-team-scoped-models.yaml+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml@@ -336,6 +336,7 @@ rules:|PluginConfig|ProductBrief|ProductTour+ |ProviderImportRun|ProjectSecretAPIKey|PropertyAccessControl|PullRequestAudience@@ -822,6 +823,7 @@ rules:|PluginConfig|ProductBrief|ProductTour+ |ProviderImportRun|ProjectSecretAPIKey|PropertyAccessControl|PullRequestAudiencediff --git a/products/messaging/backend/migrations/0009_provider_import_run.py b/products/messaging/backend/migrations/0009_provider_import_run.pynew file mode 100644index 000000000000..41a6fc105604--- /dev/null+++ b/products/messaging/backend/migrations/0009_provider_import_run.py@@ -0,0 +1,105 @@+# Generated by Django 5.2.18 on 2026-10-27 20:01++import django.db.models.deletion+from django.conf import settings+from django.db import migrations, models++import posthog.uuidt++import products.messaging.backend.models.provider_import_run+++class Migration(migrations.Migration):+ dependencies = [+ ("messaging", "0008_messagetemplate_sensitive"),+ ("posthog", "1413_event_filter_pipeline_override"),+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),+ ]++ operations = [+ migrations.CreateModel(+ name="ProviderImportRun",+ fields=[+ (+ "id",+ models.UUIDField(+ default=posthog.uuidt.uuid7,+ editable=False,+ primary_key=True,+ serialize=False,+ ),+ ),+ ("created_at", models.DateTimeField(auto_now_add=True)),+ ("started_at", models.DateTimeField(blank=True, null=True)),+ ("finished_at", models.DateTimeField(blank=True, null=True)),+ (+ "provider",+ models.CharField(+ choices=products.messaging.backend.models.provider_import_run.import_provider_choices,+ max_length=40,+ ),+ ),+ ("steps", models.JSONField(default=list)),+ (+ "status",+ models.CharField(+ choices=[+ ("queued", "Waiting to start"),+ ("running", "Importing"),+ ("completed", "Imported"),+ ("failed", "Failed"),+ ],+ default="queued",+ max_length=16,+ ),+ ),+ ("progress", models.JSONField(default=dict)),+ (+ "created_by",+ models.ForeignKey(+ blank=True,+ db_constraint=False,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="+",+ to=settings.AUTH_USER_MODEL,+ ),+ ),+ (+ "integration",+ models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="+",+ to="posthog.integration",+ ),+ ),+ (+ "team",+ models.ForeignKey(+ db_constraint=False,+ on_delete=django.db.models.deletion.CASCADE,+ related_name="+",+ to="posthog.team",+ ),+ ),+ ],+ options={+ "db_table": "posthog_messaging_provider_import_run",+ "indexes": [+ models.Index(+ fields=["team", "-created_at"],+ name="pmsg_import_run_by_created",+ )+ ],+ "constraints": [+ models.UniqueConstraint(+ condition=models.Q(("status__in", ["queued", "running"])),+ fields=("team", "provider"),+ name="unique_active_provider_import_run",+ )+ ],+ },+ ),+ ]diff --git a/products/messaging/backend/migrations/max_migration.txt b/products/messaging/backend/migrations/max_migration.txtindex 00e248f74311..e548ada71880 100644--- a/products/messaging/backend/migrations/max_migration.txt+++ b/products/messaging/backend/migrations/max_migration.txt@@ -1 +1 @@-0008_messagetemplate_sensitive+0009_provider_import_rundiff --git a/products/messaging/backend/models/__init__.py b/products/messaging/backend/models/__init__.pyindex d79860723e8e..2e0f1492f947 100644--- a/products/messaging/backend/models/__init__.py+++ b/products/messaging/backend/models/__init__.py@@ -3,6 +3,7 @@ from .message_preferences import ALL_MESSAGE_PREFERENCE_CATEGORY_ID, MessageRecifrom .message_suppression import MessageSuppression, SuppressionSourcefrom .message_template import MessageTemplatefrom .optout_sync_config import OptOutSyncConfig+from .provider_import_run import ProviderImportRun__all__ = ["ALL_MESSAGE_PREFERENCE_CATEGORY_ID",@@ -13,5 +14,6 @@ __all__ = ["MessageTemplate","OptOutSyncConfig","PreferenceStatus",+ "ProviderImportRun","SuppressionSource",]diff --git a/products/messaging/backend/models/provider_import_run.py b/products/messaging/backend/models/provider_import_run.pynew file mode 100644index 000000000000..f5dca975215f--- /dev/null+++ b/products/messaging/backend/models/provider_import_run.py@@ -0,0 +1,75 @@+from django.db import models+from django.db.models import Q+from django.utils.functional import Promise++from posthog.models.scoping.root_mixin import TeamScopedRootMixin+from posthog.models.utils import UUIDModel+++class ImportProviderKey(models.TextChoices):+ CUSTOMERIO = "customerio", "Customer.io"+++def import_provider_choices() -> list[tuple[str, str | Promise]]:+ # Callable so adding a provider doesn't generate a no-op migration.+ return list(ImportProviderKey.choices)+++class ProviderImportStatus(models.TextChoices):+ QUEUED = "queued", "Waiting to start"+ RUNNING = "running", "Importing"+ COMPLETED = "completed", "Imported"+ FAILED = "failed", "Failed"+++ACTIVE_IMPORT_STATUSES = (ProviderImportStatus.QUEUED, ProviderImportStatus.RUNNING)+++class ImportStepStatus(models.TextChoices):+ PENDING = "pending", "Not started"+ RUNNING = "running", "Importing"+ COMPLETED = "completed", "Imported"+ FAILED = "failed", "Failed"+++class ProviderImportRun(TeamScopedRootMixin, UUIDModel):+ """One background import of opt-outs, suppressions and other messaging data from another provider.++ The Temporal workflow runs the selected steps in order. `progress` holds the status, counts and resume point of+ each step, so a retried step continues from its last saved page and the UI can show progress.+ """++ # db_constraint=False on these hot-table FKs so CreateModel takes no lock on posthog_team / posthog_user.+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False, related_name="+")+ created_by = models.ForeignKey(+ "posthog.User", on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False, related_name="+"+ )+ created_at = models.DateTimeField(auto_now_add=True)+ started_at = models.DateTimeField(null=True, blank=True)+ finished_at = models.DateTimeField(null=True, blank=True)++ provider = models.CharField(max_length=40, choices=import_provider_choices)+ # The stored credentials the steps read. Null when someone deleted them, which fails the remaining steps.+ integration = models.ForeignKey(+ "posthog.Integration", on_delete=models.SET_NULL, null=True, blank=True, related_name="+"+ )+ # Step keys in the order they run.+ steps = models.JSONField(default=list)+ status = models.CharField(max_length=16, choices=ProviderImportStatus.choices, default=ProviderImportStatus.QUEUED)+ # Step key -> {"status", "processed", "imported", "warnings", "last_error", "checkpoint", "attempt"}.+ progress = models.JSONField(default=dict)++ class Meta:+ db_table = "posthog_messaging_provider_import_run"+ indexes = [models.Index(fields=["team", "-created_at"], name="pmsg_import_run_by_created")]+ constraints = [+ # Two runs against one provider would share its API rate limit and write the same rows.+ models.UniqueConstraint(+ fields=["team", "provider"],+ condition=Q(status__in=["queued", "running"]),+ name="unique_active_provider_import_run",+ ),+ ]++ def __str__(self) -> str:+ return f"ProviderImportRun {self.team_id}/{self.provider}/{self.status}"