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.

fix(cdp): make update_hog_function_code skip uncompilable destinations (#62106)

Daniel Marchuk950f87f2d614
diff --git a/posthog/management/commands/test/test_update_hog_function_code.py b/posthog/management/commands/test/test_update_hog_function_code.py
index 1509e6a3277c..022ee222a7c5 100644
--- a/posthog/management/commands/test/test_update_hog_function_code.py
+++ b/posthog/management/commands/test/test_update_hog_function_code.py
@@ -152,6 +152,44 @@ class TestUpdateHogFunctionCode(BaseTest):
self.assertIn("Updated: 1", output)
self.assertIn("Update completed", output)
+ @patch("posthog.management.commands.update_hog_function_code.compile_hog")
+ def test_update_skips_destinations_that_fail_to_compile(self, mock_compile_hog):
+ """A destination with uncompilable hog is skipped and logged, not fatal to the whole run."""
+ with patch("products.cdp.backend.models.hog_functions.hog_function.reload_hog_functions_on_workers"):
+ bad_function = HogFunction.objects.create(
+ team=self.team,
+ name="Meta Ads Broken",
+ type="destination",
+ template_id="template-meta-ads",
+ description="Broken hog",
+ hog="let res := fetch(f'https://graph.facebook.com/v21.0/{inputs.pixelId}/events BROKEN', {}); return event;",
+ enabled=True,
+ )
+
+ def fake_compile(hog, hog_type):
+ if "BROKEN" in hog:
+ raise Exception("unexpected character '&'")
+ return "compiled_bytecode"
+
+ mock_compile_hog.side_effect = fake_compile
+
+ out = StringIO()
+ call_command("update_hog_function_code", replace_key="meta-ads-api-version-update", stdout=out)
+
+ # The healthy function is still migrated
+ self.meta_ads_function1.refresh_from_db()
+ assert "graph.facebook.com/v25.0/" in self.meta_ads_function1.hog
+
+ # The uncompilable one is left untouched, not partially written
+ bad_function.refresh_from_db()
+ assert "graph.facebook.com/v21.0/" in bad_function.hog
+
+ output = out.getvalue()
+ self.assertIn("Found 3 destinations to process", output)
+ self.assertIn("Updated: 1", output)
+ self.assertIn("Failed: 1", output)
+ self.assertIn(str(bad_function.id), output)
+
@patch("posthog.cdp.validation.compile_hog")
def test_invalid_replace_key(self, mock_compile_hog):
"""Test handling of invalid replace key."""
diff --git a/posthog/management/commands/update_hog_function_code.py b/posthog/management/commands/update_hog_function_code.py
index 76ef9d642ab3..3ee2782c7ba2 100644
--- a/posthog/management/commands/update_hog_function_code.py
+++ b/posthog/management/commands/update_hog_function_code.py
@@ -55,6 +55,7 @@ class Command(BaseCommand):
)
updated_count = 0
+ failed: list[tuple[str, int, bool, str]] = []
total_found = queryset.count()
paginator = Paginator(queryset.order_by("id"), 1000)
@@ -71,15 +72,32 @@ class Command(BaseCommand):
)
for destination in page.object_list:
- if destination.hog and replaceOption["from_string"] in destination.hog:
- destination.hog = destination.hog.replace(replaceOption["from_string"], replaceOption["to_string"])
- destination.bytecode = compile_hog(destination.hog, destination.type)
- updated_count += 1
- if not dry_run:
- destination.save(update_fields=["hog", "bytecode"])
+ if not destination.hog or replaceOption["from_string"] not in destination.hog:
+ continue
+
+ new_hog = destination.hog.replace(replaceOption["from_string"], replaceOption["to_string"])
+ # A single destination with uncompilable (e.g. hand-edited) hog must not abort the whole run.
+ try:
+ new_bytecode = compile_hog(new_hog, destination.type)
+ except Exception as e:
+ failed.append((str(destination.id), destination.team_id, destination.enabled, str(e)))
+ continue
+
+ updated_count += 1
+ if not dry_run:
+ destination.hog = new_hog
+ destination.bytecode = new_bytecode
+ destination.save(update_fields=["hog", "bytecode"])
# Output summary
duration = time.time() - start_time
self.stdout.write(
- self.style.SUCCESS(f"Update completed in {duration:.2f}s. Found: {total_found}, Updated: {updated_count}, ")
+ self.style.SUCCESS(
+ f"Update completed in {duration:.2f}s. Found: {total_found}, Updated: {updated_count}, Failed: {len(failed)}"
+ )
)
+
+ if failed:
+ self.stdout.write(self.style.WARNING(f"{len(failed)} destination(s) failed to compile and were skipped:"))
+ for fn_id, team_id, enabled, error in failed:
+ self.stdout.write(f" id={fn_id} team={team_id} enabled={enabled} error={error}")