diff --git a/superset-frontend/src/SqlLab/reducers/getInitialState.js b/superset-frontend/src/SqlLab/reducers/getInitialState.js index 1d50c1067c..5cd678657a 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 e1896d882d..c06633c6f5 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]); + }); + }); });