Files
supabase/apps/studio/data/pg-graphql/schema-comment-query.ts
Joshen Lim 1baaded0bb Consolidate execute-sql-query into execute-sql-mutation (#46944)
## Context

Just some clean up as I was going through stuff
- `useExecuteSqlQuery` is deprecated and not used at all
- As such `execute-sql-query` is technically irrelevant, the more
relevant file is `execute-sql-mutation`
- Hence opting to consolidate `execute-sql-query` into
`execute-sql-mutation`
- Also removing `ExecuteSqlError` since its just re-exporting the
`ResponseError` type

There's a lot of file changes but its essentially just updating the
importing statements across the files
2026-06-16 00:07:16 +08:00

58 lines
1.9 KiB
TypeScript

import { literal, safeSql } from '@supabase/pg-meta'
import { useQuery } from '@tanstack/react-query'
import { pgGraphqlKeys } from './keys'
import { executeSql } from '@/data/sql/execute-sql-mutation'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { PROJECT_STATUS } from '@/lib/constants'
import type { UseCustomQueryOptions } from '@/types'
export type SchemaCommentVariables = {
projectRef?: string
connectionString?: string | null
schema: string
}
export type SchemaCommentData = string | null
export type SchemaCommentError = Error
const getSchemaCommentSql = (schema: string) =>
safeSql`select obj_description(${literal(schema)}::regnamespace, 'pg_namespace') as comment;`
export async function getSchemaComment(
{ projectRef, connectionString, schema }: SchemaCommentVariables,
signal?: AbortSignal
): Promise<SchemaCommentData> {
const sql = getSchemaCommentSql(schema)
const { result } = await executeSql(
{
projectRef,
connectionString,
sql,
queryKey: ['pg-graphql', 'schema-comment', schema],
},
signal
)
const row = Array.isArray(result) ? result[0] : null
const comment = row?.comment
return typeof comment === 'string' ? comment : null
}
export const useSchemaCommentQuery = <TData = SchemaCommentData>(
{ projectRef, connectionString, schema }: SchemaCommentVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<SchemaCommentData, SchemaCommentError, TData> = {}
) => {
const { data: project } = useSelectedProjectQuery()
const isActive = project?.status === PROJECT_STATUS.ACTIVE_HEALTHY
return useQuery<SchemaCommentData, SchemaCommentError, TData>({
queryKey: pgGraphqlKeys.schemaComment(projectRef, schema),
queryFn: ({ signal }) => getSchemaComment({ projectRef, connectionString, schema }, signal),
enabled: enabled && projectRef !== undefined && isActive,
...options,
})
}