Files
supabase/e2e/studio/utils/db/queries.ts
T
Ali Waseem 4acc0c599c chore: updated queries to use SQL API rather than table clicks (#42561)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Use the API to create tables rather than the SQL editor in the dashboard

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Tests**
* Refactored end-to-end test database utilities to improve
maintainability and reduce code duplication.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-06 15:04:16 +00:00

55 lines
1.6 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,
${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`)
}