From dbca9ff039c04cbdf7518f74ba22896227eedf3c Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Thu, 2 Jan 2025 13:09:31 +0530 Subject: [PATCH] fix: rename operation duplication (#373) --- frontend/src2/query/query.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/frontend/src2/query/query.ts b/frontend/src2/query/query.ts index 2306e4f0..ec2b265a 100644 --- a/frontend/src2/query/query.ts +++ b/frontend/src2/query/query.ts @@ -438,24 +438,38 @@ export function makeQuery(workbookQuery: WorkbookQuery) { } function renameColumn(oldName: string, newName: string) { - // first check if there's already a rename operation for the column const existingRenameIdx = query.currentOperations.findIndex( (op) => op.type === 'rename' && op.new_name === oldName ) - if (existingRenameIdx > -1) { - const existingRename = query.currentOperations[existingRenameIdx] as Rename - existingRename.new_name = newName - } - // if not, add a new rename operation - else { + if (existingRenameIdx === -1) { + // No existing rename, add new one addOperation( rename({ column: column(oldName), new_name: newName, }) ) + return + } + + const existingRename = query.currentOperations[existingRenameIdx] as Rename + const originalColumnName = existingRename.column.column_name + + // If renaming back to original name, remove the rename operation + if (originalColumnName === newName) { + removeOperation(existingRenameIdx) + return } + + // Update existing rename operation + query.doc.operations.splice(existingRenameIdx, 1) + addOperation( + rename({ + column: column(originalColumnName), + new_name: newName, + }) + ) } function removeColumn(column_names: string | string[]) {