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(data-imports): keep initial_sync_complete across full-refresh syncs (#79927)

Daniel RCf4fcbf086c24
diff --git a/products/warehouse_sources/backend/models/external_data_schema.py b/products/warehouse_sources/backend/models/external_data_schema.py
index 3aa84224004e..546ffa9f94eb 100644
--- a/products/warehouse_sources/backend/models/external_data_schema.py
+++ b/products/warehouse_sources/backend/models/external_data_schema.py
@@ -824,7 +824,7 @@ class ExternalDataSchema(ModelActivityMixin, CreatedMetaFields, UpdatedMetaField
return str(value)
return str(value)
- def update_sync_type_config_for_reset_pipeline(self) -> None:
+ def update_sync_type_config_for_reset_pipeline(self, *, clear_initial_sync_complete: bool = True) -> None:
self.sync_type_config.pop("reset_pipeline", None)
# Any reset resolves a pending safe-widening marker; the re-created table adopts the new
# type. column_type_widened_last_reset_at is deliberately kept so the auto-resync cooldown
@@ -849,7 +849,13 @@ class ExternalDataSchema(ModelActivityMixin, CreatedMetaFields, UpdatedMetaField
# repartition / change-partition-mode actions precisely so they survive this reset and win
# the resync it triggers. They're consumed in set_partitioning_enabled.
- self.initial_sync_complete = False
+ # Routine full-refresh syncs pass False: the flag is a "first sync ever completed" latch
+ # consumed by webhook gating and schema-state displays, and clearing it on every run left
+ # it false between runs whenever a sync wrote zero rows (no Delta table means post-load
+ # never re-set it). Explicit resets (reset_pipeline, corruption rebuild, sync-method
+ # change, delete_table) keep clearing so CDC's False->True streaming flip still fires.
+ if clear_initial_sync_complete:
+ self.initial_sync_complete = False
self.save(skip_activity_log=True)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py
index cbe9eeb75dd8..4521ec9dc79b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/extract.py
@@ -412,7 +412,11 @@ async def handle_reset_or_full_refresh(
# Avoid schema mismatches from existing data about to be overwritten
await logger.adebug("Deleting existing table due to sync being full refresh")
await delta_table_ref.reset_table()
- await database_sync_to_async_pool(schema.update_sync_type_config_for_reset_pipeline)()
+ # Keep the initial_sync_complete latch: this branch runs on every scheduled full-refresh
+ # sync, and a run that extracts zero rows never reaches post-load to re-set it.
+ await database_sync_to_async_pool(schema.update_sync_type_config_for_reset_pipeline)(
+ clear_initial_sync_complete=False
+ )
def _capture_delta_revived(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py
index e2c91c546c52..3846dee5385b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_extract.py
@@ -510,6 +510,36 @@ class TestHandleResetOrFullRefresh:
helper.reset_table.assert_awaited_once()
schema.refresh_from_db()
assert "reset_pipeline" not in schema.sync_type_config
+ # An explicit reset redoes the initial sync, so the latch must drop (CDC's snapshot->
+ # streaming flip fires on the False->True transition).
+ assert schema.initial_sync_complete is False
+
+ def test_full_refresh_sync_keeps_initial_sync_complete(self, team):
+ # The prod regression: routine full-refresh runs cleared the latch at extraction start,
+ # and a zero-row run never reaches post-load to re-set it, so the flag read false
+ # between runs on ~1k schemas despite daily completed syncs.
+ source = ExternalDataSource.objects.create(
+ source_id=str(uuid.uuid4()), connection_id=str(uuid.uuid4()), team=team, source_type="Clickhouse"
+ )
+ schema = ExternalDataSchema.objects.create(
+ name="events",
+ team=team,
+ source=source,
+ sync_type=ExternalDataSchema.SyncType.FULL_REFRESH,
+ sync_type_config={"incremental_field_last_value": "2026-01-01T00:00:00"},
+ initial_sync_complete=True,
+ )
+ helper = MagicMock(reset_table=AsyncMock())
+
+ async_to_sync(handle_reset_or_full_refresh)(
+ False, False, schema, helper, MagicMock(adebug=AsyncMock()), webhook_only=False
+ )
+
+ helper.reset_table.assert_awaited_once()
+ assert schema.initial_sync_complete is True
+ schema.refresh_from_db()
+ assert schema.initial_sync_complete is True
+ assert "incremental_field_last_value" not in schema.sync_type_config
class TestValidateIncrementalSync: