Skip to content

Commit

Permalink
Sync include columns with column name changes.#7617
Browse files Browse the repository at this point in the history
  • Loading branch information
RohitBhati8269 committed Jan 9, 2025
1 parent de6fbe7 commit 4d28fe2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default class ColumnSchema extends BaseUISchema {
seqcycle: undefined,
colconstype: 'n',
genexpr: undefined,
isInclude: false
});

this.getPrivilegeRoleSchema = getPrivilegeRoleSchema;
Expand Down Expand Up @@ -382,7 +383,12 @@ export default class ColumnSchema extends BaseUISchema {
id: 'attstattarget', label: gettext('Statistics'), cell: 'text',
type: 'text', readonly: obj.inSchemaWithColumnCheck, mode: ['properties', 'edit'],
group: gettext('Definition'),
},{
},
{
// Use to check include columns.
id: 'isInclude', label: gettext(''), type: 'boolean', visible: false
},
{
id: 'attstorage', label: gettext('Storage'), group: gettext('Definition'),
type: 'select', mode: ['properties', 'edit', 'create'],
cell: 'select', readonly: obj.inSchemaWithColumnCheck,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,33 @@ export class ConstraintsSchema extends BaseUISchema {
return {primary_key: []};
}
/* If columns changed */
if (actionObj.type === SCHEMA_STATE_ACTIONS.SET_VALUE && actionObj.path.at(-1) === 'name') {
const primaryKey = state.primary_key[0] || {};
/* Extract primary key columns and included columns in a single pass through state columns.*/
const { primaryKeyColumns, includeColumns } = state.columns.reduce((acc, column) => {
if (column.is_primary_key) acc.primaryKeyColumns.push({ column: column.name });
if (column.isInclude) acc.includeColumns.push(column.name);
return acc;
}, { primaryKeyColumns: [], includeColumns: [] });
/* Check if updates are needed. */
const needsPrimaryKeyUpdate = JSON.stringify(primaryKeyColumns) !== JSON.stringify(primaryKey.columns || []);
const needsIncludeUpdate = includeColumns.length > 0;
/* Only update state if columns updated. */
if (needsPrimaryKeyUpdate || needsIncludeUpdate) {
primaryKey.columns = needsPrimaryKeyUpdate ? primaryKeyColumns : primaryKey.columns;
primaryKey.include = needsIncludeUpdate ? includeColumns : primaryKey.include;
return state;
}
}
/* Update include columns. */
if(actionObj.type == SCHEMA_STATE_ACTIONS.SET_VALUE && actionObj.path[actionObj.path.length - 1] === 'include'){
let include = state.primary_key[0].include;
state.columns = state.columns.map((c)=>({
...c, isInclude: include.indexOf(c.name) > -1,
}));
return {columns: state.columns};
}

if(actionObj.type == SCHEMA_STATE_ACTIONS.SET_VALUE && actionObj.path[actionObj.path.length-1] == 'columns') {
/* Sync up the pk flag */
let columns = state.primary_key[0].columns.map((c)=>c.column);
Expand Down
6 changes: 1 addition & 5 deletions web/pgadmin/static/js/SchemaView/DataGridView/header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ export function DataGridHeader({tableEleRef}) {
const schemaState = useContext(SchemaStateContext);

const onAddClick = useCallback(() => {

if(!canAddRow) {
return;
}

const newRow = field.schema.getNewData();

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

0 comments on commit 4d28fe2

Please sign in to comment.