Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(tables): Create Record(s) no longer saves the same rows twice after a network hiccup (#16464)

Bartosz Majewski1b9459bf0a5d
diff --git a/brain/knowledge/data-storage-observability/tables.md b/brain/knowledge/data-storage-observability/tables.md
index 5dfac4a3e9e..2947b8309d9 100644
--- a/brain/knowledge/data-storage-observability/tables.md
+++ b/brain/knowledge/data-storage-observability/tables.md
@@ -25,6 +25,7 @@ A built-in relational database inside Activepieces: users store structured data
- Only `GT/GTE/LT/LTE` are date-aware, and only because `doesCellValueMatchFilters` is passed the field's type; every other operator compares the raw string. So `EQ` on a date column will not match two different spellings of the same instant (`…T14:30:00Z` vs `…T14:30:00.000Z`). Filters on a field type the evaluator can't resolve fall back to `parseFloat`.
- Adding a `FieldType` member means editing the enum **and** the non-dropdown `z.union([...])` branch in three places (`core/shared/.../field.ts`, `.../dto/fields.dto.ts`, `core/piece-types/.../tables.ts`) — miss the union and `POST /v1/fields` rejects the type. Also add a `case` to `field.service.createFromState`, whose `default:` throws `Unsupported field type` and which every template / project-release / MCP table-create path routes through. No migration is needed: `field.type` is a plain varchar with no Postgres enum or CHECK constraint.
- `record.create()` bulk insert caps at 50 per batch, transactional.
+- The piece's Create Record(s) action must not pass `retries` to `httpClient`: `POST /v1/records` is not idempotent, and a retry after a committed insert whose reply was lost (a proxy 5xx, a dropped connection) inserts the rows twice (ENG-800). The same lost-reply retry is not free on the actions that keep `retries: 5` either: Delete Record(s) then fails with a 404 for rows it already deleted, and Update Record fires the updated-record webhook twice.
- When adding any new table/field/record route, the `permission` arg to `securityAccess.project(...)` is required — passing `undefined` silently allows any project member.
- The per-create `field.validateCount()` check alone races on bulk paths: all concurrent creates read the same pre-save count and pass. Bulk/import paths (`table.create` with fields, project-state/project-replace apply) MUST call it once up front with the batch size **before** their `Promise.all`.
- The web table store keys each cell by the column's position (`fieldIndex`), not by field id, so every field mutator in `ap-tables-client-state.tsx` has to renumber surviving cells (`deleteField` didn't, see ENG-537). A cell edit sends only the edited cell, and `record.update` upserts just those cells, so stale indexes can't blank other columns on the server.
diff --git a/bun.lock b/bun.lock
index 0d5bdaa41e8..6b62b128ce2 100644
--- a/bun.lock
+++ b/bun.lock
@@ -10790,7 +10790,7 @@
},
"packages/pieces/core/tables": {
"name": "@activepieces/piece-tables",
- "version": "0.5.2",
+ "version": "0.5.3",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
@@ -10802,6 +10802,7 @@
"devDependencies": {
"@types/qs": "6.14.0",
"tslib": "2.6.2",
+ "vitest": "3.2.6",
},
},
"packages/pieces/core/tags": {
diff --git a/packages/pieces/core/tables/package.json b/packages/pieces/core/tables/package.json
index 7e3c0cc0537..778a78d7b8f 100644
--- a/packages/pieces/core/tables/package.json
+++ b/packages/pieces/core/tables/package.json
@@ -1,12 +1,13 @@
{
"name": "@activepieces/piece-tables",
- "version": "0.5.2",
+ "version": "0.5.3",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"scripts": {
"build": "tsc -p tsconfig.lib.json && cp package.json dist/",
"bundle": "node ../../../../dist/packages/cli/src/index.js pieces bundle",
- "lint": "eslint 'src/**/*.ts'"
+ "lint": "eslint 'src/**/*.ts'",
+ "test": "vitest run"
},
"dependencies": {
"@activepieces/pieces-common": "workspace:*",
@@ -18,6 +19,7 @@
},
"devDependencies": {
"@types/qs": "6.14.0",
- "tslib": "2.6.2"
+ "tslib": "2.6.2",
+ "vitest": "3.2.6"
}
}
diff --git a/packages/pieces/core/tables/src/lib/actions/create-records.ts b/packages/pieces/core/tables/src/lib/actions/create-records.ts
index 23623fb1e0b..7630a5139ca 100644
--- a/packages/pieces/core/tables/src/lib/actions/create-records.ts
+++ b/packages/pieces/core/tables/src/lib/actions/create-records.ts
@@ -109,7 +109,6 @@ export const createRecords = createAction({
type: AuthenticationType.BEARER_TOKEN,
token: context.server.token,
},
- retries: 5,
});
return response.body.map(tablesCommon.formatRecord);
diff --git a/packages/pieces/core/tables/test/create-records-retry.test.ts b/packages/pieces/core/tables/test/create-records-retry.test.ts
new file mode 100644
index 00000000000..89c6bee0da0
--- /dev/null
+++ b/packages/pieces/core/tables/test/create-records-retry.test.ts
@@ -0,0 +1,80 @@
+/// <reference types="vitest/globals" />
+
+import http from 'node:http';
+import { createMockActionContext } from '@activepieces/pieces-framework';
+import { createRecords } from '../src/lib/actions/create-records';
+
+let server: http.Server;
+let apiUrl: string;
+let lostReply: LostReply | null = null;
+let recordPosts = 0;
+
+beforeAll(async () => {
+ server = http.createServer((req, res) => {
+ let body = '';
+ req.on('data', (chunk) => (body += chunk));
+ req.on('end', () => {
+ const send = ({ status, payload }: { status: number; payload: unknown }) => {
+ res.writeHead(status, { 'content-type': 'application/json' });
+ res.end(JSON.stringify(payload));
+ };
+ if (req.method === 'GET' && req.url?.startsWith('/v1/tables')) {
+ return send({ status: 200, payload: { data: [{ id: 'tbl_1', externalId: 'ext_1', name: 'T' }], next: null, previous: null } });
+ }
+ if (req.method === 'GET' && req.url?.startsWith('/v1/fields')) {
+ return send({ status: 200, payload: [{ id: 'fld_1', name: 'Name', externalId: 'name', type: 'TEXT', tableId: 'tbl_1' }] });
+ }
+ if (req.method === 'POST' && req.url === '/v1/records') {
+ recordPosts++;
+ if (recordPosts === 1 && lostReply === 'proxy 503') {
+ return send({ status: 503, payload: { message: 'upstream unavailable' } });
+ }
+ if (recordPosts === 1 && lostReply === 'dropped connection') {
+ return req.socket.destroy();
+ }
+ const records: unknown[] = JSON.parse(body).records;
+ return send({ status: 201, payload: records.map((_, i) => ({ id: `rec_${i}`, created: '', updated: '', cells: {} })) });
+ }
+ send({ status: 404, payload: {} });
+ });
+ });
+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
+ const address = server.address();
+ if (address === null || typeof address === 'string') {
+ throw new Error('test server has no port');
+ }
+ apiUrl = `http://127.0.0.1:${address.port}/`;
+});
+
+afterAll(() => server.close());
+
+beforeEach(() => {
+ lostReply = null;
+ recordPosts = 0;
+});
+
+function runCreateRecords() {
+ const base = createMockActionContext<typeof createRecords.props>({
+ propsValue: { table_id: 'ext_1', values: {}, records: { Name: 'Ada' } },
+ });
+ return createRecords.run({ ...base, server: { ...base.server, apiUrl } });
+}
+
+describe('Tables "Create Record(s)"', () => {
+ test.each<LostReply>(['proxy 503', 'dropped connection'])(
+ 'fails without inserting again when a committed insert loses its reply (%s)',
+ async (reply) => {
+ lostReply = reply;
+ const error = await runCreateRecords().catch((e: unknown) => e);
+ expect(recordPosts).toBe(1);
+ expect(error).toBeInstanceOf(Error);
+ }
+ );
+
+ test('returns the created records when the insert succeeds', async () => {
+ await expect(runCreateRecords()).resolves.toEqual([{ id: 'rec_0', created: '', updated: '', cells: {} }]);
+ expect(recordPosts).toBe(1);
+ });
+});
+
+type LostReply = 'proxy 503' | 'dropped connection';
diff --git a/packages/pieces/core/tables/vitest.config.ts b/packages/pieces/core/tables/vitest.config.ts
new file mode 100644
index 00000000000..ba8ade4a178
--- /dev/null
+++ b/packages/pieces/core/tables/vitest.config.ts
@@ -0,0 +1,17 @@
+import path from 'path'
+import { defineConfig } from 'vitest/config'
+
+const repoRoot = path.resolve(__dirname, '../../../..')
+
+export default defineConfig({
+ test: {
+ globals: true,
+ environment: 'node',
+ },
+ resolve: {
+ alias: {
+ '@activepieces/pieces-framework': path.resolve(repoRoot, 'packages/pieces/framework/src/index.ts'),
+ '@activepieces/pieces-common': path.resolve(repoRoot, 'packages/pieces/common/src/index.ts'),
+ },
+ },
+})