Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Joshenlim/fe 4590 studio sql export can silently swap values for numeric (#51382)

Joshen Lim26c838a43320
diff --git a/apps/studio/components/grid/components/header/Header.tsx b/apps/studio/components/grid/components/header/Header.tsx
index d927ec09ce7..ea341a6c4fa 100644
--- a/apps/studio/components/grid/components/header/Header.tsx
+++ b/apps/studio/components/grid/components/header/Header.tsx
@@ -24,7 +24,10 @@ import { useTableSort } from '@/components/grid/hooks/useTableSort'
import type { SupaRow } from '@/components/grid/types'
import { GridHeaderActions } from '@/components/interfaces/TableGridEditor/GridHeaderActions'
import { isValueTruncated } from '@/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils'
-import { formatTableRowsToSQL } from '@/components/interfaces/TableGridEditor/TableEntity.utils'
+import {
+ formatTableRowsToJSON,
+ formatTableRowsToSQL,
+} from '@/components/interfaces/TableGridEditor/TableEntity.utils'
import {
useExportAllRowsAsCsv,
useExportAllRowsAsJson,
@@ -257,7 +260,7 @@ const RowHeader = ({ rows: visibleRows, tableQueriesEnabled = true }: RowHeaderP
} else if (type === 'sql') {
return formatTableRowsToSQL(snap.table, rows)
} else {
- return JSON.stringify(rows)
+ return formatTableRowsToJSON(snap.table, rows)
}
})()
diff --git a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.ts b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.ts
index 7e9777caf5d..be8d2ec3376 100644
--- a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.ts
+++ b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.ts
@@ -1,9 +1,52 @@
import { describe, expect, it } from 'vitest'
-import { formatTableRowsToSQL, getTablePoliciesUrl } from './TableEntity.utils'
+import {
+ formatTableRowsToJSON,
+ formatTableRowsToSQL,
+ getTablePoliciesUrl,
+} from './TableEntity.utils'
import type { SupaTable } from '@/components/grid/types'
import { ENTITY_TYPE } from '@/data/entity-types/entity-type-constants'
+describe('TableEntity.utils: formatTableRowsToJSON', () => {
+ const table: SupaTable = {
+ id: 1,
+ type: ENTITY_TYPE.TABLE,
+ columns: [
+ { name: 'id', dataType: 'bigint', format: 'int8', position: 0 },
+ { name: '2024', dataType: 'bigint', format: 'int8', position: 1 },
+ { name: '2023', dataType: 'bigint', format: 'int8', position: 2 },
+ { name: 'meta', dataType: 'jsonb', format: 'jsonb', position: 3 },
+ ],
+ name: 'yearly_totals',
+ schema: 'public',
+ comment: undefined,
+ estimateRowCount: 1,
+ }
+
+ it('should follow column order, including integer-like column names', () => {
+ const rows = [{ idx: 0, '2023': 42, '2024': 99, id: 7, meta: { a: 1 } }]
+ expect(formatTableRowsToJSON(table, rows)).toBe(`[{"id":7,"2024":99,"2023":42,"meta":{"a":1}}]`)
+ })
+
+ it('should omit the grid idx key unless the table has an idx column', () => {
+ expect(formatTableRowsToJSON(table, [{ idx: 3, id: 1 }])).not.toContain('idx')
+
+ const withIdx: SupaTable = {
+ ...table,
+ columns: [{ name: 'idx', dataType: 'bigint', format: 'int8', position: 0 }],
+ }
+ expect(formatTableRowsToJSON(withIdx, [{ idx: 3 }])).toBe(`[{"idx":3}]`)
+ })
+
+ it('should emit null for missing columns and [] for no rows', () => {
+ expect(formatTableRowsToJSON(table, [{ id: 1 }])).toBe(
+ `[{"id":1,"2024":null,"2023":null,"meta":null}]`
+ )
+ expect(formatTableRowsToJSON(table, [])).toBe('[]')
+ })
+})
+
describe('TableEntity.utils: formatTableRowsToSQL', () => {
it('should format rows into a single SQL INSERT statement', () => {
const table: SupaTable = {
@@ -137,6 +180,46 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {
expect(result).toBe(expected)
})
+ it('should keep values aligned with columns when column names are integer-like', () => {
+ const table: SupaTable = {
+ id: 1,
+ type: ENTITY_TYPE.TABLE,
+ columns: [
+ { name: 'id', dataType: 'bigint', format: 'int8', position: 0 },
+ { name: '2024', dataType: 'bigint', format: 'int8', position: 1 },
+ { name: '2023', dataType: 'bigint', format: 'int8', position: 2 },
+ ],
+ name: 'yearly_totals',
+ schema: 'public',
+ comment: undefined,
+ estimateRowCount: 1,
+ }
+ // JS hoists integer-like keys to the front of Object.entries, regardless of insertion order
+ const rows = [{ idx: 0, id: 7, '2024': 99, '2023': 42 }]
+
+ const result = formatTableRowsToSQL(table, rows)
+ const expected = `INSERT INTO public.yearly_totals (id, "2024", "2023") VALUES (7, 99, 42);`
+ expect(result).toBe(expected)
+ })
+
+ it('should emit null for columns missing from a row', () => {
+ const table: SupaTable = {
+ id: 1,
+ type: ENTITY_TYPE.TABLE,
+ columns: [
+ { name: 'id', dataType: 'bigint', format: 'int8', position: 0 },
+ { name: 'name', dataType: 'text', format: 'text', position: 1 },
+ ],
+ name: 'people',
+ schema: 'public',
+ comment: undefined,
+ estimateRowCount: 1,
+ }
+
+ const result = formatTableRowsToSQL(table, [{ id: 1 }])
+ expect(result).toBe(`INSERT INTO public.people (id, name) VALUES (1, null);`)
+ })
+
it('should return an empty string for empty rows', () => {
const table: SupaTable = {
id: 1,
diff --git a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.ts b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.ts
index 1b9bbb32e8e..2695199e507 100644
--- a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.ts
+++ b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.ts
@@ -36,6 +36,17 @@ export const getTablePoliciesUrl = (
)}&schema=${encodeURIComponent(schema ?? '')}`
}
+export const formatTableRowsToJSON = (table: SupaTable, rows: Record<string, unknown>[]) => {
+ const objects = rows.map((row) => {
+ const entries = table.columns.map(
+ ({ name }) =>
+ `${JSON.stringify(name)}:${JSON.stringify((Object.hasOwn(row, name) ? row[name] : null) ?? null)}`
+ )
+ return `{${entries.join(',')}}`
+ })
+ return `[${objects.join(',')}]`
+}
+
export const formatTableRowsToSQL = (table: SupaTable, rows: any[]) => {
if (rows.length === 0) return ''
@@ -43,18 +54,15 @@ export const formatTableRowsToSQL = (table: SupaTable, rows: any[]) => {
const valuesSets = rows
.map((row) => {
- const filteredRow = { ...row }
- if ('idx' in filteredRow) delete filteredRow.idx
-
- const values = Object.entries(filteredRow).map(([key, val]) => {
- const { dataType, format } = table.columns.find((col) => col.name === key) ?? {}
+ const values = table.columns.map(({ name, dataType, format }) => {
+ const val = Object.hasOwn(row, name) ? row[name] : undefined
// We only check for NULL, array and JSON types, everything else we stringify
// given that Postgres can implicitly cast the right type based on the column type
// For string types, we need to deal with escaping single quotes
const stringFormats = ['text', 'varchar']
- if (val === null) {
+ if (val === null || val === undefined) {
return 'null'
} else if (dataType === 'ARRAY') {
const array = Array.isArray(val) ? val : JSON.parse(val as string)