Skip to content

fix: truncate tracking data to prevent Prisma length mismatch errors …#4269

Open
KAUSHALCODER123 wants to merge 1 commit into
umami-software:masterfrom
KAUSHALCODER123:fix/prisma-p2000-truncation
Open

fix: truncate tracking data to prevent Prisma length mismatch errors …#4269
KAUSHALCODER123 wants to merge 1 commit into
umami-software:masterfrom
KAUSHALCODER123:fix/prisma-p2000-truncation

Conversation

@KAUSHALCODER123
Copy link
Copy Markdown

…(P2000)

@vercel
Copy link
Copy Markdown

vercel Bot commented May 10, 2026

@KAUSHALCODER123 is attempting to deploy a commit to the Umami Software Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 10, 2026

Greptile Summary

This PR adds truncation guards to prevent Prisma P2000 (value-too-long) errors by introducing 12 new length constants in constants.ts and applying them consistently across event and session write paths. The constants are correctly derived from the Prisma schema's @db.VarChar(n) annotations.

  • saveEvent.ts: UTM params, click IDs, tag, hostname, and currency are now truncated to their schema limits in both Prisma and ClickHouse paths.
  • saveEventData.ts: dataKey and stringValue are truncated to DATA_LENGTH (500) matching the VarChar(500) columns.
  • saveSessionData.ts: truncates dataKey and stringValue but omits distinctId, leaving P2000 errors possible for that VarChar(50) column.

Confidence Score: 3/5

Safe to merge for the fields it fixes, but one field in saveSessionData.ts is still unguarded and will continue to throw the same error the PR aims to eliminate.

The distinctId field in saveSessionData.ts is written directly to a @db.VarChar(50) column without truncation — the DISTINCT_ID_LENGTH constant introduced in this very PR is never applied there. Any caller supplying a distinctId over 50 characters will still hit a P2000 error on sessionData.create or sessionData.updateMany.

src/queries/sql/sessions/saveSessionData.ts — distinctId needs the same treatment as dataKey and stringValue in the same file.

Important Files Changed

Filename Overview
src/lib/constants.ts Adds 12 new length constants that all match the corresponding Prisma schema VarChar lengths exactly
src/queries/sql/events/saveEvent.ts Applies new length constants to truncate UTM, click-ID, tag, hostname, and currency fields in both Prisma and ClickHouse query paths
src/queries/sql/events/saveEventData.ts Truncates dataKey and stringValue to DATA_LENGTH (500) in both Prisma and ClickHouse paths, matching the VarChar(500) schema columns
src/queries/sql/sessions/createSession.ts Correctly truncates all session fields before the raw SQL insert using the new constants
src/queries/sql/sessions/saveSessionData.ts Truncates dataKey and stringValue but omits truncation of distinctId, which is also a VarChar(50) column in the SessionData Prisma model

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Incoming tracking data] --> B{Query type}
    B -->|PRISMA| C[saveEvent - relationalQuery]
    B -->|CLICKHOUSE| D[saveEvent - clickhouseQuery]
    C --> C1[Truncate: urlPath, urlQuery, UTMs, click IDs, tag, hostname, currency]
    C1 --> C2[prisma.websiteEvent.create]
    D --> D1[Truncate: same fields]
    D1 --> D2[clickhouse.insert / kafka]
    E[Session ingestion] --> F[createSession - rawQuery]
    F --> F1[Truncate: browser, os, device, screen, language, country, region, city, distinctId]
    F1 --> F2[Raw SQL INSERT ON CONFLICT DO NOTHING]
    G[Session data] --> H{Query type}
    H -->|PRISMA| I[saveSessionData - relationalQuery]
    H -->|CLICKHOUSE| J[saveSessionData - clickhouseQuery]
    I --> I1[Truncate: dataKey stringValue / distinctId NOT truncated]
    I1 --> I2[prisma.sessionData.updateMany / create]
Loading

Comments Outside Diff (1)

  1. src/queries/sql/sessions/saveSessionData.ts, line 45-47 (link)

    P1 Truncate distinctId to DISTINCT_ID_LENGTH (50) to stay within the @db.VarChar(50) constraint; without this, a long distinctId will still trigger a Prisma P2000 error on both create and updateMany.

Reviews (1): Last reviewed commit: "fix: truncate tracking data to prevent P..." | Re-trigger Greptile

@@ -1,5 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { DATA_TYPE } from '@/lib/constants';
import { DATA_LENGTH, DATA_TYPE } from '@/lib/constants';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Import DISTINCT_ID_LENGTH and apply it to distinctId so the Prisma write doesn't exceed the @db.VarChar(50) limit on SessionData.distinctId.

Suggested change
import { DATA_LENGTH, DATA_TYPE } from '@/lib/constants';
import { DATA_LENGTH, DISTINCT_ID_LENGTH, DATA_TYPE } from '@/lib/constants';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant