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(workflows): add a table for push token import jobs (#129099)

Daniel Marchuk7f688fbaf260
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index 75a02142a40b..a4cecedfcd05 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -431,6 +431,7 @@ rules:
|AnomalyScoringState
|AnomalyScoringDueInsight
+ |PushTokenImport
|SignalScratchpad
|SignalProjectProfile
|SingleSessionSummary
@@ -931,6 +932,7 @@ rules:
|AnomalyScoringState
|AnomalyScoringDueInsight
+ |PushTokenImport
|SignalScratchpad
|SignalProjectProfile
|SingleSessionSummary
diff --git a/products/workflows/backend/facade/enums.py b/products/workflows/backend/facade/enums.py
index 7b38ec2f132d..1f1065e2e150 100644
--- a/products/workflows/backend/facade/enums.py
+++ b/products/workflows/backend/facade/enums.py
@@ -206,3 +206,18 @@ class EmailDomainSetupStepState(LabeledStrEnum):
DONE = "done", "Done"
PENDING = "pending", "Pending"
FAILED = "failed", "Failed"
+
+
+class PushTokenImportStatus(LabeledStrEnum):
+ RUNNING = "running", "Import running"
+ COMPLETED = "completed", "Import completed"
+ CANCELLED = "cancelled", "Import cancelled"
+ FAILED = "failed", "Import failed"
+
+
+class PushTokenImportRowFailureReason(LabeledStrEnum):
+ INVALID_ROW = "invalid_row", "The line is missing a value or a value is too long"
+ UNKNOWN_PERSON = "unknown_person", "No person has this distinct ID"
+ TOKEN_MISMATCH = "token_mismatch", "The token does not fit this channel"
+ NOT_STORED = "not_stored", "The device was not stored"
+ PERSON_PROCESSING_DISABLED = "person_processing_disabled", "Person processing is off for this person"
diff --git a/products/workflows/backend/migrations/0042_push_token_import.py b/products/workflows/backend/migrations/0042_push_token_import.py
new file mode 100644
index 000000000000..d932e85d6643
--- /dev/null
+++ b/products/workflows/backend/migrations/0042_push_token_import.py
@@ -0,0 +1,112 @@
+# Generated by Django 6.1.2 on 2026-11-03 10:55
+
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+import posthog.uuidt
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1418_alter_team_timezone"),
+ ("workflows", "0041_hogflowbatchjob_partially_failed_status"),
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="PushTokenImport",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.uuid7,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ ("integration_id", models.BigIntegerField()),
+ (
+ "status",
+ models.CharField(
+ choices=[
+ ("running", "Import running"),
+ ("completed", "Import completed"),
+ ("cancelled", "Import cancelled"),
+ ("failed", "Import failed"),
+ ],
+ default="running",
+ max_length=20,
+ ),
+ ),
+ ("create_missing_persons", models.BooleanField(default=False)),
+ ("total_devices", models.PositiveIntegerField(default=0)),
+ ("processed_devices", models.PositiveIntegerField(default=0)),
+ ("imported_devices", models.PositiveIntegerField(default=0)),
+ ("failed_devices", models.PositiveIntegerField(default=0)),
+ ("duplicate_lines", models.PositiveIntegerField(default=0)),
+ (
+ "failures",
+ models.JSONField(
+ blank=True,
+ default=list,
+ help_text="The first failed lines: a list of {line, distinct_id, device_token_suffix, reason, detail}.",
+ ),
+ ),
+ (
+ "unknown_person_lines",
+ models.JSONField(
+ blank=True,
+ default=list,
+ help_text="Every line whose distinct ID had no person, so those devices can be imported again.",
+ ),
+ ),
+ ("error", models.TextField(blank=True, default="")),
+ ("resumed_times", models.PositiveSmallIntegerField(default=0)),
+ (
+ "storage_key",
+ models.CharField(blank=True, default="", max_length=400),
+ ),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ ("updated_at", models.DateTimeField(auto_now=True)),
+ ("finished_at", models.DateTimeField(blank=True, null=True)),
+ (
+ "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,
+ ),
+ ),
+ (
+ "team",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to="posthog.team",
+ ),
+ ),
+ ],
+ options={
+ "indexes": [
+ models.Index(
+ fields=["team", "integration_id", "-created_at"],
+ name="push_token_import_channel_idx",
+ )
+ ],
+ "constraints": [
+ models.UniqueConstraint(
+ condition=models.Q(("status", "running")),
+ fields=("team", "integration_id"),
+ name="push_token_import_one_running",
+ )
+ ],
+ },
+ ),
+ ]
diff --git a/products/workflows/backend/migrations/max_migration.txt b/products/workflows/backend/migrations/max_migration.txt
index f19ce8868624..2d1293ffa428 100644
--- a/products/workflows/backend/migrations/max_migration.txt
+++ b/products/workflows/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0041_hogflowbatchjob_partially_failed_status
+0042_push_token_import
diff --git a/products/workflows/backend/models/__init__.py b/products/workflows/backend/models/__init__.py
index 7bfb7e8871b6..375c2d57d961 100644
--- a/products/workflows/backend/models/__init__.py
+++ b/products/workflows/backend/models/__init__.py
@@ -4,6 +4,7 @@ from .hog_flow_batch_job import HogFlowBatchJob
from .hog_flow_optimization import HogFlowOptimization
from .hog_flow_revision import HogFlowRevision
from .hog_flow_schedule.hog_flow_schedule import HogFlowSchedule
+from .push_token_import import PushTokenImport
from .team_workflows_config import TeamWorkflowsConfig
from .workflow_idea import WorkflowIdea
from .workflow_import import WorkflowImport
@@ -16,6 +17,7 @@ __all__ = [
"HogFlowRevision",
"HogFlowSchedule",
"HogFlowTemplate",
+ "PushTokenImport",
"TeamWorkflowsConfig",
"WorkflowIdea",
"WorkflowImport",
diff --git a/products/workflows/backend/models/push_token_import.py b/products/workflows/backend/models/push_token_import.py
new file mode 100644
index 000000000000..314a561a961b
--- /dev/null
+++ b/products/workflows/backend/models/push_token_import.py
@@ -0,0 +1,58 @@
+from django.db import models
+
+from posthog.models.scoping.root_mixin import TeamScopedRootMixin
+from posthog.models.utils import UUIDModel
+
+from products.workflows.backend.facade.enums import PushTokenImportStatus
+
+
+class PushTokenImport(TeamScopedRootMixin, UUIDModel):
+ """One uploaded file of device tokens, imported into a push channel in the background."""
+
+ Status = PushTokenImportStatus
+
+ class Meta:
+ indexes = [models.Index(fields=["team", "integration_id", "-created_at"], name="push_token_import_channel_idx")]
+ constraints = [
+ models.UniqueConstraint(
+ fields=["team", "integration_id"],
+ condition=models.Q(status="running"),
+ name="push_token_import_one_running",
+ )
+ ]
+
+ # db_constraint=False: a real FK constraint to a hot table takes a parent-table lock on creation.
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False, related_name="+")
+ # Integration rows are read through the integrations SDK, which hands out ids, not model instances.
+ integration_id = models.BigIntegerField()
+ status = models.CharField(max_length=20, choices=Status.choices, default=Status.RUNNING.value)
+ create_missing_persons = models.BooleanField(default=False)
+ total_devices = models.PositiveIntegerField(default=0)
+ processed_devices = models.PositiveIntegerField(default=0)
+ imported_devices = models.PositiveIntegerField(default=0)
+ failed_devices = models.PositiveIntegerField(default=0)
+ duplicate_lines = models.PositiveIntegerField(default=0)
+ failures = models.JSONField(
+ default=list,
+ blank=True,
+ help_text="The first failed lines: a list of {line, distinct_id, device_token_suffix, reason, detail}.",
+ )
+ unknown_person_lines = models.JSONField(
+ default=list,
+ blank=True,
+ help_text="Every line whose distinct ID had no person, so those devices can be imported again.",
+ )
+ error = models.TextField(blank=True, default="")
+ # Times the stall check queued the task again after a worker stopped mid-import.
+ resumed_times = models.PositiveSmallIntegerField(default=0)
+ # The encrypted devices in object storage. Cleared once nothing can read them again.
+ storage_key = models.CharField(max_length=400, blank=True, default="")
+ created_at = models.DateTimeField(auto_now_add=True)
+ updated_at = models.DateTimeField(auto_now=True)
+ finished_at = models.DateTimeField(null=True, blank=True)
+ created_by = models.ForeignKey(
+ "posthog.User", on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False, related_name="+"
+ )
+
+ def __str__(self) -> str:
+ return f"PushTokenImport {self.team_id}/{self.integration_id}/{self.id}"