Files
supabase/apps/studio/data/table-rows/row-large-values-query.test.ts
Drew McLellan 4c68610167 feat(studio): copy large values when duplicating a row (#53091)
## What

Follow-up to #52952. The grid truncates large columns (over 10 KB) in
the database query, so \"Duplicate row\" used to leave those fields
blank. It now fetches their full values and fills them into the Insert
Row panel.

## Behavior

- Truncated columns are fetched by primary key after the panel opens.
Each one is disabled with \"Loading full value...\" until it arrives,
and Save is disabled until every value has settled. Submitting the form
another way (e.g. pressing Enter) is blocked too.
- Only the truncated fields are filled in, so anything the user has
edited is never overwritten.
- If a value can't be copied, the field stays empty with a note saying
why:
- the value is over 1 MB (it isn't sent, so a single row can't pull an
unbounded amount of data into the browser)
  - the table has no primary key
  - the fetch failed (also shows a toast)
- The fetch honors \"View as role\", like the existing \"Load full\"
buttons.

## How

- New `useRowLargeValuesQuery` in `data/table-rows/` selects the
truncated columns of one row in a single query. Each column is returned
as `NULL` when it exceeds `MAX_ROW_LARGE_VALUE_BYTES`. The SQL is built
with `ident()`, `literal()` and the Query builder.
- A new `useCopiedLargeValues` hook owns the fetch and per-field status,
to keep `RowEditor` from growing further. Pure helpers
(`getTruncatedFieldNames`, `getPrimaryKeyMatch`, `mergeLargeValues`) are
in `RowEditor.utils.ts`.
- `InputField` takes an optional `status` and renders the note.

## Also changes

`isValueTruncated` now compares byte length instead of character length.
The query truncates when a value exceeds the limit in bytes but cuts by
characters, so a value of multibyte characters could be truncated and
still be shorter than the limit in characters, and wasn't detected.
Comparing bytes still never flags a genuine value, because an
untruncated value is at most the limit in bytes. Edit row's \"Load
full\" buttons benefit from the same fix.

## Notes

- A fetched value over 10 KB that genuinely ends in `...` still looks
truncated to the existing check, so the form disables that field, as it
already does when editing such a row.
- A column that changed to `NULL` after the grid loaded also shows the
\"too large\" note.

## Testing

- Unit tests for the SQL builder (including identifier and literal
escaping), the byte-aware detection, and the new helpers.
- Component tests for `RowEditor`: the full value is fetched and
included in the insert; field and Save are disabled while loading
(including form submit); too large, fetch failure, and no primary key
leave the field empty with a note; nothing is fetched when no value was
truncated. I confirmed the loading test fails when the Save guard is
removed.

Resolves FE-4927
2026-11-09 12:53:25 +00:00

64 lines
2.0 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { getRowLargeValuesSql, MAX_ROW_LARGE_VALUE_BYTES } from './row-large-values-query'
describe('getRowLargeValuesSql', () => {
const table = { schema: 'public', name: 'posts' }
it('selects each column as NULL when it exceeds the size limit', () => {
const sql = getRowLargeValuesSql({ table, columns: ['body', 'meta'], pkMatch: { id: 1 } })
expect(sql).toContain(
`case when octet_length(body::text) > ${MAX_ROW_LARGE_VALUE_BYTES} then null else body end as body`
)
expect(sql).toContain(
`case when octet_length(meta::text) > ${MAX_ROW_LARGE_VALUE_BYTES} then null else meta end as meta`
)
})
it('selects from the qualified table and matches on the primary key', () => {
const sql = getRowLargeValuesSql({ table, columns: ['body'], pkMatch: { id: 1 } })
expect(sql).toContain('from public.posts')
expect(sql).toContain('where id = 1')
})
it('matches on every primary key column', () => {
const sql = getRowLargeValuesSql({
table,
columns: ['body'],
pkMatch: { org_id: 7, id: 1 },
})
expect(sql).toContain('where org_id = 7 and id = 1')
})
it('quotes column names that need it', () => {
const sql = getRowLargeValuesSql({ table, columns: ['Body'], pkMatch: { id: 1 } })
expect(sql).toContain(
`case when octet_length("Body"::text) > ${MAX_ROW_LARGE_VALUE_BYTES} then null else "Body" end as "Body"`
)
})
it('quotes identifiers so column names cannot inject SQL', () => {
const sql = getRowLargeValuesSql({
table,
columns: ['body"; drop table posts; --'],
pkMatch: { id: 1 },
})
expect(sql).toContain('"body""; drop table posts; --"')
})
it('escapes primary key values as literals', () => {
const sql = getRowLargeValuesSql({
table,
columns: ['body'],
pkMatch: { slug: "x'; drop table posts; --" },
})
expect(sql).toContain("'x''; drop table posts; --'")
})
})