From a05ec1498f4e6d5b01b5657b6603b1bdb02da16d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 26 Jul 2024 13:27:51 +0200 Subject: [PATCH 1/3] Allow importing internal tables --- .../server/src/api/controllers/table/internal.ts | 3 +++ .../server/src/sdk/app/tables/internal/index.ts | 5 ++++- packages/shared-core/src/table.ts | 13 +++++++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/server/src/api/controllers/table/internal.ts b/packages/server/src/api/controllers/table/internal.ts index a06cc4dee3..4286d51d3e 100644 --- a/packages/server/src/api/controllers/table/internal.ts +++ b/packages/server/src/api/controllers/table/internal.ts @@ -25,6 +25,8 @@ export async function save( sourceType: rest.sourceType || TableSourceType.INTERNAL, } + const isImport = !!rows + if (!tableToSave.views) { tableToSave.views = {} } @@ -35,6 +37,7 @@ export async function save( rowsToImport: rows, tableId: ctx.request.body._id, renaming, + isImport, }) return table diff --git a/packages/server/src/sdk/app/tables/internal/index.ts b/packages/server/src/sdk/app/tables/internal/index.ts index fc32708708..504b73e496 100644 --- a/packages/server/src/sdk/app/tables/internal/index.ts +++ b/packages/server/src/sdk/app/tables/internal/index.ts @@ -31,6 +31,7 @@ export async function save( tableId?: string rowsToImport?: Row[] renaming?: RenameColumn + isImport: boolean } ) { const db = context.getAppDB() @@ -47,7 +48,9 @@ export async function save( } // check for case sensitivity - we don't want to allow duplicated columns - const duplicateColumn = findDuplicateInternalColumns(table) + const duplicateColumn = findDuplicateInternalColumns(table, { + internalRowsAllowed: !!opts?.isImport, + }) if (duplicateColumn.length) { throw new Error( `Column(s) "${duplicateColumn.join( diff --git a/packages/shared-core/src/table.ts b/packages/shared-core/src/table.ts index 8fd7909b18..da0df370bb 100644 --- a/packages/shared-core/src/table.ts +++ b/packages/shared-core/src/table.ts @@ -53,7 +53,10 @@ export function canBeSortColumn(type: FieldType): boolean { return !!allowSortColumnByType[type] } -export function findDuplicateInternalColumns(table: Table): string[] { +export function findDuplicateInternalColumns( + table: Table, + opts?: { internalRowsAllowed: boolean } +): string[] { // maintains the case of keys const casedKeys = Object.keys(table.schema) // get the column names @@ -69,9 +72,11 @@ export function findDuplicateInternalColumns(table: Table): string[] { } } } - for (let internalColumn of CONSTANT_INTERNAL_ROW_COLS) { - if (casedKeys.find(key => key === internalColumn)) { - duplicates.push(internalColumn) + if (!opts?.internalRowsAllowed) { + for (let internalColumn of CONSTANT_INTERNAL_ROW_COLS) { + if (casedKeys.find(key => key === internalColumn)) { + duplicates.push(internalColumn) + } } } return duplicates From d7e2f0fc0ef8af7fa6c7b2a483c68a57415d739a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 26 Jul 2024 13:42:33 +0200 Subject: [PATCH 2/3] Fix build --- packages/server/src/sdk/app/tables/internal/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/sdk/app/tables/internal/index.ts b/packages/server/src/sdk/app/tables/internal/index.ts index 504b73e496..293eaa797c 100644 --- a/packages/server/src/sdk/app/tables/internal/index.ts +++ b/packages/server/src/sdk/app/tables/internal/index.ts @@ -31,7 +31,7 @@ export async function save( tableId?: string rowsToImport?: Row[] renaming?: RenameColumn - isImport: boolean + isImport?: boolean } ) { const db = context.getAppDB() From 4cf37069040687b8c90927265906faa820aa8cd8 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 26 Jul 2024 14:29:20 +0200 Subject: [PATCH 3/3] Renamings --- packages/server/src/sdk/app/tables/internal/index.ts | 2 +- packages/shared-core/src/table.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/server/src/sdk/app/tables/internal/index.ts b/packages/server/src/sdk/app/tables/internal/index.ts index 293eaa797c..58f172e016 100644 --- a/packages/server/src/sdk/app/tables/internal/index.ts +++ b/packages/server/src/sdk/app/tables/internal/index.ts @@ -49,7 +49,7 @@ export async function save( // check for case sensitivity - we don't want to allow duplicated columns const duplicateColumn = findDuplicateInternalColumns(table, { - internalRowsAllowed: !!opts?.isImport, + ignoreProtectedColumnNames: !oldTable && !!opts?.isImport, }) if (duplicateColumn.length) { throw new Error( diff --git a/packages/shared-core/src/table.ts b/packages/shared-core/src/table.ts index da0df370bb..b463fe7bfb 100644 --- a/packages/shared-core/src/table.ts +++ b/packages/shared-core/src/table.ts @@ -55,7 +55,7 @@ export function canBeSortColumn(type: FieldType): boolean { export function findDuplicateInternalColumns( table: Table, - opts?: { internalRowsAllowed: boolean } + opts?: { ignoreProtectedColumnNames: boolean } ): string[] { // maintains the case of keys const casedKeys = Object.keys(table.schema) @@ -72,7 +72,7 @@ export function findDuplicateInternalColumns( } } } - if (!opts?.internalRowsAllowed) { + if (!opts?.ignoreProtectedColumnNames) { for (let internalColumn of CONSTANT_INTERNAL_ROW_COLS) { if (casedKeys.find(key => key === internalColumn)) { duplicates.push(internalColumn)