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): clear sql editor content when the open view is deleted (#73168)

Jovan Sakovice223a7d06b2d
diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
index 0e407f3c21ca..35c361a76b82 100644
--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
@@ -163,6 +163,39 @@ function createMockEditor(): any {
}
}
+// A Monaco model that records pushEditOperations as if it had a real undo stack, so tests can
+// assert an edit is applied undoably (pushEditOperations) rather than via model.setValue
+// (which would wipe history).
+function createUndoTrackingModel(initialValue: string): any {
+ let value = initialValue
+ return {
+ getValue: () => value,
+ getFullModelRange: () => ({
+ startLineNumber: 1,
+ startColumn: 1,
+ endLineNumber: 1,
+ endColumn: value.length + 1,
+ }),
+ pushStackElement: jest.fn(),
+ pushEditOperations: jest.fn((_before: any, ops: any[]) => {
+ value = ops[0].text
+ return null
+ }),
+ setValue: jest.fn((next: string) => {
+ value = next
+ }),
+ onDidChangeContent: jest.fn(() => ({ dispose: jest.fn() })),
+ dispose: jest.fn(),
+ }
+}
+
+function createMonacoWithModel(model: any): any {
+ const monaco = createMockMonaco()
+ monaco.editor.getModel = () => model
+ monaco.editor.createModel = () => model
+ return monaco
+}
+
describe('sqlEditorLogic', () => {
let logic: ReturnType<typeof sqlEditorLogic.build>
let editorRootLogic: ReturnType<typeof editorSceneLogic.build> | undefined
@@ -235,6 +268,7 @@ describe('sqlEditorLogic', () => {
},
delete: {
'/api/environments/:team_id/query/:id/': [204],
+ '/api/environments/:team_id/warehouse_saved_queries/:id/': [204],
},
})
@@ -1628,39 +1662,6 @@ describe('sqlEditorLogic', () => {
const ORIGINAL = 'SELECT 1'
const ACCEPTED = 'SELECT 2 FROM events'
- // A Monaco model that records pushEditOperations as if it had a real undo stack,
- // so we can assert the accepted query is applied as an undoable edit rather than a
- // model.setValue (which would wipe history).
- function createUndoTrackingModel(initialValue: string): any {
- let value = initialValue
- return {
- getValue: () => value,
- getFullModelRange: () => ({
- startLineNumber: 1,
- startColumn: 1,
- endLineNumber: 1,
- endColumn: value.length + 1,
- }),
- pushStackElement: jest.fn(),
- pushEditOperations: jest.fn((_before: any, ops: any[]) => {
- value = ops[0].text
- return null
- }),
- setValue: jest.fn((next: string) => {
- value = next
- }),
- onDidChangeContent: jest.fn(() => ({ dispose: jest.fn() })),
- dispose: jest.fn(),
- }
- }
-
- function createMonacoWithModel(model: any): any {
- const monaco = createMockMonaco()
- monaco.editor.getModel = () => model
- monaco.editor.createModel = () => model
- return monaco
- }
-
function mountWithModel(model: any): any {
const monaco = createMonacoWithModel(model)
logic = sqlEditorLogic({ tabId: TAB_ID, monaco, editor: createMockEditor() })
@@ -1727,6 +1728,51 @@ describe('sqlEditorLogic', () => {
})
})
+ describe('deleting the open view', () => {
+ it('clears the tab and the editor content when the active view is deleted', async () => {
+ const viewsLogic = dataWarehouseViewsLogic()
+ viewsLogic.mount()
+
+ const model = createUndoTrackingModel(MOCK_VIEW.query.query)
+ const monaco = createMonacoWithModel(model)
+ logic = sqlEditorLogic({ tabId: TAB_ID, monaco, editor: createMockEditor() })
+ logic.mount()
+ logic.actions.updateTab({ uri: monaco.Uri.parse(`tab-${TAB_ID}`), name: MOCK_VIEW.name, view: MOCK_VIEW })
+ logic.actions.setQueryInput(MOCK_VIEW.query.query)
+
+ viewsLogic.actions.deleteDataWarehouseSavedQuery(MOCK_VIEW.id)
+ await expectLogic(viewsLogic).toDispatchActions(['deleteDataWarehouseSavedQuerySuccess'])
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(model.getValue()).toEqual('')
+ expect(logic.values.queryInput).toEqual('')
+ expect(logic.values.activeTab?.view).toBeUndefined()
+
+ viewsLogic.unmount()
+ })
+
+ it('leaves an unrelated tab untouched', async () => {
+ const viewsLogic = dataWarehouseViewsLogic()
+ viewsLogic.mount()
+
+ const model = createUndoTrackingModel('SELECT 2')
+ const monaco = createMonacoWithModel(model)
+ logic = sqlEditorLogic({ tabId: TAB_ID, monaco, editor: createMockEditor() })
+ logic.mount()
+ logic.actions.updateTab({ uri: monaco.Uri.parse(`tab-${TAB_ID}`), name: 'Untitled' })
+ logic.actions.setQueryInput('SELECT 2')
+
+ viewsLogic.actions.deleteDataWarehouseSavedQuery(MOCK_VIEW.id)
+ await expectLogic(viewsLogic).toDispatchActions(['deleteDataWarehouseSavedQuerySuccess'])
+ await expectLogic(viewsLogic).toFinishAllListeners()
+
+ expect(model.getValue()).toEqual('SELECT 2')
+ expect(logic.values.queryInput).toEqual('SELECT 2')
+
+ viewsLogic.unmount()
+ })
+ })
+
describe('attaching the dashboard when saving from a dashboard flow', () => {
const DASHBOARD_ID = 99
diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
index 5e9af60f995b..fbc87c4e9eaa 100644
--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
@@ -2575,6 +2575,9 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([
},
deleteDataWarehouseSavedQuerySuccess: ({ payload: viewId }) => {
if (values.activeTab?.view?.id === viewId && !values.activeTab?.draft) {
+ // createTab() alone reuses the existing model and doesn't clear queryInput
+ applyUndoableModelEdit(props.monaco, values.activeTab?.uri, '')
+ actions.setQueryInput('')
actions.createTab()
}
},