From 41f045d8a60fabd6015da7cdcda55925295829a3 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 26 Jun 2024 13:36:20 +0100 Subject: [PATCH] Allow constant internal columns to be duplicated based on being case sensitive. --- .../server/src/api/routes/tests/table.spec.ts | 9 +++------ packages/shared-core/src/table.ts | 17 +++++++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index e75e5e23e7..8102966ad1 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -285,12 +285,9 @@ describe.each([ type: FieldType.STRING, name: "Type", } + // allow the "Type" column - internal columns aren't case sensitive await config.api.table.save(saveTableRequest, { - status: 400, - body: { - message: - 'Column(s) "type" are duplicated - check for other columns with these name (case in-sensitive)', - }, + status: 200, }) saveTableRequest.schema.foo = { type: FieldType.STRING, name: "foo" } saveTableRequest.schema.FOO = { type: FieldType.STRING, name: "FOO" } @@ -299,7 +296,7 @@ describe.each([ status: 400, body: { message: - 'Column(s) "type, foo" are duplicated - check for other columns with these name (case in-sensitive)', + 'Column(s) "foo" are duplicated - check for other columns with these name (case in-sensitive)', }, }) }) diff --git a/packages/shared-core/src/table.ts b/packages/shared-core/src/table.ts index 4b578a2aef..8fd7909b18 100644 --- a/packages/shared-core/src/table.ts +++ b/packages/shared-core/src/table.ts @@ -54,20 +54,25 @@ export function canBeSortColumn(type: FieldType): boolean { } export function findDuplicateInternalColumns(table: Table): string[] { + // maintains the case of keys + const casedKeys = Object.keys(table.schema) // get the column names - const columnNames = Object.keys(table.schema) - .concat(CONSTANT_INTERNAL_ROW_COLS) - .map(colName => colName.toLowerCase()) + const uncasedKeys = casedKeys.map(colName => colName.toLowerCase()) // there are duplicates - const set = new Set(columnNames) + const set = new Set(uncasedKeys) let duplicates: string[] = [] - if (set.size !== columnNames.length) { + if (set.size !== uncasedKeys.length) { for (let key of set.keys()) { - const count = columnNames.filter(name => name === key).length + const count = uncasedKeys.filter(name => name === key).length if (count > 1) { duplicates.push(key) } } } + for (let internalColumn of CONSTANT_INTERNAL_ROW_COLS) { + if (casedKeys.find(key => key === internalColumn)) { + duplicates.push(internalColumn) + } + } return duplicates }