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(cdp): backfill internal event filter sources (#86682)

Ben Whited3590b30c38e
diff --git a/products/cdp/backend/migrations/0004_backfill_internal_event_filter_sources.py b/products/cdp/backend/migrations/0004_backfill_internal_event_filter_sources.py
new file mode 100644
index 000000000000..98a01bd27ef4
--- /dev/null
+++ b/products/cdp/backend/migrations/0004_backfill_internal_event_filter_sources.py
@@ -0,0 +1,54 @@
+from django.db import migrations
+
+BATCH_SIZE = 1000
+
+
+def set_internal_event_sources(apps, schema_editor):
+ HogFunction = apps.get_model("cdp", "HogFunction")
+ db_alias = schema_editor.connection.alias
+ functions_to_update = []
+
+ for row in (
+ HogFunction.objects.using(db_alias)
+ .filter(type="internal_destination")
+ .order_by("pk")
+ .values("pk", "filters")
+ .iterator(chunk_size=BATCH_SIZE)
+ ):
+ filters = row["filters"]
+ # The column is nullable, and rows predating the filter UI hold null or a bare list.
+ if not isinstance(filters, dict):
+ continue
+ events = filters.get("events")
+ has_explicit_events = (
+ isinstance(events, list)
+ and bool(events)
+ and all(
+ isinstance(event, dict) and isinstance(event.get("id"), str) and bool(event["id"].strip())
+ for event in events
+ )
+ )
+ has_unsupported_filters = any(
+ value is not None and (not isinstance(value, list) or bool(value))
+ for value in (filters.get("actions"), filters.get("data_warehouse"))
+ )
+ if not has_explicit_events or has_unsupported_filters or filters.get("source") == "internal-events":
+ continue
+
+ functions_to_update.append(HogFunction(pk=row["pk"], filters={**filters, "source": "internal-events"}))
+ if len(functions_to_update) == BATCH_SIZE:
+ HogFunction.objects.using(db_alias).bulk_update(functions_to_update, ["filters"], batch_size=BATCH_SIZE)
+ functions_to_update = []
+
+ if functions_to_update:
+ HogFunction.objects.using(db_alias).bulk_update(functions_to_update, ["filters"], batch_size=BATCH_SIZE)
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("cdp", "0003_hog_function_drafts"),
+ ]
+
+ operations = [
+ migrations.RunPython(set_internal_event_sources, migrations.RunPython.noop),
+ ]
diff --git a/products/cdp/backend/migrations/max_migration.txt b/products/cdp/backend/migrations/max_migration.txt
index a6a9e9c6c8b9..9faf2e3cf857 100644
--- a/products/cdp/backend/migrations/max_migration.txt
+++ b/products/cdp/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0003_hog_function_drafts
+0004_backfill_internal_event_filter_sources
diff --git a/products/workflows/backend/migrations/0021_backfill_internal_event_triggers.py b/products/workflows/backend/migrations/0021_backfill_internal_event_triggers.py
new file mode 100644
index 000000000000..bcd76eb9e2dd
--- /dev/null
+++ b/products/workflows/backend/migrations/0021_backfill_internal_event_triggers.py
@@ -0,0 +1,63 @@
+from django.db import migrations
+
+BATCH_SIZE = 1000
+
+LEGACY_TRIGGER_TYPE = "slack-message"
+INTERNAL_EVENT_TRIGGER_TYPE = "internal-event"
+SLACK_MESSAGE_RECEIVED_EVENT = "$slack_message_received"
+
+
+def _upgraded(config: object) -> dict | None:
+ if not isinstance(config, dict) or config.get("type") != LEGACY_TRIGGER_TYPE:
+ return None
+ filters = config.get("filters")
+ filters = dict(filters) if isinstance(filters, dict) else {}
+ filters["source"] = "internal-events"
+ filters["events"] = [{"id": SLACK_MESSAGE_RECEIVED_EVENT, "type": "events"}]
+ return {**config, "type": INTERNAL_EVENT_TRIGGER_TYPE, "filters": filters}
+
+
+def set_internal_event_triggers(apps, schema_editor):
+ HogFlow = apps.get_model("workflows", "HogFlow")
+ db_alias = schema_editor.connection.alias
+ flows_to_update = []
+
+ for row in (
+ HogFlow.objects.using(db_alias)
+ .filter(trigger__type=LEGACY_TRIGGER_TYPE)
+ .order_by("pk")
+ .values("pk", "trigger", "actions")
+ .iterator(chunk_size=BATCH_SIZE)
+ ):
+ trigger = _upgraded(row["trigger"])
+ if trigger is None:
+ continue
+
+ # The trigger column is derived from the workflow's single trigger action, so both hold the
+ # legacy type and both have to move together.
+ actions = row["actions"]
+ if isinstance(actions, list):
+ actions = [
+ {**action, "config": _upgraded(action.get("config")) or action.get("config")}
+ if isinstance(action, dict) and action.get("type") == "trigger"
+ else action
+ for action in actions
+ ]
+
+ flows_to_update.append(HogFlow(pk=row["pk"], trigger=trigger, actions=actions))
+ if len(flows_to_update) == BATCH_SIZE:
+ HogFlow.objects.using(db_alias).bulk_update(flows_to_update, ["trigger", "actions"], batch_size=BATCH_SIZE)
+ flows_to_update = []
+
+ if flows_to_update:
+ HogFlow.objects.using(db_alias).bulk_update(flows_to_update, ["trigger", "actions"], batch_size=BATCH_SIZE)
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("workflows", "0020_teamworkflowsconfig_email_sending_tier_and_more"),
+ ]
+
+ operations = [
+ migrations.RunPython(set_internal_event_triggers, migrations.RunPython.noop),
+ ]
diff --git a/products/workflows/backend/migrations/max_migration.txt b/products/workflows/backend/migrations/max_migration.txt
index 79087aafb222..5b5400ec7055 100644
--- a/products/workflows/backend/migrations/max_migration.txt
+++ b/products/workflows/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0020_teamworkflowsconfig_email_sending_tier_and_more
+0021_backfill_internal_event_triggers