Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync include columns with column name changes.#7617 #8348

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is required. The problem is with syncing the column names. Normal columns works without any extra variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isInclude variable helps track which columns are added to the "Include Column" field in the create table modal, especially when column names change. This works similarly to the is_primary_key variable that tracks columns in the model's column field. This helps maintain proper tracking of column states. Let me know if you'd prefer a different approach!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any is_foreign_key or is_unique_key variables. I don't think it is the right direction.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then what is the use of include_columns key?

});

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