From f4e14db22a361ee1dd9b5d6696a0ccc2592ceff0 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 7 Jun 2024 10:54:14 +0200 Subject: [PATCH 1/5] Add failing test --- .../server/src/api/routes/tests/row.spec.ts | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 4801ac4c55..ccfd3891c5 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -38,7 +38,7 @@ describe.each([ [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], -])("/rows (%s)", (__, dsProvider) => { +])("/rows (%s)", (providerType, dsProvider) => { const isInternal = dsProvider === undefined const config = setup.getConfig() @@ -693,6 +693,49 @@ describe.each([ }) expect(resp.relationship.length).toBe(1) }) + + !isInternal && + // TODO: SQL is having issues creating composite keys + providerType !== DatabaseName.SQL_SERVER && + it("should support updating fields that are part of a composite key", async () => { + const tableRequest = saveTableRequest({ + primary: ["number", "string"], + schema: { + string: { + type: FieldType.STRING, + name: "string", + }, + number: { + type: FieldType.NUMBER, + name: "number", + }, + }, + }) + + delete tableRequest.schema.id + + const table = await config.api.table.save(tableRequest) + + const stringValue = generator.word() + const naturalValue = generator.integer({ min: 0, max: 1000 }) + + const existing = await config.api.row.save(table._id!, { + string: stringValue, + number: naturalValue, + }) + + expect(existing._id).toEqual(`%5B${naturalValue}%2C'${stringValue}'%5D`) + + const row = await config.api.row.patch(table._id!, { + _id: existing._id!, + _rev: existing._rev!, + tableId: table._id!, + string: stringValue, + number: 1500, + }) + + expect(row._id).toEqual(`%5B${"1500"}%2C'${stringValue}'%5D`) + }) }) describe("destroy", () => { From b86501e25eb7ec109377dec7cecc0527caecf80c Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 7 Jun 2024 10:54:26 +0200 Subject: [PATCH 2/5] Fix --- packages/server/src/api/controllers/row/external.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/controllers/row/external.ts b/packages/server/src/api/controllers/row/external.ts index e0e3cb6c18..51511840e8 100644 --- a/packages/server/src/api/controllers/row/external.ts +++ b/packages/server/src/api/controllers/row/external.ts @@ -25,6 +25,7 @@ import { outputProcessing, } from "../../../utilities/rowProcessor" import { cloneDeep } from "lodash" +import { generateIdForRow } from "./utils" export async function handleRequest( operation: T, @@ -59,7 +60,9 @@ export async function patch(ctx: UserCtx) { id: breakRowIdField(_id), row: dataToUpdate, }) - const row = await sdk.rows.external.getRow(tableId, _id, { + + const updatedId = generateIdForRow(dataToUpdate, table) + const row = await sdk.rows.external.getRow(tableId, updatedId, { relationships: true, }) const enrichedRow = await outputProcessing(table, row, { From b22ea1b0f9e9363e0164bb2f07a9cbd581d76759 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 7 Jun 2024 11:11:32 +0200 Subject: [PATCH 3/5] Fix test --- packages/server/src/api/controllers/row/external.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/controllers/row/external.ts b/packages/server/src/api/controllers/row/external.ts index 51511840e8..f57a52a307 100644 --- a/packages/server/src/api/controllers/row/external.ts +++ b/packages/server/src/api/controllers/row/external.ts @@ -61,7 +61,8 @@ export async function patch(ctx: UserCtx) { row: dataToUpdate, }) - const updatedId = generateIdForRow(dataToUpdate, table) + // The id might have been changed, so the refetching would fail. Recalculating the id just in case + const updatedId = generateIdForRow({ _id, ...dataToUpdate }, table) const row = await sdk.rows.external.getRow(tableId, updatedId, { relationships: true, }) From 20d1b81099408de8455ad2c8c6410c32d65d9477 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 7 Jun 2024 11:30:35 +0200 Subject: [PATCH 4/5] Fix tests --- packages/server/src/api/controllers/row/external.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/api/controllers/row/external.ts b/packages/server/src/api/controllers/row/external.ts index f57a52a307..32bd1f313a 100644 --- a/packages/server/src/api/controllers/row/external.ts +++ b/packages/server/src/api/controllers/row/external.ts @@ -62,7 +62,7 @@ export async function patch(ctx: UserCtx) { }) // The id might have been changed, so the refetching would fail. Recalculating the id just in case - const updatedId = generateIdForRow({ _id, ...dataToUpdate }, table) + const updatedId = generateIdForRow(ctx.request.body, table) const row = await sdk.rows.external.getRow(tableId, updatedId, { relationships: true, }) From 853810704c22e5d4a604476209adeb348d83e03b Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 7 Jun 2024 11:39:36 +0200 Subject: [PATCH 5/5] Fix patch --- packages/server/src/api/controllers/row/external.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/controllers/row/external.ts b/packages/server/src/api/controllers/row/external.ts index 32bd1f313a..d301155231 100644 --- a/packages/server/src/api/controllers/row/external.ts +++ b/packages/server/src/api/controllers/row/external.ts @@ -56,13 +56,18 @@ export async function patch(ctx: UserCtx) { throw { validation: validateResult.errors } } + const beforeRow = await sdk.rows.external.getRow(tableId, _id, { + relationships: true, + }) + const response = await handleRequest(Operation.UPDATE, tableId, { id: breakRowIdField(_id), row: dataToUpdate, }) // The id might have been changed, so the refetching would fail. Recalculating the id just in case - const updatedId = generateIdForRow(ctx.request.body, table) + const updatedId = + generateIdForRow({ ...beforeRow, ...dataToUpdate }, table) || _id const row = await sdk.rows.external.getRow(tableId, updatedId, { relationships: true, })