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(billing): stop stripe enrichment renaming salesforce accounts (#114410)

Abhischek8139b9360f1f
diff --git a/ee/billing/salesforce_enrichment/constants.py b/ee/billing/salesforce_enrichment/constants.py
index 60115b30ac4e..5b613d282414 100644
--- a/ee/billing/salesforce_enrichment/constants.py
+++ b/ee/billing/salesforce_enrichment/constants.py
@@ -156,8 +156,8 @@ SALESFORCE_MOMENTUM_MAX = 99_999.99
STRIPE_ENRICHMENT_PAGE_SIZE: int = 5_000
+# Account.Name is not mapped, because the billing customer's name is the organization's name and not the company's.
STRIPE_ENRICHMENT_FIELD_MAPPINGS: dict[str, str] = {
- "billing_customer_name": "Name",
"stripe_customer_id": "Stripe_id__c",
"address_city": "BillingCity",
"address_state": "BillingState",
diff --git a/ee/billing/salesforce_enrichment/stripe_signals.py b/ee/billing/salesforce_enrichment/stripe_signals.py
index e0e71e378bbd..b66e72e69c8f 100644
--- a/ee/billing/salesforce_enrichment/stripe_signals.py
+++ b/ee/billing/salesforce_enrichment/stripe_signals.py
@@ -1,8 +1,8 @@
"""Stripe / billing customer signals read from the duckgres Postgres DWH."""
import datetime as dt
-from dataclasses import dataclass
+from posthog.dataclasses import frozen
from posthog.temporal.common.logger import get_logger
from .duckgres_client import duckgres_cursor
@@ -10,13 +10,12 @@ from .duckgres_client import duckgres_cursor
LOGGER = get_logger(__name__)
-@dataclass
+@frozen
class StripeSignals:
- """Stripe + billing customer data for a single PostHog organization."""
+ """Stripe customer data for a single PostHog organization."""
posthog_organization_id: str
billing_customer_id: str
- billing_customer_name: str | None
stripe_customer_id: str | None
address_line_1: str | None
address_line_2: str | None
@@ -44,7 +43,6 @@ WITH posthog_customer AS (
cts._fivetran_synced AS mapping_synced_at,
bc.id AS billing_customer_id,
bc.organization_id AS posthog_organization_id,
- bc.name AS billing_customer_name,
bc._fivetran_synced AS billing_customer_synced_at
FROM ducklake.billing_public.billing_customertostripecustomer cts
JOIN ducklake.billing_public.billing_customer bc
@@ -56,7 +54,6 @@ enriched AS (
SELECT
pc.posthog_organization_id,
pc.billing_customer_id,
- pc.billing_customer_name,
sc.id AS stripe_customer_id,
sc.address_line_1,
sc.address_line_2,
@@ -119,7 +116,6 @@ def fetch_stripe_signals(
StripeSignals(
posthog_organization_id=str(row["posthog_organization_id"]),
billing_customer_id=str(row["billing_customer_id"]),
- billing_customer_name=row["billing_customer_name"],
stripe_customer_id=row["stripe_customer_id"],
address_line_1=row["address_line_1"],
address_line_2=row["address_line_2"],
diff --git a/ee/billing/test/test_stripe_signals.py b/ee/billing/test/test_stripe_signals.py
index 56f822dbff94..b8b374b511a7 100644
--- a/ee/billing/test/test_stripe_signals.py
+++ b/ee/billing/test/test_stripe_signals.py
@@ -16,7 +16,6 @@ def _row(
return {
"posthog_organization_id": org_id,
"billing_customer_id": "bc-1",
- "billing_customer_name": "Acme Inc",
"stripe_customer_id": stripe_id,
"address_line_1": "1 Main St",
"address_line_2": None,
@@ -62,7 +61,6 @@ class TestStripeSignalsDataClass(TestCase):
signals = StripeSignals(
posthog_organization_id="org-1",
billing_customer_id="bc-1",
- billing_customer_name="Acme",
stripe_customer_id="cus_1",
address_line_1="1 Main St",
address_line_2=None,
@@ -136,7 +134,6 @@ class TestFetchStripeSignals(TestCase):
{
"posthog_organization_id": "org-1",
"billing_customer_id": "bc-1",
- "billing_customer_name": "Acme",
"stripe_customer_id": "cus_1",
"address_line_1": "1 Main St",
"address_line_2": "Suite 200",
diff --git a/posthog/temporal/salesforce_enrichment/stripe_workflow.py b/posthog/temporal/salesforce_enrichment/stripe_workflow.py
index 89c63f0bfb68..335f35c5f1ef 100644
--- a/posthog/temporal/salesforce_enrichment/stripe_workflow.py
+++ b/posthog/temporal/salesforce_enrichment/stripe_workflow.py
@@ -1,10 +1,9 @@
"""Salesforce stripe/billing enrichment workflow.
-Pushes Stripe customer data (customer id + billing address) and internal
-``billing_customer.name`` onto Salesforce Account records. Runs daily with a
-Redis-backed high-water mark so only rows that changed in the duckgres DWH since
-the last successful run are touched. The first run (or any run after watermark
-eviction) performs a full backfill.
+Pushes Stripe customer data (customer id + billing address) onto Salesforce
+Account records. Runs daily with a Redis-backed high-water mark so only rows
+that changed in the duckgres DWH since the last successful run are touched. The
+first run (or any run after watermark eviction) performs a full backfill.
"""
import json
@@ -169,7 +168,7 @@ async def enrich_stripe_page_activity(inputs: EnrichStripePageInputs) -> EnrichS
Matching Account rows are looked up by ``Posthog_Org_ID__c`` — the same join
key the usage enrichment workflow uses. Rows with no matching account are
- counted and skipped
+ counted and skipped, and so are rows with no Stripe data to push.
"""
async with Heartbeater() as heartbeater:
close_old_connections()
@@ -226,11 +225,14 @@ async def enrich_stripe_page_activity(inputs: EnrichStripePageInputs) -> EnrichS
skipped_no_account = sum(1 for org_id in all_org_ids if org_id not in org_to_account_id)
- update_records = [
+ prepared_records = [
prepare_stripe_update_record(org_to_account_id[org_id], signals_by_org[org_id])
for org_id in all_org_ids
if org_id in org_to_account_id
]
+ # An Id-only record changes no field, but patching it still touches the Account and runs its automation.
+ update_records = [record for record in prepared_records if len(record) > 1]
+ skipped_no_stripe_data = len(prepared_records) - len(update_records)
errors: list[str] = []
updated = 0
@@ -252,6 +254,7 @@ async def enrich_stripe_page_activity(inputs: EnrichStripePageInputs) -> EnrichS
rows_fetched=len(signals_rows),
updated=updated,
skipped_no_account=skipped_no_account,
+ skipped_no_stripe_data=skipped_no_stripe_data,
error_count=len(errors),
)
@@ -267,7 +270,7 @@ async def enrich_stripe_page_activity(inputs: EnrichStripePageInputs) -> EnrichS
@workflow.defn(name="salesforce-stripe-enrichment")
class SalesforceStripeEnrichmentWorkflow(PostHogWorkflow):
- """Incrementally push Stripe + billing customer data to Salesforce Accounts."""
+ """Incrementally push Stripe customer ids and billing addresses to Salesforce Accounts."""
@staticmethod
def parse_inputs(inputs: list[str]) -> StripeEnrichmentInputs:
diff --git a/posthog/temporal/schedule.py b/posthog/temporal/schedule.py
index f0e11eac96aa..43488e828f39 100644
--- a/posthog/temporal/schedule.py
+++ b/posthog/temporal/schedule.py
@@ -304,8 +304,8 @@ async def create_salesforce_usage_enrichment_schedule(client: Client):
async def create_salesforce_stripe_enrichment_schedule(client: Client):
"""Create or update the schedule for the Salesforce stripe enrichment workflow.
- Runs daily at 4 AM UTC to push Stripe customer data and billing customer
- names to Salesforce Accounts. The workflow is incremental via a Redis
+ Runs daily at 4 AM UTC to push Stripe customer ids and billing addresses
+ to Salesforce Accounts. The workflow is incremental via a Redis
watermark, so a long backfill run is only expected on the first execution;
``SKIP`` prevents the next day's run from starting while a backfill is still
in progress.
diff --git a/posthog/temporal/tests/salesforce_enrichment/test_stripe_workflow.py b/posthog/temporal/tests/salesforce_enrichment/test_stripe_workflow.py
index 9cf3e70df38a..b2ede1f1c256 100644
--- a/posthog/temporal/tests/salesforce_enrichment/test_stripe_workflow.py
+++ b/posthog/temporal/tests/salesforce_enrichment/test_stripe_workflow.py
@@ -35,7 +35,6 @@ def _signals(
return StripeSignals(
posthog_organization_id=org_id,
billing_customer_id="bc-1",
- billing_customer_name="Acme Inc",
stripe_customer_id=stripe_id,
address_line_1=line1,
address_line_2=line2,
@@ -47,6 +46,21 @@ def _signals(
)
+def _signals_with_no_stripe_data(org_id: str = "org-1") -> StripeSignals:
+ return StripeSignals(
+ posthog_organization_id=org_id,
+ billing_customer_id="bc-1",
+ stripe_customer_id=None,
+ address_line_1=None,
+ address_line_2=None,
+ address_city=None,
+ address_state=None,
+ address_postal_code=None,
+ address_country=None,
+ last_changed_at=dt.datetime(2026, 4, 10, tzinfo=dt.UTC),
+ )
+
+
async def mock_to_thread(fn, *args, **kwargs):
return fn(*args, **kwargs)
@@ -68,30 +82,18 @@ class TestPrepareStripeUpdateRecord(SimpleTestCase):
def test_full_record(self):
record = prepare_stripe_update_record("001ABC", _signals(line2="Suite 200"))
- assert record["Id"] == "001ABC"
- assert record["Name"] == "Acme Inc"
- assert record["Stripe_id__c"] == "cus_1"
- assert record["BillingStreet"] == "1 Main St\nSuite 200"
- assert record["BillingCity"] == "SF"
- assert record["BillingState"] == "CA"
- assert record["BillingPostalCode"] == "94107"
- assert record["BillingCountry"] == "US"
+ assert record == {
+ "Id": "001ABC",
+ "Stripe_id__c": "cus_1",
+ "BillingStreet": "1 Main St\nSuite 200",
+ "BillingCity": "SF",
+ "BillingState": "CA",
+ "BillingPostalCode": "94107",
+ "BillingCountry": "US",
+ }
def test_none_values_omitted(self):
- signals = StripeSignals(
- posthog_organization_id="org-1",
- billing_customer_id="bc-1",
- billing_customer_name=None,
- stripe_customer_id=None,
- address_line_1=None,
- address_line_2=None,
- address_city=None,
- address_state=None,
- address_postal_code=None,
- address_country=None,
- last_changed_at=dt.datetime(2026, 4, 10, tzinfo=dt.UTC),
- )
- record = prepare_stripe_update_record("001ABC", signals)
+ record = prepare_stripe_update_record("001ABC", _signals_with_no_stripe_data())
assert record == {"Id": "001ABC"}
@@ -178,40 +180,73 @@ class TestEnrichStripePageActivity(SimpleTestCase):
mock_bulk.assert_called_once()
sent_records = mock_bulk.call_args[0][1]
assert len(sent_records) == 2
- assert sent_records[0]["Name"] == "Acme Inc"
+ assert sent_records[0]["Stripe_id__c"] == "cus_1"
+ @parameterized.expand(
+ [
+ (
+ "no_matching_account",
+ [_signals(org_id="org-1"), _signals(org_id="org-missing")],
+ {"org-1": "001ABC"},
+ ["001ABC"],
+ 1,
+ ),
+ (
+ "no_stripe_data",
+ [_signals(org_id="org-1"), _signals_with_no_stripe_data(org_id="org-2")],
+ {"org-1": "001ABC", "org-2": "001DEF"},
+ ["001ABC"],
+ 0,
+ ),
+ (
+ "whole_page_without_stripe_data",
+ [_signals_with_no_stripe_data(org_id="org-1"), _signals_with_no_stripe_data(org_id="org-2")],
+ {"org-1": "001ABC", "org-2": "001DEF"},
+ [],
+ 0,
+ ),
+ ]
+ )
@pytest.mark.asyncio
@patch(f"{WORKFLOW_MODULE}.Heartbeater")
- @patch(f"{WORKFLOW_MODULE}.bulk_update_salesforce_accounts", return_value=BulkUpdateResult(succeeded=1, failed=0))
+ @patch(f"{WORKFLOW_MODULE}.bulk_update_salesforce_accounts")
@patch(f"{WORKFLOW_MODULE}.get_salesforce_client")
@patch(f"{WORKFLOW_MODULE}.fetch_stripe_signals")
@patch(f"{WORKFLOW_MODULE}.close_old_connections")
- async def test_skips_rows_with_no_matching_sfdc_account(
+ async def test_skips_rows_it_cannot_update(
self,
+ _name,
+ signals_rows,
+ account_ids_by_org,
+ expected_sent_ids,
+ expected_skipped_no_account,
_mock_close,
mock_fetch,
mock_sf_client,
mock_bulk,
_mock_heartbeat,
):
- mock_fetch.return_value = [
- _signals(org_id="org-1"),
- _signals(org_id="org-missing"),
- ]
+ mock_fetch.return_value = signals_rows
+ mock_bulk.return_value = BulkUpdateResult(succeeded=len(expected_sent_ids), failed=0)
mock_sf = MagicMock()
- mock_sf.query_all.return_value = {"records": [{"Id": "001ABC", "Posthog_Org_ID__c": "org-1"}]}
+ mock_sf.query_all.return_value = {
+ "records": [
+ {"Id": account_id, "Posthog_Org_ID__c": org_id} for org_id, account_id in account_ids_by_org.items()
+ ]
+ }
mock_sf_client.return_value = mock_sf
with patch(f"{WORKFLOW_MODULE}.asyncio.to_thread", side_effect=mock_to_thread):
result = await enrich_stripe_page_activity(EnrichStripePageInputs(page_size=5000))
+ sent_ids = [record["Id"] for call in mock_bulk.call_args_list for record in call[0][1]]
+ assert sent_ids == expected_sent_ids
assert result.rows_fetched == 2
- assert result.updated == 1
- assert result.skipped_no_account == 1
- sent_records = mock_bulk.call_args[0][1]
- assert len(sent_records) == 1
- assert sent_records[0]["Id"] == "001ABC"
+ assert result.updated == len(expected_sent_ids)
+ assert result.skipped_no_account == expected_skipped_no_account
+ assert result.errors == []
+ assert result.next_cursor_org_id == signals_rows[-1].posthog_organization_id
@pytest.mark.asyncio
@patch(f"{WORKFLOW_MODULE}.Heartbeater")
diff --git a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt b/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
index 76cebd9b4c43..ad870bff2d97 100644
--- a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
+++ b/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
@@ -1,7 +1,6 @@
1 common/hogvm/python/execute.py
1 common/hogvm/python/stl/__init__.py
1 ee/billing/salesforce_enrichment/conversations_signals.py
-1 ee/billing/salesforce_enrichment/stripe_signals.py
1 ee/billing/salesforce_enrichment/usage_signals.py
1 ee/clickhouse/models/test/test_action.py
1 ee/clickhouse/views/test/funnel/util.py