From a946c708697e1f8dc71151737bccc3b5d07875f7 Mon Sep 17 00:00:00 2001 From: Anil Sahoo Date: Mon, 17 Jun 2024 14:07:45 +0530 Subject: [PATCH] Fixed an issue where changing a column name should reflect in all the constraints in table object dialog and ERD table dialog. #7476 --- .../static/js/exclusion_constraint.ui.js | 11 +++++------ .../foreign_key/static/js/foreign_key.ui.js | 5 ++--- .../index_constraint/static/js/primary_key.ui.js | 9 +++++---- .../static/js/unique_constraint.ui.js | 4 ++-- .../databases/schemas/tables/static/js/table.ui.js | 10 +++++----- web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js | 6 +++--- .../schema_ui_files/exclusion_constraint.ui.spec.js | 11 +++++------ .../javascript/schema_ui_files/primary_key.ui.spec.js | 2 +- 8 files changed, 28 insertions(+), 30 deletions(-) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/static/js/exclusion_constraint.ui.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/static/js/exclusion_constraint.ui.js index 6fc3caf94..b63b08bca 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/static/js/exclusion_constraint.ui.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/static/js/exclusion_constraint.ui.js @@ -68,7 +68,6 @@ class ExclusionColHeaderSchema extends BaseUISchema { return this.exColumnSchema.getNewData({ is_exp: data.is_exp, column: data.is_exp ? data.expression : data.column, - column_cid: data.is_exp ? null : column?.cid, col_type: data.is_exp ? null : column?.datatype, }); } @@ -379,13 +378,13 @@ export default class ExclusionConstraintSchema extends BaseUISchema { if(obj.inTable && source[0] == 'columns') { if(actionObj.type == SCHEMA_STATE_ACTIONS.DELETE_ROW) { let column = _.get(actionObj.oldState, actionObj.path.concat(actionObj.value)); - currColumns = _.filter(currColumns, (cc)=>cc.column_cid != column.cid); + currColumns = _.filter(currColumns, (cc)=>cc.column != column.name); } else if(actionObj.type == SCHEMA_STATE_ACTIONS.SET_VALUE) { - let tabColPath = _.slice(actionObj.path, 0, -1); - let column = _.get(topState, tabColPath); - let idx = _.findIndex(currColumns, (cc)=>cc.column_cid == column.cid); + let tabColPath =_.slice(actionObj.path, 0, -1); + let column = _.get(actionObj.oldState, tabColPath); + let idx = _.findIndex(currColumns, (cc)=>cc.column == column.name); if(idx > -1) { - currColumns[idx].column = column.name; + currColumns[idx].column = _.get(topState, tabColPath).name; } } } diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.ui.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.ui.js index 22eead166..261d89cbe 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.ui.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/static/js/foreign_key.ui.js @@ -66,7 +66,6 @@ class ForeignKeyHeaderSchema extends BaseUISchema { let references_table_name = _.find(this.refTables, (t)=>t.value==data.references || t.value == this.origData.references)?.label; return { local_column: data.local_column, - local_column_cid: _.find(this.fieldOptions.local_column, (c)=>c.value == data.local_column)?.cid, referenced: data.referenced, references: data.references, references_table_name: references_table_name, @@ -348,11 +347,11 @@ export default class ForeignKeySchema extends BaseUISchema { if(obj.inTable && source[0] == 'columns') { if(actionObj.type == SCHEMA_STATE_ACTIONS.DELETE_ROW) { let column = _.get(actionObj.oldState, actionObj.path.concat(actionObj.value)); - currColumns = _.filter(currColumns, (cc)=>cc.local_column_cid != column.cid); + currColumns = _.filter(currColumns, (cc)=>cc.local_column != column.name); } else if(actionObj.type == SCHEMA_STATE_ACTIONS.SET_VALUE) { let tabColPath = _.slice(actionObj.path, 0, -1); let column = _.get(actionObj.oldState, tabColPath); - let idx = _.findIndex(currColumns, (cc)=>cc.local_column_cid == column.cid); + let idx = _.findIndex(currColumns, (cc)=>cc.local_column == column.name); if(idx > -1) { currColumns[idx].local_column = _.get(topState, tabColPath).name; } diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/primary_key.ui.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/primary_key.ui.js index b0bc8c01d..b61e6a090 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/primary_key.ui.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/primary_key.ui.js @@ -86,13 +86,14 @@ export default class PrimaryKeySchema extends BaseUISchema { if(obj.inTable && source[0] == 'columns') { if(actionObj.type == SCHEMA_STATE_ACTIONS.DELETE_ROW) { let oldColumn = _.get(actionObj.oldState, actionObj.path.concat(actionObj.value)); - currColumns = _.filter(currColumns, (cc)=>cc.cid != oldColumn.cid); + currColumns = _.filter(currColumns, (cc)=>cc.column != oldColumn.name); } else if(actionObj.type == SCHEMA_STATE_ACTIONS.SET_VALUE) { let tabColPath = _.slice(actionObj.path, 0, -1); let oldCol = _.get(actionObj.oldState, tabColPath); - let idx = _.findIndex(currColumns, (cc)=>cc.cid == oldCol.cid); - if(idx > -1) { - currColumns[idx].column = _.get(topState, tabColPath).name; + let idx = _.findIndex(currColumns, (cc)=>cc.column == oldCol.name); + let updatedCol = _.get(topState, tabColPath); + if(idx > -1 && updatedCol.is_primary_key) { + currColumns[idx].column = updatedCol.name; } } } diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/unique_constraint.ui.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/unique_constraint.ui.js index 632193db5..f13b73843 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/unique_constraint.ui.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/unique_constraint.ui.js @@ -88,11 +88,11 @@ export default class UniqueConstraintSchema extends BaseUISchema { if(obj.inTable && source[0] == 'columns') { if(actionObj.type == SCHEMA_STATE_ACTIONS.DELETE_ROW) { let oldColumn = _.get(actionObj.oldState, actionObj.path.concat(actionObj.value)); - currColumns = _.filter(currColumns, (cc)=>cc.cid != oldColumn.cid); + currColumns = _.filter(currColumns, (cc)=>cc.column != oldColumn.name); } else if(actionObj.type == SCHEMA_STATE_ACTIONS.SET_VALUE) { let tabColPath = _.slice(actionObj.path, 0, -1); let oldCol = _.get(actionObj.oldState, tabColPath); - let idx = _.findIndex(currColumns, (cc)=>cc.cid == oldCol.cid); + let idx = _.findIndex(currColumns, (cc)=>cc.column == oldCol.name); if(idx > -1) { currColumns[idx].column = _.get(topState, tabColPath).name; } diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/static/js/table.ui.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/static/js/table.ui.js index d79d2e8a7..92ee4a662 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/static/js/table.ui.js +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/static/js/table.ui.js @@ -448,7 +448,7 @@ export default class TableSchema extends BaseUISchema { } changeColumnOptions(columns) { - let colOptions = (columns||[]).map((c)=>({label: c.name, value: c.name, image:'icon-column', cid:c.cid})); + let colOptions = (columns||[]).map((c)=>({label: c.name, value: c.name, image:'icon-column'})); this.constraintsObj.changeColumnOptions(colOptions); this.partitionKeysObj.changeColumnOptions(colOptions); this.partitionsObj.changeColumnOptions(colOptions); @@ -682,12 +682,11 @@ export default class TableSchema extends BaseUISchema { let currPk = state.primary_key[0]; /* If col is not PK, remove it */ if(!columnData.is_primary_key) { - currPk.columns = _.filter(currPk.columns, (c)=>c.cid !== columnData.cid); + currPk.columns = _.filter(currPk.columns, (c)=>c.column !== columnData.name); } else { - currPk.columns = _.filter(currPk.columns, (c)=>c.cid !== columnData.cid); + currPk.columns = _.filter(currPk.columns, (c)=>c.column !== columnData.name); currPk.columns.push({ column: columnData.name, - cid: columnData.cid, }); } /* Remove the PK if all columns not PK */ @@ -700,7 +699,8 @@ export default class TableSchema extends BaseUISchema { /* Create PK if none */ return {primary_key: [ obj.constraintsObj.primaryKeyObj.getNewData({ - columns: [{column: columnData.name, cid: columnData.cid}], + columns: [{column: columnData.name, + }], }) ]}; } diff --git a/web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js b/web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js index 49b2e7063..748f17f5f 100644 --- a/web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js +++ b/web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js @@ -368,7 +368,7 @@ export default class ERDCore { fkTableNode.getData().foreign_key?.forEach((theFkRow)=>{ for(let fkColumn of theFkRow.columns) { if(fkColumn.references == tableNode.getID()) { - let attnum = _.find(oldTableData.columns, (c)=>c.name==fkColumn.referenced).attnum; + let attnum = _.find(oldTableData.columns, (c)=>c.name==fkColumn.referenced)?.attnum; fkColumn.referenced = _.find(tableData.columns, (colm)=>colm.attnum==attnum).name; fkColumn.references_table_name = tableData.name; } @@ -413,8 +413,8 @@ export default class ERDCore { let tableNodesDict = this.getModel().getNodesDict(); let sourceNode = tableNodesDict[theFk.references]; - let localAttnum = _.find(tableNode.getColumns(), (col)=>col.name==theFk.local_column).attnum; - let refAttnum = _.find(sourceNode.getColumns(), (col)=>col.name==theFk.referenced).attnum; + let localAttnum = _.find(tableNode.getColumns(), (col)=>col.name==theFk.local_column)?.attnum; + let refAttnum = _.find(sourceNode.getColumns(), (col)=>col.name==theFk.referenced)?.attnum; const fkLink = Object.values(tableNode.getLinks()).find((link)=>{ const ldata = link.getData(); return ldata.local_column_attnum == localAttnum diff --git a/web/regression/javascript/schema_ui_files/exclusion_constraint.ui.spec.js b/web/regression/javascript/schema_ui_files/exclusion_constraint.ui.spec.js index 0e01d3ed9..6acc967fb 100644 --- a/web/regression/javascript/schema_ui_files/exclusion_constraint.ui.spec.js +++ b/web/regression/javascript/schema_ui_files/exclusion_constraint.ui.spec.js @@ -106,7 +106,6 @@ describe('ExclusionConstraintSchema', ()=>{ is_exp: true, column: 'abc', col_type: null, - column_cid: null, }); }); }); @@ -126,14 +125,14 @@ describe('ExclusionConstraintSchema', ()=>{ }); it('depChange', ()=>{ - let state = {columns: [{column: 'id', column_cid: 'c123'}]}; + let state = {columns: [{column: 'id'}]}; schemaObj.top = new TableSchema({}, null); expect(getFieldDepChange(schemaObj, 'columns')(state, ['columns', 0], null, { type: SCHEMA_STATE_ACTIONS.DELETE_ROW, oldState: { columns: [ - {name: 'id', cid: 'c123'} + {name: 'id'} ], }, path: ['columns'], @@ -144,19 +143,19 @@ describe('ExclusionConstraintSchema', ()=>{ expect(getFieldDepChange(schemaObj, 'columns')(state, ['columns', 0], { columns: [ - {name: 'id123', cid: 'c123'} + {name: 'id123'} ], }, { type: SCHEMA_STATE_ACTIONS.SET_VALUE, oldState: { columns: [ - {name: 'id', cid: 'c123'} + {name: 'id'} ], }, path: ['columns', 0, 'name'], value: 'id123', })).toEqual({ - columns: [{column: 'id123', column_cid: 'c123'}], + columns: [{column: 'id123'}], }); state = {}; diff --git a/web/regression/javascript/schema_ui_files/primary_key.ui.spec.js b/web/regression/javascript/schema_ui_files/primary_key.ui.spec.js index e04aa5990..35294fe5d 100644 --- a/web/regression/javascript/schema_ui_files/primary_key.ui.spec.js +++ b/web/regression/javascript/schema_ui_files/primary_key.ui.spec.js @@ -122,7 +122,7 @@ describe('PrimaryKeySchema', ()=>{ expect(getFieldDepChange(schemaObj, 'columns')(state, ['columns', 0], { columns: [ - {name: 'id123'} + {name: 'id123', is_primary_key: true} ], }, { type: SCHEMA_STATE_ACTIONS.SET_VALUE,