From f3163e1c275569eb081a0477f6dfd7e1f792a867 Mon Sep 17 00:00:00 2001 From: "Michael S. Molina" <70410625+michael-s-molina@users.noreply.github.com> Date: Thu, 11 Sep 2025 17:53:51 -0300 Subject: [PATCH] fix: SQL Lab tab events (#35105) (cherry picked from commit e729b2dbb42ada7feb6484a2236be73932b630bc) --- superset-frontend/src/SqlLab/actions/sqlLab.js | 2 ++ superset-frontend/src/SqlLab/actions/sqlLab.test.js | 4 ++++ superset-frontend/src/SqlLab/fixtures.ts | 4 ++++ superset-frontend/src/SqlLab/reducers/getInitialState.ts | 4 ++++ superset-frontend/src/SqlLab/types.ts | 1 + .../src/SqlLab/utils/reduxStateToLocalStorageHelper.ts | 1 + .../src/hooks/apiResources/sqlEditorTabs.test.ts | 1 + 7 files changed, 17 insertions(+) diff --git a/superset-frontend/src/SqlLab/actions/sqlLab.js b/superset-frontend/src/SqlLab/actions/sqlLab.js index f0745695fbe..edb9f3462d4 100644 --- a/superset-frontend/src/SqlLab/actions/sqlLab.js +++ b/superset-frontend/src/SqlLab/actions/sqlLab.js @@ -391,6 +391,7 @@ export function runQueryFromSqlEditor( dbId: qe.dbId, sql: qe.selectedText || qe.sql, sqlEditorId: qe.tabViewId ?? qe.id, + immutableId: qe.immutableId, tab: qe.name, catalog: qe.catalog, schema: qe.schema, @@ -533,6 +534,7 @@ export function addQueryEditor(queryEditor) { const newQueryEditor = { ...queryEditor, id: nanoid(11), + immutableId: nanoid(11), loaded: true, inLocalStorage: true, }; diff --git a/superset-frontend/src/SqlLab/actions/sqlLab.test.js b/superset-frontend/src/SqlLab/actions/sqlLab.test.js index 4f344c973b9..0280a283a66 100644 --- a/superset-frontend/src/SqlLab/actions/sqlLab.test.js +++ b/superset-frontend/src/SqlLab/actions/sqlLab.test.js @@ -441,6 +441,7 @@ describe('async actions', () => { queryLimit: undefined, maxRow: undefined, id: 'abcd', + immutableId: 'abcd', templateParams: undefined, inLocalStorage: true, loaded: true, @@ -570,6 +571,7 @@ describe('async actions', () => { type: actions.ADD_QUERY_EDITOR, queryEditor: { ...queryEditor, + immutableId: 'abcd', inLocalStorage: true, loaded: true, }, @@ -597,6 +599,7 @@ describe('async actions', () => { type: actions.ADD_QUERY_EDITOR, queryEditor: { id: 'abcd', + immutableId: 'abcd', sql: expect.stringContaining('SELECT ...'), name: `Untitled Query 7`, dbId: defaultQueryEditor.dbId, @@ -753,6 +756,7 @@ describe('async actions', () => { queryEditor: { ...queryEditor, id: 'abcd', + immutableId: 'abcd', loaded: true, inLocalStorage: true, }, diff --git a/superset-frontend/src/SqlLab/fixtures.ts b/superset-frontend/src/SqlLab/fixtures.ts index 9ac8bed6e78..3ebc1db5980 100644 --- a/superset-frontend/src/SqlLab/fixtures.ts +++ b/superset-frontend/src/SqlLab/fixtures.ts @@ -188,6 +188,7 @@ export const table = { export const defaultQueryEditor = { version: LatestQueryEditorVersion, id: 'dfsadfs', + immutableId: 'immutable-id', autorun: false, dbId: 1, latestQueryId: null, @@ -204,6 +205,7 @@ export const defaultQueryEditor = { export const extraQueryEditor1 = { ...defaultQueryEditor, id: 'diekd23', + immutableId: 'immutable-id', sql: 'SELECT *\nFROM\nWHERE\nLIMIT', name: 'Untitled Query 2', selectedText: 'SELECT', @@ -212,6 +214,7 @@ export const extraQueryEditor1 = { export const extraQueryEditor2 = { ...defaultQueryEditor, id: 'owkdi998', + immutableId: 'immutable-id', sql: '', name: 'Untitled Query 3', }; @@ -219,6 +222,7 @@ export const extraQueryEditor2 = { export const extraQueryEditor3 = { ...defaultQueryEditor, id: 'kvk23', + immutableId: 'immutable-id', sql: '', name: 'Untitled Query 4', tabViewId: 37, diff --git a/superset-frontend/src/SqlLab/reducers/getInitialState.ts b/superset-frontend/src/SqlLab/reducers/getInitialState.ts index 91386e3fe67..7d9f0fddaca 100644 --- a/superset-frontend/src/SqlLab/reducers/getInitialState.ts +++ b/superset-frontend/src/SqlLab/reducers/getInitialState.ts @@ -17,6 +17,7 @@ * under the License. */ import { t } from '@superset-ui/core'; +import { nanoid } from 'nanoid'; import type { BootstrapData } from 'src/types/bootstrapTypes'; import type { InitialState } from 'src/hooks/apiResources/sqlLab'; import { @@ -55,6 +56,7 @@ export default function getInitialState({ let queryEditors: Record = {}; const defaultQueryEditor = { version: LatestQueryEditorVersion, + immutableId: nanoid(11), loaded: true, name: t('Untitled query'), sql: '', @@ -78,6 +80,7 @@ export default function getInitialState({ queryEditor = { version: activeTab.extra_json?.version ?? QueryEditorVersion.V1, id: id.toString(), + immutableId: activeTab.extra_json?.immutableId ?? nanoid(11), loaded: true, name: activeTab.label, sql: activeTab.sql || '', @@ -100,6 +103,7 @@ export default function getInitialState({ queryEditor = { ...defaultQueryEditor, id: id.toString(), + immutableId: nanoid(11), loaded: false, name: label, dbId: undefined, diff --git a/superset-frontend/src/SqlLab/types.ts b/superset-frontend/src/SqlLab/types.ts index 9665a88fae8..5532f155b74 100644 --- a/superset-frontend/src/SqlLab/types.ts +++ b/superset-frontend/src/SqlLab/types.ts @@ -49,6 +49,7 @@ export interface CursorPosition { export interface QueryEditor { version: QueryEditorVersion; id: string; + immutableId: string; dbId?: number; name: string; title?: string; // keep it optional for backward compatibility diff --git a/superset-frontend/src/SqlLab/utils/reduxStateToLocalStorageHelper.ts b/superset-frontend/src/SqlLab/utils/reduxStateToLocalStorageHelper.ts index e8e1e156739..683b082b38c 100644 --- a/superset-frontend/src/SqlLab/utils/reduxStateToLocalStorageHelper.ts +++ b/superset-frontend/src/SqlLab/utils/reduxStateToLocalStorageHelper.ts @@ -45,6 +45,7 @@ const PERSISTENT_QUERY_EDITOR_KEYS = new Set([ 'dbId', 'height', 'id', + 'immutableId', 'latestQueryId', 'northPercent', 'queryLimit', diff --git a/superset-frontend/src/hooks/apiResources/sqlEditorTabs.test.ts b/superset-frontend/src/hooks/apiResources/sqlEditorTabs.test.ts index 42f738f3c92..5d6c9a55a90 100644 --- a/superset-frontend/src/hooks/apiResources/sqlEditorTabs.test.ts +++ b/superset-frontend/src/hooks/apiResources/sqlEditorTabs.test.ts @@ -33,6 +33,7 @@ import { const expectedQueryEditor = { version: LatestQueryEditorVersion, id: '123', + immutableId: 'immutable-id', dbId: 456, name: 'tab 1', sql: 'SELECT * from example_table',