fix(sqllab): dedupe table_schemas in active_tab (#23514)

This commit is contained in:
JUST.in DO IT 2023-03-31 10:20:29 -07:00 committed by GitHub
parent 1ced7cdbbb
commit b52efe0599
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 5 deletions

View File

@ -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,

View File

@ -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]);
});
});
});