The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Studio SQL export emits valid PostgreSQL literals (#44025)
Matejb93733c811c5
diff --git a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.ts b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.tsindex 24b0133b77b..756eea86af4 100644--- a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.ts+++ b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.test.ts@@ -24,7 +24,7 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {]const result = formatTableRowsToSQL(table, rows)- const expected = `INSERT INTO "public"."people" ("id", "name") VALUES ('1', 'Person 1'), ('2', 'Person 2'), ('3', 'Person 3');`+ const expected = `INSERT INTO "public"."people" ("id", "name") VALUES (1, 'Person 1'), (2, 'Person 2'), (3, 'Person 3');`expect(result).toBe(expected)})@@ -48,7 +48,7 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {]const result = formatTableRowsToSQL(table, rows)- const expected = `INSERT INTO "public"."people" ("id", "name") VALUES ('1', 'Person 1'), ('2', null), ('3', 'Person 3');`+ const expected = `INSERT INTO "public"."people" ("id", "name") VALUES (1, 'Person 1'), (2, null), (3, 'Person 3');`expect(result).toBe(expected)})@@ -84,7 +84,55 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {},]const result = formatTableRowsToSQL(table, rows)- const expected = `INSERT INTO "public"."demo" ("id", "name", "tags", "metadata") VALUES ('2', 'Person 1', ARRAY["tag-a","tag-c"], '{"version": 1}'), ('3', 'ONeil', ARRAY["tag-a"], '{"version": 1, "name": "O''Neil"}');`+ const expected = `INSERT INTO "public"."demo" ("id", "name", "tags", "metadata") VALUES (2, 'Person 1', ARRAY['tag-a','tag-c'], '{"version": 1}'), (3, 'ONeil', ARRAY['tag-a'], '{"version": 1, "name": "O''Neil"}');`+ expect(result).toBe(expected)+ })++ it('should emit valid Postgres literals for booleans, numbers and text arrays', () => {+ const table: SupaTable = {+ id: 1,+ type: ENTITY_TYPE.TABLE,+ columns: [+ { name: 'id', dataType: 'text', format: 'text', position: 0 },+ { name: 'public', dataType: 'bool', format: 'bool', position: 1 },+ { name: 'avif_autodetection', dataType: 'bool', format: 'bool', position: 2 },+ { name: 'file_size_limit', dataType: 'int8', format: 'int8', position: 3 },+ { name: 'allowed_mime_types', dataType: 'ARRAY', format: '_text', position: 4 },+ ],+ name: 'buckets',+ schema: 'storage',+ comment: undefined,+ estimateRowCount: 1,+ }+ const rows = [+ {+ id: 'emails',+ public: true,+ avif_autodetection: false,+ file_size_limit: 10485760,+ allowed_mime_types: ['image/*', "image/o'neil"],+ },+ ]++ const result = formatTableRowsToSQL(table, rows)+ const expected = `INSERT INTO "storage"."buckets" ("id", "public", "avif_autodetection", "file_size_limit", "allowed_mime_types") VALUES ('emails', true, false, 10485760, ARRAY['image/*','image/o''neil']);`+ expect(result).toBe(expected)+ })++ it('should escape fallback string formats outside text and varchar', () => {+ const table: SupaTable = {+ id: 1,+ type: ENTITY_TYPE.TABLE,+ columns: [{ name: 'email', dataType: 'USER-DEFINED', format: 'citext', position: 0 }],+ name: 'users',+ schema: 'public',+ comment: undefined,+ estimateRowCount: 1,+ }+ const rows = [{ email: "o'[email protected]" }]++ const result = formatTableRowsToSQL(table, rows)+ const expected = `INSERT INTO "public"."users" ("email") VALUES ('o''[email protected]');`expect(result).toBe(expected)})@@ -124,7 +172,7 @@ describe('TableEntity.utils: formatTableRowsToSQL', () => {]const result = formatTableRowsToSQL(table, rows)- const expected = `INSERT INTO "public"."people" ("id", "name") VALUES ('1', 'Person 1'), ('2', 'Person 2');`+ const expected = `INSERT INTO "public"."people" ("id", "name") VALUES (1, 'Person 1'), (2, 'Person 2');`expect(result).toBe(expected)})})diff --git a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.ts b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.tsindex c091fb6bbc6..565174ed7cd 100644--- a/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.ts+++ b/apps/studio/components/interfaces/TableGridEditor/TableEntity.utils.ts@@ -56,6 +56,10 @@ export const formatTableRowsToSQL = (table: SupaTable, rows: any[]) => {stringFormats.includes(format)) {return `'${val.replaceAll("'", "''")}'`+ } else if (typeof val === 'number' || typeof val === 'boolean') {+ return `${val}`+ } else if (typeof val === 'string') {+ return `'${val.replaceAll("'", "''")}'`} else {return `'${val}'`}@@ -83,7 +87,7 @@ const generateRandomTag = (): `$${string}$` => {/*** Wrap a string in dollar-quote tags, ensuring the tag does not appear in the string*- * @throws Error if unable to generate a unique tag after multiple attempts+ * @throws Error if unable to generate a unique dollar-quote tag after multiple attempts*/const safeDollarQuote = (str: string): string => {let tag = generateRandomTag()@@ -108,7 +112,7 @@ const formatArrayForSql = (arr: unknown[]): string => {if (Array.isArray(item)) {result += formatArrayForSql(item)} else if (typeof item === 'string') {- result += `"${item.replace(/"/g, '""')}"`+ result += `'${item.replaceAll("'", "''")}'`} else if (!!item && typeof item === 'object') {result += `${safeDollarQuote(JSON.stringify(item))}::json`} else {diff --git a/e2e/studio/features/table-editor.spec.ts b/e2e/studio/features/table-editor.spec.tsindex 652bd6b1e77..fb8aaf26a8d 100644--- a/e2e/studio/features/table-editor.spec.ts+++ b/e2e/studio/features/table-editor.spec.ts@@ -665,7 +665,7 @@ testRunner('table editor', () => {const downloadSqlPath = await downloadSql.path()const sqlContent = fs.readFileSync(downloadSqlPath, 'utf-8')expect(sqlContent).toBe(- `INSERT INTO "public"."${tableNameDataActions}" ("id", "created_at", "pw_column") VALUES ('4', '2025-01-01 12:00:00+00', 'value 4 to export'), ('5', '2025-01-01 12:00:00+00', 'value 5 to export'), ('6', '2025-01-01 12:00:00+00', 'value 6 to export');`+ `INSERT INTO "public"."${tableNameDataActions}" ("id", "created_at", "pw_column") VALUES (4, '2025-01-01 12:00:00+00', 'value 4 to export'), (5, '2025-01-01 12:00:00+00', 'value 5 to export'), (6, '2025-01-01 12:00:00+00', 'value 6 to export');`)await page.waitForTimeout(1000) // wait for event processing to completefs.unlinkSync(downloadSqlPath)