From 07e403d8ee110fafb8aab586537e53dfbb91bffe Mon Sep 17 00:00:00 2001 From: GagandeepSingh20 Date: Mon, 22 Jun 2026 14:08:57 +0530 Subject: [PATCH] fix(sqllab): preserve database state on SET_DATABASES --- .../src/SqlLab/reducers/sqlLab.test.ts | 42 ++++++++++++++++++- .../src/SqlLab/reducers/sqlLab.ts | 8 +++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/superset-frontend/src/SqlLab/reducers/sqlLab.test.ts b/superset-frontend/src/SqlLab/reducers/sqlLab.test.ts index eacbe65ea83e..742f10da0e48 100644 --- a/superset-frontend/src/SqlLab/reducers/sqlLab.test.ts +++ b/superset-frontend/src/SqlLab/reducers/sqlLab.test.ts @@ -21,13 +21,53 @@ import sqlLabReducer from 'src/SqlLab/reducers/sqlLab'; import * as actions from 'src/SqlLab/actions/sqlLab'; import type { SqlLabAction } from 'src/SqlLab/actions/sqlLab'; import type { SqlLabRootState } from 'src/SqlLab/types'; -import { table, initialState as mockState } from '../fixtures'; +import { + table, + databases, + initialState as mockState, +} from '../fixtures'; type SqlLabState = SqlLabRootState['sqlLab']; const initialState = mockState.sqlLab as unknown as SqlLabState; // eslint-disable-next-line no-restricted-globals -- TODO: Migrate from describe blocks describe('sqlLabReducer', () => { + describe('Database actions', () => { + test('should merge databases instead of replacing existing database state', () => { + const existingDb = databases.result[0]; + const incomingDb = databases.result[1]; + + const state = { + ...initialState, + databases: { + [existingDb.id]: { + ...existingDb, + extra_json: {}, + }, + }, + }; + + const action = actions.setDatabases([ + { + ...incomingDb, + extra: '{}', + }, + ] as any); + + const newState = sqlLabReducer(state as any, action); + + expect(newState.databases[existingDb.id]).toBeDefined(); + expect(newState.databases[incomingDb.id]).toBeDefined(); + + expect( + newState.databases[existingDb.id].database_name, + ).toBe(existingDb.database_name); + + expect( + newState.databases[incomingDb.id].database_name, + ).toBe(incomingDb.database_name); + }); + }); // eslint-disable-next-line no-restricted-globals -- TODO: Migrate from describe blocks describe('Query editors actions', () => { let newState: SqlLabState; diff --git a/superset-frontend/src/SqlLab/reducers/sqlLab.ts b/superset-frontend/src/SqlLab/reducers/sqlLab.ts index 34e3af847ccf..577406c99dbd 100644 --- a/superset-frontend/src/SqlLab/reducers/sqlLab.ts +++ b/superset-frontend/src/SqlLab/reducers/sqlLab.ts @@ -718,7 +718,13 @@ export default function sqlLabReducer( extra_json: JSON.parse(db.extra || ''), }; }); - return { ...state, databases }; + return { + ...state, + databases: { + ...state.databases, + ...databases, + }, + }; }, [actions.REFRESH_QUERIES]() { let newQueries = { ...state.queries };