Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(audit): harden no-PK unique-index fallback, guard tiptap text extraction

naveenkash53757cd4a584
diff --git a/packages/database/supabase/migrations/20260717143448_fix-event-update-row-pairing.sql b/packages/database/supabase/migrations/20260717143448_fix-event-update-row-pairing.sql
index 89c78db9a4..bf4847ef9c 100644
--- a/packages/database/supabase/migrations/20260717143448_fix-event-update-row-pairing.sql
+++ b/packages/database/supabase/migrations/20260717143448_fix-event-update-row-pairing.sql
@@ -29,16 +29,23 @@ BEGIN
WHERE i.indrelid = ('"' || p_table_name || '"')::regclass
AND i.indisprimary;
- -- No PK: all columns of the smallest unique index
+ -- No PK: key columns of the smallest valid, non-partial, non-expression
+ -- unique index. Partial/expression indexes don't guarantee row identity for
+ -- every row, and INCLUDE columns (positions past indnkeyatts) aren't part of
+ -- the uniqueness constraint — the slice keeps key columns only.
IF pk_columns IS NULL THEN
SELECT array_agg(a.attname ORDER BY a.attnum) INTO pk_columns
FROM pg_index i
- JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)
+ JOIN pg_attribute a ON a.attrelid = i.indrelid
+ AND a.attnum = ANY ((i.indkey::int2[])[1:i.indnkeyatts])
WHERE i.indexrelid = (
SELECT i2.indexrelid
FROM pg_index i2
WHERE i2.indrelid = ('"' || p_table_name || '"')::regclass
AND i2.indisunique
+ AND i2.indisvalid
+ AND i2.indpred IS NULL
+ AND i2.indexprs IS NULL
ORDER BY i2.indnkeyatts ASC
LIMIT 1
);
diff --git a/packages/jobs/src/inngest/functions/events/audit.ts b/packages/jobs/src/inngest/functions/events/audit.ts
index d3658c645c..e637f99bdd 100644
--- a/packages/jobs/src/inngest/functions/events/audit.ts
+++ b/packages/jobs/src/inngest/functions/events/audit.ts
@@ -79,7 +79,13 @@ function isTiptapDoc(value: unknown): boolean {
*/
function richTextToText(value: unknown): string {
if (isTiptapDoc(value)) {
- return tiptapToText(value as Parameters<typeof tiptapToText>[0]);
+ // Malformed doc JSON must degrade to "", not throw — an unhandled throw
+ // here poisons the event batch and Inngest retries it forever.
+ try {
+ return tiptapToText(value as Parameters<typeof tiptapToText>[0]);
+ } catch {
+ return "";
+ }
}
if (typeof value === "string") return value;
return "";