From 84a67bb3460a04999e62cdba9b5431eb0c9720c9 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 17 Dec 2024 18:42:19 +0100 Subject: [PATCH 1/2] Add test capturing --- .../server/src/api/routes/tests/row.spec.ts | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index fb728a3fea..a3012c3760 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -1333,6 +1333,62 @@ if (descriptions.length) { expect(resp.relationship.length).toBe(1) }) + it("should be able to keep linked data when updating from views that trims links from the main table", async () => { + let row = await config.api.row.save(table._id!, { + name: "main", + description: "main description", + }) + const row2 = await config.api.row.save(otherTable._id!, { + name: "link", + description: "link description", + relationship: [row._id], + }) + + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: "view", + schema: { + name: { visible: true }, + }, + }) + const resp = await config.api.row.patch(view.id, { + _id: row._id!, + _rev: row._rev!, + tableId: row.tableId!, + name: "test2", + relationship: [row2._id], + }) + expect(resp.relationship).toBeUndefined() + + const updatedRow = await config.api.row.get(table._id!, row._id!) + expect(updatedRow.relationship.length).toBe(1) + }) + + it("should be able to keep linked data when updating from views that trims links from the foreign table", async () => { + let row = await config.api.row.save(table._id!, { + name: "main", + description: "main description", + }) + const row2 = await config.api.row.save(otherTable._id!, { + name: "link", + description: "link description", + relationship: [row._id], + }) + + const view = await config.api.viewV2.create({ + tableId: otherTable._id!, + name: "view", + }) + await config.api.row.patch(view.id, { + _id: row2._id!, + _rev: row2._rev!, + tableId: row2.tableId!, + }) + + const updatedRow = await config.api.row.get(table._id!, row._id!) + expect(updatedRow.relationship.length).toBe(1) + }) + !isInternal && // MSSQL needs a setting called IDENTITY_INSERT to be set to ON to allow writing // to identity columns. This is not something Budibase does currently. From d061f44eda07dd1e08129da983edd5a5218299a7 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 17 Dec 2024 18:50:17 +0100 Subject: [PATCH 2/2] Fix external patches --- .../src/api/controllers/row/external.ts | 20 +++++++++++++------ .../src/api/controllers/row/utils/utils.ts | 15 ++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/server/src/api/controllers/row/external.ts b/packages/server/src/api/controllers/row/external.ts index 082d07283b..d56fb1a344 100644 --- a/packages/server/src/api/controllers/row/external.ts +++ b/packages/server/src/api/controllers/row/external.ts @@ -52,10 +52,22 @@ export async function patch(ctx: UserCtx) { const table = await utils.getTableFromSource(source) const { _id, ...rowData } = ctx.request.body - const dataToUpdate = await inputProcessing( + const beforeRow = await sdk.rows.external.getRow(table._id!, _id, { + relationships: true, + }) + + let dataToUpdate = cloneDeep(beforeRow) + const allowedField = utils.getSourceFields(source) + for (const key of Object.keys(rowData)) { + if (!allowedField.includes(key)) continue + + dataToUpdate[key] = rowData[key] + } + + dataToUpdate = await inputProcessing( ctx.user?._id, cloneDeep(source), - rowData + dataToUpdate ) const validateResult = await sdk.rows.utils.validate({ @@ -66,10 +78,6 @@ export async function patch(ctx: UserCtx) { throw { validation: validateResult.errors } } - const beforeRow = await sdk.rows.external.getRow(table._id!, _id, { - relationships: true, - }) - const response = await handleRequest(Operation.UPDATE, source, { id: breakRowIdField(_id), row: dataToUpdate, diff --git a/packages/server/src/api/controllers/row/utils/utils.ts b/packages/server/src/api/controllers/row/utils/utils.ts index baa811fe90..9d1711d000 100644 --- a/packages/server/src/api/controllers/row/utils/utils.ts +++ b/packages/server/src/api/controllers/row/utils/utils.ts @@ -110,6 +110,21 @@ function fixBooleanFields(row: Row, table: Table) { return row } +export function getSourceFields(source: Table | ViewV2): string[] { + const isView = sdk.views.isView(source) + if (isView) { + const fields = Object.keys( + helpers.views.basicFields(source, { visible: true }) + ) + return fields + } + + const fields = Object.entries(source.schema) + .filter(([_, field]) => field.visible !== false) + .map(([columnName]) => columnName) + return fields +} + export async function sqlOutputProcessing( rows: DatasourcePlusQueryResponse, source: Table | ViewV2,