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-warehouse): stop direct mysql refresh from soft-deleting matched rows (#108028)

posthog[bot]38fb87680df4
diff --git a/products/data_warehouse/backend/mysql_helpers.py b/products/data_warehouse/backend/mysql_helpers.py
index d8f7877bd0ef..bc794e01a2c0 100644
--- a/products/data_warehouse/backend/mysql_helpers.py
+++ b/products/data_warehouse/backend/mysql_helpers.py
@@ -155,6 +155,7 @@ def reconcile_mysql_schemas(
)
schema_models_by_location.setdefault(location, schema_model)
+ matched_ids: set[Any] = set()
for source_schema in source_schemas:
matched: ExternalDataSchema | None = schema_models.get(source_schema.name)
if matched is None:
@@ -169,6 +170,7 @@ def reconcile_mysql_schemas(
matched = schema_models_by_location.get(location)
if matched is None:
continue
+ matched_ids.add(matched.id)
resolved_schema, resolved_table = get_mysql_source_location(
schema_name=source_schema.name,
@@ -239,10 +241,16 @@ def reconcile_mysql_schemas(
return []
stale_names: list[str] = []
- stale = ExternalDataSchema.objects.filter(
- Q(team_id=team_id, source_id=source.id),
- Q(deleted=False) | Q(table__deleted=False),
- ).exclude(name__in=source_schema_names)
+ # A row matched by location keeps its old name (e.g. bare `users` for discovered `db.users`),
+ # so a name-only check would hide a table that this refresh just updated.
+ stale = (
+ ExternalDataSchema.objects.filter(
+ Q(team_id=team_id, source_id=source.id),
+ Q(deleted=False) | Q(table__deleted=False),
+ )
+ .exclude(name__in=source_schema_names)
+ .exclude(id__in=matched_ids)
+ )
for s in stale:
hide_direct_mysql_table(s.table)
if not s.deleted:
diff --git a/products/data_warehouse/backend/test/test_mysql_helpers.py b/products/data_warehouse/backend/test/test_mysql_helpers.py
new file mode 100644
index 000000000000..434c1506ad0b
--- /dev/null
+++ b/products/data_warehouse/backend/test/test_mysql_helpers.py
@@ -0,0 +1,48 @@
+import uuid
+
+from posthog.test.base import APIBaseTest
+
+from products.data_warehouse.backend.mysql_helpers import reconcile_mysql_schemas
+from products.warehouse_sources.backend.facade.models import ExternalDataSchema, ExternalDataSource
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.schema import SourceSchema
+
+
+class TestReconcileMySQLSchemas(APIBaseTest):
+ def test_direct_query_keeps_bare_row_matched_by_qualified_discovered_name(self) -> None:
+ source = ExternalDataSource.objects.create(
+ team_id=self.team.pk,
+ source_id=str(uuid.uuid4()),
+ connection_id=str(uuid.uuid4()),
+ destination_id=str(uuid.uuid4()),
+ source_type="MySQL",
+ created_by=self.user,
+ access_method=ExternalDataSource.AccessMethod.DIRECT,
+ job_inputs={"host": "localhost", "port": 3306, "database": "app_db", "schema": "app_db"},
+ )
+ users = ExternalDataSchema.objects.create(
+ team_id=self.team.pk, source_id=source.pk, name="users", should_sync=True
+ )
+ orders = ExternalDataSchema.objects.create(
+ team_id=self.team.pk, source_id=source.pk, name="orders", should_sync=True
+ )
+
+ source_schemas = [
+ SourceSchema(
+ name="app_db.users",
+ supports_incremental=False,
+ supports_append=False,
+ columns=[("id", "int", False)],
+ source_schema="app_db",
+ source_table_name="users",
+ ),
+ ]
+
+ stale_names = reconcile_mysql_schemas(source=source, source_schemas=source_schemas, team_id=self.team.pk)
+
+ assert stale_names == ["orders"]
+ users.refresh_from_db()
+ orders.refresh_from_db()
+ assert users.deleted is False
+ assert users.table is not None
+ assert users.table.deleted is False
+ assert orders.deleted is True