Files
supabase/e2e/studio/utils/db/queries.ts
T
Gildas Garcia 13bfc4502a chore: e2e general improvements (#43547)
## Problem

e2e tests are still flaky and not as fast as they could be

## Solution

- [x] Reset supabase instance: this makes the database visualiser tests
faster as there are less tables to screenshot
- [x] Improve the single file setup utilities so that they never block
local tests by cleaning them up before starting
- [x] Disable animations while running the tests (less time waiting for
animations to complete
- [x] Add utility functions that help reproducing flaky tests locally
2026-03-09 15:38:06 +01:00

72 lines
2.2 KiB
TypeScript

import { query } from './client.js'
/**
* Check if a table exists in the specified schema.
*
* @param schema - The schema name (e.g., 'public')
* @param tableName - The table name to check
* @returns true if the table exists, false otherwise
*/
export async function tableExists(schema: string, tableName: string): Promise<boolean> {
const result = await query<{ exists: boolean }>(
`SELECT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = $1 AND table_name = $2
) as exists`,
[schema, tableName]
)
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,
created_at timestamp with time zone null default now(),
${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)
}
}
/**
* Create a table with a single text column with RLS enabled (as if created through UI) 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 createTableWithRLS(
tableName: string,
columnName: string,
initialData?: Array<Record<string, string>>
) {
await createTable(tableName, columnName, initialData)
await query(`alter table public.${tableName} enable row level security;`)
}
/**
* 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`)
}