Skip to content

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

chore: updated queries to use SQL API rather than table clicks (#42561)

Ali Waseem4acc0c599c1f
diff --git a/e2e/studio/features/queue-table-operations.spec.ts b/e2e/studio/features/queue-table-operations.spec.ts
index ac1442ec7ed..89d637f4e8a 100644
--- a/e2e/studio/features/queue-table-operations.spec.ts
+++ b/e2e/studio/features/queue-table-operations.spec.ts
@@ -1,4 +1,5 @@
import { expect, Page } from '@playwright/test'
+import { createTable, dropTable } from '../utils/db/index.js'
import { releaseFileOnceCleanup, withFileOnceSetup } from '../utils/once-per-file.js'
import { deleteTable } from '../utils/table-helpers.js'
import { test } from '../utils/test.js'
@@ -14,53 +15,6 @@ const enableQueueOperations = async (page: Page) => {
}, QUEUE_OPERATIONS_KEY)
}
-const runSQL = async (page: Page, ref: string, sql: string) => {
- await page.goto(toUrl(`/project/${ref}/sql/new`))
- await expect(page.getByText('Loading...')).not.toBeVisible({ timeout: 10000 })
-
- await page.locator('.view-lines').click()
- await page.keyboard.press('ControlOrMeta+KeyA')
- await page.evaluate((text) => navigator.clipboard.writeText(text), sql)
- await page.keyboard.press('ControlOrMeta+KeyV')
-
- await page.getByTestId('sql-run-button').click()
-
- const confirmButton = page.getByRole('button', { name: 'Run this query' })
- if (await confirmButton.isVisible({ timeout: 1000 }).catch(() => false)) {
- await confirmButton.click()
- }
-
- await expect(
- page.getByText('Success. No rows returned').or(page.getByText(/^\d+ rows?$/)).first()
- ).toBeVisible({ timeout: 10000 })
-}
-
-const createTableSQL = async (
- page: Page,
- ref: string,
- tableName: string,
- columnName: string,
- initialData?: { [key: string]: string }[]
-) => {
- let sql = `CREATE TABLE IF NOT EXISTS ${tableName} (
- id bigint generated by default as identity primary key,
- ${columnName} text
- );`
-
- if (initialData && initialData.length > 0) {
- const values = initialData
- .map((row) => `('${row[columnName].replace(/'/g, "''")}')`)
- .join(', ')
- sql += `\nINSERT INTO ${tableName} (${columnName}) VALUES ${values};`
- }
-
- await runSQL(page, ref, sql)
-}
-
-const dropTableSQL = async (page: Page, ref: string, tableName: string) => {
- await runSQL(page, ref, `DROP TABLE IF EXISTS ${tableName} CASCADE;`)
-}
-
test.describe('Queue Table Operations', () => {
test.beforeAll(async ({ browser, ref }) => {
await withFileOnceSetup(import.meta.url, async () => {
@@ -110,7 +64,7 @@ test.describe('Queue Table Operations', () => {
const tableName = `${tableNamePrefix}_cell_edit`
const columnName = 'name'
- await createTableSQL(page, ref, tableName, columnName, [{ name: 'original value' }])
+ await createTable(tableName, columnName, [{ name: 'original value' }])
await page.goto(toUrl(`/project/${ref}/editor?schema=public`))
await enableQueueOperations(page)
@@ -146,14 +100,14 @@ test.describe('Queue Table Operations', () => {
await expect(page.getByRole('gridcell', { name: 'edited value' })).toBeVisible()
await expect(page.getByRole('gridcell', { name: 'original value' })).not.toBeVisible()
- await dropTableSQL(page, ref, tableName)
+ await dropTable(tableName)
})
test('cell edits can be cancelled', async ({ page, ref }) => {
const tableName = `${tableNamePrefix}_cell_cancel`
const columnName = 'name'
- await createTableSQL(page, ref, tableName, columnName, [{ name: 'keep this value' }])
+ await createTable(tableName, columnName, [{ name: 'keep this value' }])
await page.goto(toUrl(`/project/${ref}/editor?schema=public`))
await enableQueueOperations(page)
@@ -181,14 +135,14 @@ test.describe('Queue Table Operations', () => {
await expect(page.getByText('pending change')).not.toBeVisible()
- await dropTableSQL(page, ref, tableName)
+ await dropTable(tableName)
})
test('row inserts are queued and can be saved', async ({ page, ref }) => {
const tableName = `${tableNamePrefix}_row_insert`
const columnName = 'name'
- await createTableSQL(page, ref, tableName, columnName)
+ await createTable(tableName, columnName)
await page.goto(toUrl(`/project/${ref}/editor?schema=public`))
await enableQueueOperations(page)
@@ -219,14 +173,14 @@ test.describe('Queue Table Operations', () => {
await expect(page.getByRole('gridcell', { name: 'new row value' })).toBeVisible()
- await dropTableSQL(page, ref, tableName)
+ await dropTable(tableName)
})
test('multiple operations can be batched and saved together', async ({ page, ref }) => {
const tableName = `${tableNamePrefix}_batch_ops`
const columnName = 'name'
- await createTableSQL(page, ref, tableName, columnName)
+ await createTable(tableName, columnName)
await page.goto(toUrl(`/project/${ref}/editor?schema=public`))
await enableQueueOperations(page)
@@ -260,14 +214,14 @@ test.describe('Queue Table Operations', () => {
await expect(page.getByRole('gridcell', { name: 'row one' })).toBeVisible()
await expect(page.getByRole('gridcell', { name: 'row two' })).toBeVisible()
- await dropTableSQL(page, ref, tableName)
+ await dropTable(tableName)
})
test('individual operations can be removed from the queue', async ({ page, ref }) => {
const tableName = `${tableNamePrefix}_remove_op`
const columnName = 'name'
- await createTableSQL(page, ref, tableName, columnName)
+ await createTable(tableName, columnName)
await page.goto(toUrl(`/project/${ref}/editor?schema=public`))
await enableQueueOperations(page)
@@ -303,14 +257,14 @@ test.describe('Queue Table Operations', () => {
await expect(page.getByRole('gridcell', { name: 'keep this row' })).toBeVisible()
await expect(page.getByRole('gridcell', { name: 'remove this row' })).not.toBeVisible()
- await dropTableSQL(page, ref, tableName)
+ await dropTable(tableName)
})
test('keyboard shortcuts work for queue operations', async ({ page, ref }) => {
const tableName = `${tableNamePrefix}_shortcuts`
const columnName = 'name'
- await createTableSQL(page, ref, tableName, columnName)
+ await createTable(tableName, columnName)
await page.goto(toUrl(`/project/${ref}/editor?schema=public`))
await enableQueueOperations(page)
@@ -338,14 +292,14 @@ test.describe('Queue Table Operations', () => {
await expect(page.getByRole('gridcell', { name: 'shortcut test' })).toBeVisible()
- await dropTableSQL(page, ref, tableName)
+ await dropTable(tableName)
})
test('row deletes via context menu are queued', async ({ page, ref }) => {
const tableName = `${tableNamePrefix}_row_delete`
const columnName = 'name'
- await createTableSQL(page, ref, tableName, columnName, [{ name: 'row to delete' }])
+ await createTable(tableName, columnName, [{ name: 'row to delete' }])
await page.goto(toUrl(`/project/${ref}/editor?schema=public`))
await enableQueueOperations(page)
@@ -384,7 +338,7 @@ test.describe('Queue Table Operations', () => {
const tableName = `${tableNamePrefix}_delete_cancel`
const columnName = 'name'
- await createTableSQL(page, ref, tableName, columnName, [{ name: 'should not be deleted' }])
+ await createTable(tableName, columnName, [{ name: 'should not be deleted' }])
await page.goto(toUrl(`/project/${ref}/editor?schema=public`))
await enableQueueOperations(page)
@@ -406,14 +360,14 @@ test.describe('Queue Table Operations', () => {
await expect(page.getByRole('gridcell', { name: 'should not be deleted' })).toBeVisible()
await expect(page.getByText('pending change')).not.toBeVisible()
- await dropTableSQL(page, ref, tableName)
+ await dropTable(tableName)
})
test('mixed operations (add, edit, delete) can be batched', async ({ page, ref }) => {
const tableName = `${tableNamePrefix}_mixed_ops`
const columnName = 'name'
- await createTableSQL(page, ref, tableName, columnName, [
+ await createTable(tableName, columnName, [
{ name: 'row to edit' },
{ name: 'row to delete' },
])
@@ -458,7 +412,7 @@ test.describe('Queue Table Operations', () => {
await expect(page.getByRole('gridcell', { name: 'row to delete' })).not.toBeVisible()
await expect(page.getByRole('gridcell', { name: 'row to edit' })).not.toBeVisible()
- await dropTableSQL(page, ref, tableName)
+ await dropTable(tableName)
})
test('pending changes persist when switching between tables', async ({ page, ref }) => {
@@ -466,8 +420,8 @@ test.describe('Queue Table Operations', () => {
const tableName2 = `${tableNamePrefix}_persist2`
const columnName = 'name'
- await createTableSQL(page, ref, tableName1, columnName)
- await createTableSQL(page, ref, tableName2, columnName)
+ await createTable(tableName1, columnName)
+ await createTable(tableName2, columnName)
await page.goto(toUrl(`/project/${ref}/editor?schema=public`))
await enableQueueOperations(page)
@@ -506,7 +460,7 @@ test.describe('Queue Table Operations', () => {
await page.getByRole('button', { name: `View ${tableName2}`, exact: true }).click()
await expect(page.getByRole('gridcell', { name: 'pending in table 2' })).toBeVisible()
- await dropTableSQL(page, ref, tableName1)
- await dropTableSQL(page, ref, tableName2)
+ await dropTable(tableName1)
+ await dropTable(tableName2)
})
})
diff --git a/e2e/studio/utils/db/index.ts b/e2e/studio/utils/db/index.ts
index 030c48412cc..c5979283622 100644
--- a/e2e/studio/utils/db/index.ts
+++ b/e2e/studio/utils/db/index.ts
@@ -1,2 +1,2 @@
export { query } from './client.js'
-export { tableExists } from './queries.js'
+export { createTable, dropTable, tableExists } from './queries.js'
diff --git a/e2e/studio/utils/db/queries.ts b/e2e/studio/utils/db/queries.ts
index 28b0eef1296..cfebc3a4238 100644
--- a/e2e/studio/utils/db/queries.ts
+++ b/e2e/studio/utils/db/queries.ts
@@ -17,3 +17,38 @@ export async function tableExists(schema: string, tableName: string): Promise<bo
)
return result[0]?.exists ?? false
}
+
+/**
+ * Create a table with a single text column and optionally insert initial rows.
+ *
+ * @param tableName - The table name to create
+ * @param columnName - The name of the text column
+ * @param initialData - Optional array of rows to insert (each row is a key-value map)
+ */
+export async function createTable(
+ tableName: string,
+ columnName: string,
+ initialData?: Array<Record<string, string>>
+) {
+ await query(
+ `CREATE TABLE IF NOT EXISTS ${tableName} (
+ id bigint generated by default as identity primary key,
+ ${columnName} text
+ )`
+ )
+
+ if (initialData && initialData.length > 0) {
+ const placeholders = initialData.map((_, i) => `($${i + 1})`).join(', ')
+ const values = initialData.map((row) => row[columnName])
+ await query(`INSERT INTO ${tableName} (${columnName}) VALUES ${placeholders}`, values)
+ }
+}
+
+/**
+ * Drop a table if it exists.
+ *
+ * @param tableName - The table name to drop
+ */
+export async function dropTable(tableName: string) {
+ await query(`DROP TABLE IF EXISTS ${tableName} CASCADE`)
+}