From b52efe05999b519a710fb80e1f8ca16c53da809f Mon Sep 17 00:00:00 2001 From: "JUST.in DO IT" Date: Fri, 31 Mar 2023 10:20:29 -0700 Subject: [PATCH] fix(sqllab): dedupe table_schemas in active_tab (#23514) --- .../src/SqlLab/reducers/getInitialState.js | 20 +++++-- .../SqlLab/reducers/getInitialState.test.ts | 54 +++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/superset-frontend/src/SqlLab/reducers/getInitialState.js b/superset-frontend/src/SqlLab/reducers/getInitialState.js index 1d50c1067c36e..5cd678657a29f 100644 --- a/superset-frontend/src/SqlLab/reducers/getInitialState.js +++ b/superset-frontend/src/SqlLab/reducers/getInitialState.js @@ -113,7 +113,7 @@ export default function getInitialState({ }); const tabHistory = activeTab ? [activeTab.id.toString()] : []; - const tables = []; + let tables = {}; if (activeTab) { activeTab.table_schemas .filter(tableSchema => tableSchema.description !== null) @@ -146,7 +146,10 @@ export default function getInitialState({ partitions, metadata, }; - tables.push(table); + tables = { + ...tables, + [table.id]: table, + }; }); } @@ -183,8 +186,15 @@ export default function getInitialState({ }, }; }); - sqlLab.tables.forEach(table => - tables.push({ ...table, inLocalStorage: true }), + tables = sqlLab.tables.reduce( + (merged, table) => ({ + ...merged, + [table.id]: { + ...tables[table.id], + ...table, + }, + }), + tables, ); Object.values(sqlLab.queries).forEach(query => { queries[query.id] = { ...query, inLocalStorage: true }; @@ -202,7 +212,7 @@ export default function getInitialState({ queries, queryEditors: Object.values(queryEditors), tabHistory: dedupeTabHistory(tabHistory), - tables, + tables: Object.values(tables), queriesLastUpdate: Date.now(), user, unsavedQueryEditor, diff --git a/superset-frontend/src/SqlLab/reducers/getInitialState.test.ts b/superset-frontend/src/SqlLab/reducers/getInitialState.test.ts index e1896d882da8d..c06633c6f5c7f 100644 --- a/superset-frontend/src/SqlLab/reducers/getInitialState.test.ts +++ b/superset-frontend/src/SqlLab/reducers/getInitialState.test.ts @@ -68,4 +68,58 @@ describe('getInitialState', () => { }); }); }); + + describe('dedupe tables schema', () => { + afterEach(() => { + localStorage.clear(); + }); + + it('should dedupe the table schema', () => { + localStorage.setItem( + 'redux', + JSON.stringify({ + sqlLab: { + tables: [ + { id: 1, name: 'test1' }, + { id: 6, name: 'test6' }, + ], + queryEditors: [{ id: 1, title: 'editor1' }], + queries: {}, + tabHistory: [], + }, + }), + ); + const initializedTables = getInitialState({ + ...apiData, + active_tab: { + id: 1, + table_schemas: [ + { + id: 1, + table: 'table1', + tab_state_id: 1, + description: { + columns: [ + { name: 'id', type: 'INT' }, + { name: 'column2', type: 'STRING' }, + ], + }, + }, + { + id: 2, + table: 'table2', + tab_state_id: 1, + description: { + columns: [ + { name: 'id', type: 'INT' }, + { name: 'column2', type: 'STRING' }, + ], + }, + }, + ], + }, + }).sqlLab.tables; + expect(initializedTables.map(({ id }) => id)).toEqual([1, 2, 6]); + }); + }); });