Files
supabase/apps/studio/data/database-queues/database-queues-expose-postgrest-status-query.ts
Joshen Lim d8a57c1c7e Add settings for queues: toggle expose through postgrest + permissions via table privileges (#30564)
* Add settings for queues: toggle expose through postgrest + permissions via table privileges

* Ensure appropriate grants are granted when toggling, and revoked when disabling

* Update to use queues_public schema

* Update queue schema to pgmq_public and add/remove from data api when enabling/disabling

* Fix query for retrieving toggle state

* Add schema invalidation

* Remove hard code

* Use QueuesSettings from Queues folder, remove from NewQueues

* Update SQL for toggling exposure + support RLS enabling

* Support toggling RLS for a queue

* Update admonition copy in queues for enabling/disable postgrest exposure

* Add custom RLS policy for queue

* Minor style fixes

* Fix

* Remove hard code

* Update RLS to add message regarding relevancy only if exposure to PostgREST is enabled

* Update message in exposing queues to postgREST

* Address feedback

* Address feedback

* Don't revoke postgres role stuff

* Remove hard code

* Update copy

* Update

* Address Oli's feedback, ensure that queues ALL have RLS enabled prior to allowing exposure to PostgREST

* Address remaining feedback

* Remove hardcode

* Update

* Address feedback
2024-11-27 12:10:33 +08:00

48 lines
1.6 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import minify from 'pg-minify'
import { executeSql } from 'data/sql/execute-sql-query'
import { ResponseError } from 'types'
import { QUEUES_SCHEMA } from './database-queues-toggle-postgrest-mutation'
import { databaseQueuesKeys } from './keys'
export type DatabaseQueuesVariables = {
projectRef?: string
connectionString?: string
}
// [Joshen] Check if all the relevant functions exist to indicate whether PGMQ has been exposed through PostgREST
const queueSqlQuery = minify(/**SQL */ `
SELECT exists (select schema_name FROM information_schema.schemata WHERE schema_name = '${QUEUES_SCHEMA}');
`)
export async function getDatabaseQueuesExposePostgrestStatus({
projectRef,
connectionString,
}: DatabaseQueuesVariables) {
if (!projectRef) throw new Error('Project ref is required')
const { result } = await executeSql({
projectRef,
connectionString,
sql: queueSqlQuery,
})
return result[0].exists as boolean
}
export type DatabaseQueueData = boolean
export type DatabaseQueueError = ResponseError
export const useQueuesExposePostgrestStatusQuery = <TData = DatabaseQueueData>(
{ projectRef, connectionString }: DatabaseQueuesVariables,
{ enabled = true, ...options }: UseQueryOptions<DatabaseQueueData, DatabaseQueueError, TData> = {}
) =>
useQuery<DatabaseQueueData, DatabaseQueueError, TData>(
databaseQueuesKeys.exposePostgrestStatus(projectRef),
() => getDatabaseQueuesExposePostgrestStatus({ projectRef, connectionString }),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)