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(meta_ads): refresh stale db connection before integration lookup (#63778)

Tom Owersc9720e0b6618
diff --git a/posthog/temporal/data_imports/sources/meta_ads/test_meta_ads.py b/posthog/temporal/data_imports/sources/meta_ads/test_meta_ads.py
index 686d42eee24c..6e7ade32057b 100644
--- a/posthog/temporal/data_imports/sources/meta_ads/test_meta_ads.py
+++ b/posthog/temporal/data_imports/sources/meta_ads/test_meta_ads.py
@@ -5,7 +5,8 @@ import pytest
from unittest import mock
from posthog.temporal.data_imports.sources.common.resumable import ResumableSourceManager
-from posthog.temporal.data_imports.sources.meta_ads import meta_ads
+from posthog.temporal.data_imports.sources.generated_configs import MetaAdsSourceConfig
+from posthog.temporal.data_imports.sources.meta_ads import meta_ads as meta_ads_module
from posthog.temporal.data_imports.sources.meta_ads.meta_ads import (
META_AUTH_ERROR_MESSAGE,
PAGE_LIMIT_FALLBACK_SIZES,
@@ -16,6 +17,7 @@ from posthog.temporal.data_imports.sources.meta_ads.meta_ads import (
_next_smaller_limit,
_override_limit,
_strip_access_token,
+ get_integration,
)
from posthog.temporal.data_imports.sources.meta_ads.source import MetaAdsSource
@@ -819,28 +821,30 @@ class TestNonRetryableErrors:
class TestGetIntegration:
def test_refreshes_stale_db_connection_before_query(self, monkeypatch) -> None:
- # The ORM read runs lazily inside `get_rows` on a worker thread whose pooled
- # Django connection may have been closed server-side, surfacing as
+ # `get_integration` runs lazily inside `get_rows` on a worker thread whose
+ # pooled Django connection may have been closed server-side, surfacing as
# `OperationalError: the connection is closed`. We must drop the stale
# connection before querying, so the read happens on a fresh connection.
calls: list[str] = []
- monkeypatch.setattr(meta_ads, "close_old_connections", lambda: calls.append("close_old_connections"))
+ monkeypatch.setattr(meta_ads_module, "close_old_connections", lambda: calls.append("close_old_connections"))
integration = mock.MagicMock()
- integration.errors = None
def fake_get(*args: Any, **kwargs: Any) -> mock.MagicMock:
calls.append("Integration.objects.get")
return integration
- monkeypatch.setattr(meta_ads.Integration.objects, "get", fake_get)
- monkeypatch.setattr(meta_ads, "MetaAdsIntegration", lambda i: mock.MagicMock(integration=i))
+ monkeypatch.setattr(meta_ads_module.Integration.objects, "get", fake_get)
- config = mock.MagicMock()
- config.meta_ads_integration_id = 1
+ meta_ads_integration = mock.MagicMock()
+ meta_ads_integration.integration = integration
+ integration.errors = None
+ monkeypatch.setattr(meta_ads_module, "MetaAdsIntegration", lambda _integration: meta_ads_integration)
- result = meta_ads.get_integration(config, team_id=1)
+ config = MetaAdsSourceConfig(account_id="act_123", meta_ads_integration_id=42)
+ result = get_integration(config, team_id=1)
assert calls == ["close_old_connections", "Integration.objects.get"]
+ meta_ads_integration.refresh_access_token.assert_called_once()
assert result is integration