Files
supabase/apps/studio/data/table-rows
Drew McLellan f7f27aac78 fix(studio): escape json and jsonb values in SQL copy and export (#53372)
## What

Fixes invalid or corrupted SQL from "Copy rows as SQL" and "Export table
as SQL" for json/jsonb columns (FE-4860, closes #49196).

## Why

The SQL formatter built json literals by rewriting `JSON.stringify`
output with regexes. That corrupted values containing double quotes or
backslashes, and produced invalid SQL for jsonb columns holding plain
strings or other non-object values.

## How

- `formatTableRowsToSQL` now serializes the parsed value and only
escapes single quotes.
- Grid rows carry json columns as text (the grid query casts them to
`::text`), so a new `parseGridJsonColumns` helper parses untruncated
json cells before formatting. It is used by "Copy rows as SQL" and the
selected-rows export. Truncated cells are left alone so they are still
fetched in full.
- The SQL export keeps json columns unstringified
(`preserveJsonValues`), so a jsonb string like `"hello"` stays
distinguishable from JSON text. CSV and JSON exports are unchanged.

No `::text` cast was added to the export query: the API already returns
json values parsed.

## Testing

- Unit tests for values with `"`, `\`, `'`, plain-string jsonb, arrays,
numbers, booleans and null, plus `parseGridJsonColumns`.
- Ran the formatter output against a local Postgres: both the parsed
(fetch-all) and grid-text (copy / selected rows) shapes loaded and
round-tripped identically for jsonb and json columns. The old
formatter's output failed with `invalid input syntax for type json` on
the same data.
- Not exercised end to end in the Studio UI, and the truncated-cell path
is covered by unit tests only.

## Not in scope

The JSON export still stringifies objects before `JSON.stringify`, so
json columns come out as nested strings. That is separate pre-existing
behavior.
2026-11-16 13:52:05 +00:00
..