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 HogFlowRevision model and migration (#72572)

Harley Alexanderddd71b0081ba
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index f28f9572e94a..0361c521a495 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -170,6 +170,7 @@ rules:
|HealthIssue
|HogFlow
|HogFlowBatchJob
+ |HogFlowRevision
|HogFlowSchedule
|HogFlowTemplate
|HogFunction
@@ -483,6 +484,7 @@ rules:
|HealthIssue
|HogFlow
|HogFlowBatchJob
+ |HogFlowRevision
|HogFlowSchedule
|HogFlowTemplate
|HogFunction
diff --git a/products/workflows/backend/migrations/0011_hogflowrevision.py b/products/workflows/backend/migrations/0011_hogflowrevision.py
new file mode 100644
index 000000000000..21b01ab04249
--- /dev/null
+++ b/products/workflows/backend/migrations/0011_hogflowrevision.py
@@ -0,0 +1,77 @@
+# Generated by Django 5.2.14 on 2026-07-21 12:12
+
+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", "1261_alter_personalapikey_scoped_organizations_and_more"),
+ ("workflows", "0010_email_reputation_snapshot"),
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="HogFlowRevision",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.UUIDT,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ (
+ "version",
+ models.IntegerField(help_text="Workflow version this snapshot was published as."),
+ ),
+ (
+ "content",
+ models.JSONField(
+ help_text="Full snapshot of the workflow's content fields (actions, edges, trigger, etc.) at this version."
+ ),
+ ),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ (
+ "created_by",
+ models.ForeignKey(
+ blank=True,
+ db_constraint=False,
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ to=settings.AUTH_USER_MODEL,
+ ),
+ ),
+ (
+ "hog_flow",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="revisions",
+ to="workflows.hogflow",
+ ),
+ ),
+ (
+ "team",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ to="posthog.team",
+ ),
+ ),
+ ],
+ options={
+ "constraints": [
+ models.UniqueConstraint(
+ fields=("hog_flow", "version"),
+ name="unique_hogflow_revision_version",
+ )
+ ],
+ },
+ ),
+ ]
diff --git a/products/workflows/backend/migrations/max_migration.txt b/products/workflows/backend/migrations/max_migration.txt
index 52bbaf43cfc1..e3bbda0289dd 100644
--- a/products/workflows/backend/migrations/max_migration.txt
+++ b/products/workflows/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0010_email_reputation_snapshot
+0011_hogflowrevision
diff --git a/products/workflows/backend/models/__init__.py b/products/workflows/backend/models/__init__.py
index 4001c992589d..7c979faa1992 100644
--- a/products/workflows/backend/models/__init__.py
+++ b/products/workflows/backend/models/__init__.py
@@ -2,6 +2,7 @@ from .email_reputation import EmailReputationSnapshot
from .hog_flow.hog_flow import HogFlow
from .hog_flow.hog_flow_template import HogFlowTemplate
from .hog_flow_batch_job import HogFlowBatchJob
+from .hog_flow_revision import HogFlowRevision
from .hog_flow_schedule.hog_flow_schedule import HogFlowSchedule
from .team_workflows_config import TeamWorkflowsConfig
@@ -9,6 +10,7 @@ __all__ = [
"EmailReputationSnapshot",
"HogFlow",
"HogFlowBatchJob",
+ "HogFlowRevision",
"HogFlowSchedule",
"HogFlowTemplate",
"TeamWorkflowsConfig",
diff --git a/products/workflows/backend/models/hog_flow_revision.py b/products/workflows/backend/models/hog_flow_revision.py
new file mode 100644
index 000000000000..120a2c06e115
--- /dev/null
+++ b/products/workflows/backend/models/hog_flow_revision.py
@@ -0,0 +1,39 @@
+from typing import Any
+
+from django.db import models
+
+from posthog.models.scoping.root_mixin import TeamScopedRootMixin
+from posthog.models.utils import UUIDTModel
+
+
+class HogFlowRevision(TeamScopedRootMixin, UUIDTModel):
+ """Append-only snapshot of a workflow's live content, written whenever the live config
+ changes. Rollback copies a snapshot back into the draft; workers never read this table."""
+
+ class Meta:
+ constraints = [
+ models.UniqueConstraint(fields=["hog_flow", "version"], name="unique_hogflow_revision_version"),
+ ]
+
+ # db_constraint=False on team/created_by: a real FK constraint to a hot table (posthog_team,
+ # posthog_user) takes a parent-table lock on creation; enforcement stays app-level.
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, db_constraint=False)
+ hog_flow = models.ForeignKey("workflows.HogFlow", on_delete=models.CASCADE, related_name="revisions")
+ version = models.IntegerField(help_text="Workflow version this snapshot was published as.")
+ content = models.JSONField(
+ help_text="Full snapshot of the workflow's content fields (actions, edges, trigger, etc.) at this version."
+ )
+ created_by = models.ForeignKey(
+ "posthog.User", on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False
+ )
+ created_at = models.DateTimeField(auto_now_add=True)
+
+ def save(self, *args: Any, **kwargs: Any) -> None:
+ # A revision's tenant scope always mirrors its workflow's. A mismatched (team, hog_flow)
+ # pair would leak the revision into the wrong team's history — fail-closed reads filter on
+ # this row's team_id, not the workflow's.
+ self.team_id = self.hog_flow.team_id
+ super().save(*args, **kwargs)
+
+ def __str__(self) -> str:
+ return f"HogFlowRevision {self.hog_flow_id} v{self.version}"
diff --git a/products/workflows/backend/test/test_hog_flow_revision_model.py b/products/workflows/backend/test/test_hog_flow_revision_model.py
new file mode 100644
index 000000000000..b44cb4f7b2e9
--- /dev/null
+++ b/products/workflows/backend/test/test_hog_flow_revision_model.py
@@ -0,0 +1,25 @@
+from posthog.test.base import BaseTest
+
+from posthog.models.organization import Organization
+from posthog.models.scoping import team_scope
+from posthog.models.team.team import Team
+
+from products.workflows.backend.models import HogFlow, HogFlowRevision
+
+
+class TestHogFlowRevisionModel(BaseTest):
+ def test_save_derives_team_from_workflow(self):
+ # A revision written with a mismatched team must not leak into that team's history — the
+ # fail-closed manager filters on the revision's own team_id, so save() pins it to the flow's.
+ other_org = Organization.objects.create(name="other")
+ other_team = Team.objects.create(organization=other_org, name="other team")
+ flow = HogFlow.objects.create(team=self.team, name="Flow")
+
+ with team_scope(other_team.id):
+ revision = HogFlowRevision.objects.create(team=other_team, hog_flow=flow, version=1, content={})
+
+ assert revision.team_id == self.team.id
+ with team_scope(self.team.id):
+ assert HogFlowRevision.objects.filter(hog_flow=flow).count() == 1
+ with team_scope(other_team.id):
+ assert HogFlowRevision.objects.count() == 0