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): keep "Update insight" enabled after editing SQL (#57931)

Thomas Obermüllerce89e560e843
diff --git a/frontend/src/scenes/data-warehouse/editor/editorSceneLogic.tsx b/frontend/src/scenes/data-warehouse/editor/editorSceneLogic.tsx
index 61f21f032f75..a3ba2d926797 100644
--- a/frontend/src/scenes/data-warehouse/editor/editorSceneLogic.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/editorSceneLogic.tsx
@@ -13,12 +13,7 @@ import { DataVisualizationNode, FileSystemIconType, HogQLFilters, NodeKind } fro
import { Breadcrumb } from '~/types'
import type { editorSceneLogicType } from './editorSceneLogicType'
-import {
- getCurrentVisualizationQuery,
- normalizeFiltersForUrl,
- sqlEditorLogic,
- toDataVisualizationNode,
-} from './sqlEditorLogic'
+import { normalizeFiltersForUrl, sqlEditorLogic, toDataVisualizationNode } from './sqlEditorLogic'
export interface SaveAsMenuItem {
action: 'insight' | 'endpoint' | 'view'
@@ -282,16 +277,14 @@ export const editorSceneLogic = kea<editorSceneLogicType>([
},
],
updateInsightButtonEnabled: [
- (s) => [s.sourceQuery, s.activeTab, s.editingInsight, s.dataLogicKey],
- (sourceQuery, activeTab, editingInsight, dataLogicKey) => {
+ (s) => [s.sourceQuery, s.activeTab, s.editingInsight],
+ (sourceQuery, activeTab, editingInsight) => {
if (!editingInsight?.query) {
return false
}
const updatedName = activeTab?.name !== editingInsight.name
- const currentVisualizationQuery = getCurrentVisualizationQuery(dataLogicKey, sourceQuery)
-
- const sourceQueryWithoutUndefinedAndNullKeys = removeUndefinedAndNull(currentVisualizationQuery)
+ const sourceQueryWithoutUndefinedAndNullKeys = removeUndefinedAndNull(sourceQuery)
// Normalize so DataTableNode-based insights don't look "changed" immediately after load.
const editingInsightQuery = toDataVisualizationNode(editingInsight.query) ?? editingInsight.query
diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
index 868ef534eb79..d8f8c436e783 100644
--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
@@ -7,6 +7,7 @@ import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
import { useMocks } from '~/mocks/jest'
+import { dataVisualizationLogic } from '~/queries/nodes/DataVisualization/dataVisualizationLogic'
import * as queryRunner from '~/queries/query'
import {
DataTableNode,
@@ -680,6 +681,50 @@ describe('sqlEditorLogic', () => {
expect(editorRootLogic.values.updateInsightButtonEnabled).toEqual(false)
})
+ it('enables Update insight as soon as sourceQuery diverges from the saved insight, even when dataVisualizationLogic mirror lags behind', async () => {
+ logic = sqlEditorLogic({
+ tabId: TAB_ID,
+ monaco: createMockMonaco(),
+ editor: createMockEditor(),
+ })
+ logic.mount()
+ editorRootLogic = editorSceneLogic({ tabId: TAB_ID })
+ editorRootLogic.mount()
+
+ router.actions.push(urls.sqlEditor(), { open_insight: MOCK_INSIGHT_SHORT_ID })
+
+ await expectLogic(logic)
+ .toDispatchActions(['editInsight', 'createTab', 'updateTab'])
+ .toMatchValues({
+ editingInsight: partial({ short_id: MOCK_INSIGHT_SHORT_ID }),
+ })
+
+ // Mount dataVisualizationLogic with the saved query, mirroring what BindLogic
+ // does in production. This is the state right after the insight finishes loading.
+ const dataLogicKey = logic.values.dataLogicKey
+ const visualizationLogic = dataVisualizationLogic({
+ key: dataLogicKey,
+ query: MOCK_INSIGHT_QUERY,
+ dataNodeCollectionId: dataLogicKey,
+ })
+ visualizationLogic.mount()
+
+ expect(editorRootLogic.values.updateInsightButtonEnabled).toEqual(false)
+
+ // Simulate runQuery firing setSourceQuery with a different SQL string.
+ // dataVisualizationLogic.values.query still mirrors the OLD query at this point —
+ // in production it only catches up after React re-renders and propsChanged fires.
+ // The selector must reflect the change immediately so the user can save.
+ logic.actions.setSourceQuery({
+ ...MOCK_INSIGHT_QUERY,
+ source: { ...MOCK_INSIGHT_QUERY.source, query: 'SELECT count() FROM events WHERE event = $pageview' },
+ })
+
+ expect(editorRootLogic.values.updateInsightButtonEnabled).toEqual(true)
+
+ visualizationLogic.unmount()
+ })
+
it('does not dispatch syncUrlWithQuery before the API responds', async () => {
logic = sqlEditorLogic({
tabId: TAB_ID,