Skip to content

Commit 4d28fe2

Browse files
Sync include columns with column name changes.#7617
1 parent de6fbe7 commit 4d28fe2

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.ui.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default class ColumnSchema extends BaseUISchema {
6060
seqcycle: undefined,
6161
colconstype: 'n',
6262
genexpr: undefined,
63+
isInclude: false
6364
});
6465

6566
this.getPrivilegeRoleSchema = getPrivilegeRoleSchema;
@@ -382,7 +383,12 @@ export default class ColumnSchema extends BaseUISchema {
382383
id: 'attstattarget', label: gettext('Statistics'), cell: 'text',
383384
type: 'text', readonly: obj.inSchemaWithColumnCheck, mode: ['properties', 'edit'],
384385
group: gettext('Definition'),
385-
},{
386+
},
387+
{
388+
// Use to check include columns.
389+
id: 'isInclude', label: gettext(''), type: 'boolean', visible: false
390+
},
391+
{
386392
id: 'attstorage', label: gettext('Storage'), group: gettext('Definition'),
387393
type: 'select', mode: ['properties', 'edit', 'create'],
388394
cell: 'select', readonly: obj.inSchemaWithColumnCheck,

web/pgadmin/browser/server_groups/servers/databases/schemas/tables/static/js/table.ui.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,33 @@ export class ConstraintsSchema extends BaseUISchema {
129129
return {primary_key: []};
130130
}
131131
/* If columns changed */
132+
if (actionObj.type === SCHEMA_STATE_ACTIONS.SET_VALUE && actionObj.path.at(-1) === 'name') {
133+
const primaryKey = state.primary_key[0] || {};
134+
/* Extract primary key columns and included columns in a single pass through state columns.*/
135+
const { primaryKeyColumns, includeColumns } = state.columns.reduce((acc, column) => {
136+
if (column.is_primary_key) acc.primaryKeyColumns.push({ column: column.name });
137+
if (column.isInclude) acc.includeColumns.push(column.name);
138+
return acc;
139+
}, { primaryKeyColumns: [], includeColumns: [] });
140+
/* Check if updates are needed. */
141+
const needsPrimaryKeyUpdate = JSON.stringify(primaryKeyColumns) !== JSON.stringify(primaryKey.columns || []);
142+
const needsIncludeUpdate = includeColumns.length > 0;
143+
/* Only update state if columns updated. */
144+
if (needsPrimaryKeyUpdate || needsIncludeUpdate) {
145+
primaryKey.columns = needsPrimaryKeyUpdate ? primaryKeyColumns : primaryKey.columns;
146+
primaryKey.include = needsIncludeUpdate ? includeColumns : primaryKey.include;
147+
return state;
148+
}
149+
}
150+
/* Update include columns. */
151+
if(actionObj.type == SCHEMA_STATE_ACTIONS.SET_VALUE && actionObj.path[actionObj.path.length - 1] === 'include'){
152+
let include = state.primary_key[0].include;
153+
state.columns = state.columns.map((c)=>({
154+
...c, isInclude: include.indexOf(c.name) > -1,
155+
}));
156+
return {columns: state.columns};
157+
}
158+
132159
if(actionObj.type == SCHEMA_STATE_ACTIONS.SET_VALUE && actionObj.path[actionObj.path.length-1] == 'columns') {
133160
/* Sync up the pk flag */
134161
let columns = state.primary_key[0].columns.map((c)=>c.column);

web/pgadmin/static/js/SchemaView/DataGridView/header.jsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ export function DataGridHeader({tableEleRef}) {
3636
const schemaState = useContext(SchemaStateContext);
3737

3838
const onAddClick = useCallback(() => {
39-
40-
if(!canAddRow) {
41-
return;
42-
}
43-
4439
const newRow = field.schema.getNewData();
4540

4641
newRowIndex.current = addOnTop ? 0 : rows.length;
@@ -91,6 +86,7 @@ export function DataGridHeader({tableEleRef}) {
9186
<PgIconButton data-test="add-row" title={gettext('Add row')}
9287
onClick={onAddClick}
9388
icon={<AddIcon />} className='DataGridView-gridControlsButton'
89+
disabled={!canAddRow}
9490
/>
9591
}
9692
</Box>

0 commit comments

Comments
 (0)