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(warehouse-sources): prompt resync when Meta Ads lookback is raised (#86789)

posthog[bot]dcb859d69c69
diff --git a/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.test.ts b/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.test.ts
index bf67dc92b623..44f17c0596f0 100644
--- a/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.test.ts
+++ b/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.test.ts
@@ -1,15 +1,17 @@
import type { SourceFieldConfig } from '~/queries/schema/schema-general'
-import type { ExternalDataSourceSchema } from '~/types'
+import type { ExternalDataSource, ExternalDataSourceSchema } from '~/types'
import { clampSyncFrequency } from 'products/data_warehouse/frontend/utils'
import {
buildBulkEnablePayloads,
clonePayloadPreservingFiles,
+ effectiveLookbackDays,
isSensitiveCredentialField,
removeEmptySensitiveValues,
runBulkSchemaAction,
schemasEligibleForSync,
+ schemasNeedingLookbackResync,
} from './sourceSettingsLogic'
function makeSchema(overrides: Partial<ExternalDataSourceSchema>): ExternalDataSourceSchema {
@@ -229,6 +231,53 @@ describe('schemasEligibleForSync', () => {
})
})
+describe('schemasNeedingLookbackResync', () => {
+ it('keeps only enabled incremental tables so a raised lookback resyncs stats tables, not entity tables', () => {
+ const source = {
+ schemas: [
+ makeSchema({ id: 'stats-on', should_sync: true, incremental: true }),
+ makeSchema({ id: 'stats-off', should_sync: false, incremental: true }),
+ makeSchema({ id: 'entity-on', should_sync: true, incremental: false }),
+ ],
+ } as ExternalDataSource
+
+ expect(schemasNeedingLookbackResync(source).map((s) => s.id)).toEqual(['stats-on'])
+ })
+
+ it('returns an empty list when the source is missing', () => {
+ expect(schemasNeedingLookbackResync(null)).toEqual([])
+ })
+})
+
+describe('effectiveLookbackDays', () => {
+ // Blank, absent, and sub-1 values all mean the backend's 90-day default, so the resync prompt
+ // must compare against 90 rather than 0/null — otherwise it misfires on both raises and lowers.
+ it.each([
+ ['an absent value', undefined, 90],
+ ['a null value', null, 90],
+ ['a blank string', '', 90],
+ ['zero', 0, 90],
+ ['a negative value', -5, 90],
+ ['a set value', 120, 120],
+ ['a numeric string', '30', 30],
+ ['a value above the max', 10_000, 3 * 365],
+ ])('normalizes %s', (_label, input, expected) => {
+ expect(effectiveLookbackDays(input)).toBe(expected)
+ })
+
+ it('does not prompt when narrowing a blank (effective-90) window to 30', () => {
+ expect(effectiveLookbackDays('30') > effectiveLookbackDays('')).toBe(false)
+ })
+
+ it('prompts when raising from an absent value to 120', () => {
+ expect(effectiveLookbackDays(120) > effectiveLookbackDays(undefined)).toBe(true)
+ })
+
+ it('does not prompt when raising past a max that is already reached', () => {
+ expect(effectiveLookbackDays(10_000) > effectiveLookbackDays(3 * 365)).toBe(false)
+ })
+})
+
describe('clampSyncFrequency', () => {
it('floors every schema at 5 minutes, CDC included', () => {
expect(clampSyncFrequency('1min')).toBe('5min')
diff --git a/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.ts b/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.ts
index 8f35935a72b4..dbb651c8c7c5 100644
--- a/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.ts
+++ b/products/data_warehouse/frontend/scenes/SourceScene/tabs/sourceSettingsLogic.ts
@@ -18,7 +18,7 @@ import { loaders } from 'kea-loaders'
import { router } from 'kea-router'
import posthog from 'posthog-js'
-import { lemonToast } from '@posthog/lemon-ui'
+import { LemonDialog, lemonToast } from '@posthog/lemon-ui'
import api from 'lib/api'
import { tryShowMCPHint } from 'lib/components/MCPHint/mcpHintLogic'
@@ -315,6 +315,31 @@ export function schemasEligibleForSync(schemas: ExternalDataSourceSchema[]): Ext
return schemas.filter((schema) => !!schema.sync_type && schema.should_sync)
}
+const SYNC_LOOKBACK_FIELD = 'sync_lookback_days'
+
+// Mirrors the backend's Meta Ads normalization (`meta_ads.py`): a missing, blank, or sub-1 value
+// falls back to the default window, and any value is capped at the max the source will request.
+const DEFAULT_SYNC_LOOKBACK_DAYS = 90
+const META_ADS_MAX_HISTORY_DAYS = 3 * 365
+
+// The effective lookback the backend would use for `value`. Comparing raw form values instead
+// would offer a destructive resync when narrowing a blank (effective-90) window, and skip the
+// resync prompt when widening from an absent value.
+export function effectiveLookbackDays(value: unknown): number {
+ const parsed = Number(value)
+ if (!Number.isFinite(parsed) || parsed < 1) {
+ return DEFAULT_SYNC_LOOKBACK_DAYS
+ }
+ return Math.min(parsed, META_ADS_MAX_HISTORY_DAYS)
+}
+
+// A raised history window (e.g. Meta Ads `sync_lookback_days`) only pulls in older data on a full
+// resync. An enabled incremental table that already imported keeps its start date until then, so
+// these are the tables a raise would otherwise silently skip.
+export function schemasNeedingLookbackResync(source: ExternalDataSource | null): ExternalDataSourceSchema[] {
+ return source?.schemas.filter((schema) => schema.should_sync && schema.incremental) ?? []
+}
+
// Bulk-enable payloads: already-enabled schemas are skipped; schemas without a sync method ask
// the backend to discover and fill in default sync settings as part of the same update.
export function buildBulkEnablePayloads(
@@ -1055,6 +1080,12 @@ export const sourceSettingsLogic = kea<sourceSettingsLogicType>([
...sanitizedPayload,
}
+ // Read before the update, while `values.source` still holds the old config, so we can
+ // offer a resync when the user widens the history window (otherwise it's silently ignored).
+ const previousLookbackDays = effectiveLookbackDays(values.source?.job_inputs?.[SYNC_LOOKBACK_FIELD])
+ const nextLookbackDays = effectiveLookbackDays(sanitizedPayload[SYNC_LOOKBACK_FIELD])
+ const schemasToResync = schemasNeedingLookbackResync(values.source)
+
// Handle file uploads
const sourceFieldConfig = values.sourceFieldConfig
if (sourceFieldConfig?.fields) {
@@ -1102,6 +1133,19 @@ export const sourceSettingsLogic = kea<sourceSettingsLogicType>([
})
actions.loadSource()
lemonToast.success('Source updated')
+
+ if (nextLookbackDays > previousLookbackDays && schemasToResync.length > 0) {
+ LemonDialog.open({
+ title: 'Import the older data?',
+ description: `A wider history window applies to tables you already synced only after a full resync. Resync ${pluralize(schemasToResync.length, 'table', 'tables')} now to import the older data?`,
+ primaryButton: {
+ children: 'Resync now',
+ onClick: () => actions.bulkResync(schemasToResync),
+ },
+ secondaryButton: { children: 'Not now' },
+ })
+ }
+
tryShowMCPHint('data_warehouse_sources.update', {
derivedPrompt: values.source?.source_type
? `Update the configuration on my ${values.source.source_type} source`
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py
index e8f1b635f7c4..f23114fce694 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py
@@ -248,6 +248,10 @@ class MetaAdsSource(ResumableSource[MetaAdsSourceConfig, MetaAdsResumeConfig], O
required=False,
placeholder="90",
secret=False,
+ caption=(
+ "A stats table that already imported keeps its current start date. A higher "
+ "value pulls in older data only after you resync that table."
+ ),
),
# Attribution settings for insights (spend/conversion) tables. Left unset, Meta
# applies its own default, so existing connections are unaffected. Set them to