From 7f2ab8b1ae56c83517c4d42d62b21620fa00628d Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 11 Oct 2023 12:29:43 +0100 Subject: [PATCH] Make sure table gets saved after bulkImport if it has changed. This fixes auto ID columns having the wrong lastID. --- .../server/src/api/controllers/table/external.ts | 1 + .../server/src/api/controllers/table/index.ts | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/controllers/table/external.ts b/packages/server/src/api/controllers/table/external.ts index 327904666d..ad6a56ee27 100644 --- a/packages/server/src/api/controllers/table/external.ts +++ b/packages/server/src/api/controllers/table/external.ts @@ -376,6 +376,7 @@ export async function destroy(ctx: UserCtx) { export async function bulkImport(ctx: UserCtx) { const table = await sdk.tables.getTable(ctx.params.tableId) + const { rows }: { rows: unknown } = ctx.request.body const schema: unknown = table.schema diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index e7c6ae57b0..aa3f7cc702 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -6,7 +6,7 @@ import { validate as validateSchema, } from "../../../utilities/schema" import { isExternalTable, isSQL } from "../../../integrations/utils" -import { events } from "@budibase/backend-core" +import { context, events } from "@budibase/backend-core" import { FetchTablesResponse, SaveTableRequest, @@ -18,7 +18,7 @@ import { import sdk from "../../../sdk" import { jsonFromCsvString } from "../../../utilities/csv" import { builderSocket } from "../../../websockets" -import { cloneDeep } from "lodash" +import { cloneDeep, isEqual } from "lodash" function pickApi({ tableId, table }: { tableId?: string; table?: Table }) { if (table && !tableId) { @@ -99,7 +99,16 @@ export async function destroy(ctx: UserCtx) { export async function bulkImport(ctx: UserCtx) { const tableId = ctx.params.tableId - await pickApi({ tableId }).bulkImport(ctx) + let db = context.getAppDB() + let tableBefore = await sdk.tables.getTable(tableId) + let tableAfter = await pickApi({ tableId }).bulkImport(ctx) + + if (!isEqual(tableBefore, tableAfter)) { + await db.put(tableAfter) + ctx.eventEmitter && + ctx.eventEmitter.emitTable(`table:save`, ctx.appId, tableAfter) + } + // right now we don't trigger anything for bulk import because it // can only be done in the builder, but in the future we may need to // think about events for bulk items