: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-imports): stop resurrecting deleted schema rows on bookkeeping save (#73430)
Tom Owersd4c597de079e
diff --git a/products/warehouse_sources/backend/models/external_data_schema.py b/products/warehouse_sources/backend/models/external_data_schema.pyindex b13f2849c35c..aebcfa5cfb68 100644--- a/products/warehouse_sources/backend/models/external_data_schema.py+++ b/products/warehouse_sources/backend/models/external_data_schema.py@@ -120,6 +120,13 @@ class ExternalDataSchema(ModelActivityMixin, CreatedMetaFields, UpdatedMetaField# an audit trail. Bypass ModelActivityMixin.save() so we skip its extra _get_before_update# SELECT — that read needs a fresh pooler connection and raises OperationalError when the# transaction pooler has dropped the connection mid-sync, failing the import activity.+ #+ # These calls always target an already-persisted row. Without force_update, Django's+ # UUID-pk-with-default fallback would silently retry a no-op UPDATE as an INSERT if the+ # row was deleted concurrently (e.g. the source/schema deleted mid-sync) — either+ # resurrecting deleted data, or failing with a misleading FK IntegrityError on source_id+ # instead of a clear "no such row" error.+ kwargs.setdefault("force_update", True)super(ModelActivityMixin, self).save(*args, **kwargs)else:super().save(*args, **kwargs)diff --git a/products/warehouse_sources/backend/tests/test_models.py b/products/warehouse_sources/backend/tests/test_models.pyindex 13c650d0da90..376226596d1c 100644--- a/products/warehouse_sources/backend/tests/test_models.py+++ b/products/warehouse_sources/backend/tests/test_models.py@@ -5,6 +5,7 @@ import pytestfrom posthog.test.base import BaseTestfrom unittest.mock import patch+from django.db import DatabaseError, transactionfrom django.db.models import Modelfrom django.test import SimpleTestCasefrom django.utils import timezone@@ -204,6 +205,26 @@ class TestExternalDataSchemaActivityLogging(BaseTest):schema.refresh_from_db()assert schema.incremental_field_last_value == 42+ def test_bookkeeping_save_raises_instead_of_resurrecting_deleted_row(self) -> None:+ # Source (and its schema, via CASCADE) deleted concurrently with a sync still holding a+ # stale in-memory schema reference. Without force_update, Django's UUID-pk insert fallback+ # would silently recreate the row here and hit an FK violation on source_id instead.+ schema = self._create(+ sync_type=ExternalDataSchema.SyncType.INCREMENTAL,+ sync_type_config={"incremental_field_type": IncrementalFieldType.Integer},+ )+ schema_id = schema.pk+ # A queryset delete (unlike self.source.delete()) doesn't null out this process's cached+ # `schema.source`, matching production where the delete happens on another connection.+ ExternalDataSource.objects.filter(pk=self.source.pk).delete()++ # Postgres aborts the whole transaction on an unhandled DatabaseError; a savepoint keeps+ # the failure scoped so the existence check below can still run.+ with self.assertRaises(DatabaseError), transaction.atomic():+ schema.update_incremental_field_value(42)++ assert not ExternalDataSchema.objects.filter(pk=schema_id).exists()+class TestExternalDataSchemaOOMEvent(BaseTest):def _source(self) -> ExternalDataSource: