Files
supabase/e2e/studio/utils/db/client.ts
Charis 76b767c5e1 test(studio): add db utils for checking database state (#42533)
Test utilities

## What is the current behavior?

E2E tests that need to verify database state go through the SQL Editor,
which is unnecessarily slow and prone to future breakage.

## What is the new behavior?

Adds shared db utils for checking database state in Studio E2E tests by
querying the database directly. Refactors API access tests to use the
new utilities.

## Summary by CodeRabbit

* **Tests**
* Refactored API access privilege verification to use direct database
queries instead of SQL editor interaction.
* Added database utility functions for e2e tests to query table
existence and access privileges.
  * Updated test signatures to simplify privilege verification calls.
2026-02-05 17:46:33 -05:00

34 lines
1.2 KiB
TypeScript

// Default Supabase CLI constants (hardcoded for local development)
const SERVICE_ROLE_KEY =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU'
const API_URL = 'http://127.0.0.1:54321'
/**
* Execute a SQL query against the local Supabase database via pg-meta.
*
* Uses the local Supabase API gateway to route to pg-meta, which executes
* the query against PostgreSQL using the default local connection.
*
* @param sql - The SQL query to execute
* @param params - Optional array of parameters for parameterized queries
* @returns Array of result rows
* @throws Error if the query fails
*/
export async function query<T>(sql: string, params?: Array<unknown>): Promise<Array<T>> {
const response = await fetch(`${API_URL}/pg/query`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
apikey: SERVICE_ROLE_KEY,
},
body: JSON.stringify({ query: sql, parameters: params }),
})
if (!response.ok) {
const error = await response.json()
throw new Error(`Database query failed: ${error.message || JSON.stringify(error)}`)
}
return response.json()
}