mirror of
https://github.com/supabase/supabase.git
synced 2026-10-10 11:55:05 +03:00
## Summary - Converts ~27 `executeSql` call sites in `apps/studio/data/**` to build SQL through `safeSql` / `ident` / `literal` / `keyword` / `joinSqlFragments` instead of raw template-string interpolation. - Tightens the `useDatabaseCronJobCreateMutation` and `useDatabaseEventTriggerCreateMutation` `sql`/`query` parameter types from `string` to `SafeSqlFragment` (callers already produce one). - Updates `getDeleteEnumeratedTypeSQL` in `packages/pg-meta` to return `SafeSqlFragment`. - Fixes a bug noticed while testing where Queues integration does not correctly handle queues with uppercase names. ## Pages to manually test - Integrations > Cron Jobs - Integrations > Queues - Database > Triggers > Event Triggers - Database > Indexes - Reports > Query Performance - Storage <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Bug Fixes** * Queue lookups now correctly handle case-insensitive queue names. * Queue table references are now properly managed and consistently applied throughout the queue management interface. * Improved queue name display normalization in the user interface. * **Chores** * Enhanced SQL query safety across the database layer through parameterized query construction and safer templating approaches. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
101 lines
2.9 KiB
TypeScript
101 lines
2.9 KiB
TypeScript
import { ident, literal, safeSql } from '@supabase/pg-meta/src/pg-format'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { databaseQueuesKeys } from './keys'
|
|
import {
|
|
isQueueNameValid,
|
|
pgmqQueueTable,
|
|
} from '@/components/interfaces/Integrations/Queues/Queues.utils'
|
|
import { executeSql } from '@/data/sql/execute-sql-query'
|
|
import type { ResponseError, UseCustomQueryOptions } from '@/types'
|
|
|
|
export type DatabaseQueuesMetricsVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
queueName: string
|
|
}
|
|
|
|
export type PostgresQueueMetric = {
|
|
queue_name: string
|
|
queue_length: number
|
|
method: 'estimated' | 'precise'
|
|
}
|
|
|
|
const preciseMetricsSqlQuery = (queueName: string) =>
|
|
safeSql`
|
|
set local statement_timeout = '1s';
|
|
SELECT
|
|
COUNT(*) AS row_count
|
|
FROM
|
|
${ident('pgmq')}.${ident(pgmqQueueTable(queueName))};
|
|
`
|
|
|
|
const estimateMetricsSqlQuery = (queueName: string) =>
|
|
safeSql`
|
|
select
|
|
reltuples::bigint as estimated_rows
|
|
from
|
|
pg_class
|
|
where
|
|
relname = ${literal(pgmqQueueTable(queueName))}
|
|
and relnamespace = 'pgmq'::regnamespace;
|
|
`
|
|
|
|
export async function getDatabaseQueuesMetrics({
|
|
projectRef,
|
|
connectionString,
|
|
queueName,
|
|
}: DatabaseQueuesMetricsVariables) {
|
|
if (!projectRef) throw new Error('Project ref is required')
|
|
if (!isQueueNameValid(queueName)) {
|
|
throw new Error(
|
|
'Invalid queue name: must contain only alphanumeric characters, underscores, and hyphens'
|
|
)
|
|
}
|
|
|
|
try {
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql: preciseMetricsSqlQuery(queueName),
|
|
})
|
|
return {
|
|
queue_name: queueName,
|
|
queue_length: result[0].row_count,
|
|
method: 'precise',
|
|
} as PostgresQueueMetric
|
|
} catch (error: any) {
|
|
// if the error is caused because the count timeouted, try to fetch an approximate count
|
|
if (error?.message === 'canceling statement due to statement timeout') {
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql: estimateMetricsSqlQuery(queueName),
|
|
})
|
|
return {
|
|
queue_name: queueName,
|
|
queue_length: result[0].estimated_rows,
|
|
method: 'estimated',
|
|
} as PostgresQueueMetric
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export type DatabaseQueuesMetricsData = PostgresQueueMetric
|
|
export type DatabaseQueuesMetricsError = ResponseError
|
|
|
|
export const useQueuesMetricsQuery = <TData = DatabaseQueuesMetricsData>(
|
|
{ projectRef, connectionString, queueName }: DatabaseQueuesMetricsVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<DatabaseQueuesMetricsData, DatabaseQueuesMetricsError, TData> = {}
|
|
) =>
|
|
useQuery<DatabaseQueuesMetricsData, DatabaseQueuesMetricsError, TData>({
|
|
queryKey: databaseQueuesKeys.metrics(projectRef, queueName),
|
|
queryFn: () => getDatabaseQueuesMetrics({ projectRef, connectionString, queueName }),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|