Files
supabase/apps/studio/data/database-policies/database-policies-query.ts
Joshen Lim 4901f081e5 Migrate remaining requests to pg-meta API to use query endpoint (#47758)
## Context

Migrates the remaining API requests to the pg-meta endpoint to use the
query endpoint directly with the SQL from the pg-meta package. This
touches the following:
- policies
- publications
- triggers
- views
- materialized views
- types

## To test
Just need to verify that we're still fetching the data correctly on
these pages
- Database policies
- Database publications
- Database triggers
- Database tables (views + materialized views)
- Database types

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

* **Bug Fixes**
* Improved and stabilized loading of database metadata (views, triggers,
RLS policies, publications, materialized views, and enum types),
including more reliable schema-scoped filtering.
* Updated policy loading behavior and related UI queries to consistently
use schema arrays, improving cache correctness and consistency.
* **Tests**
* Updated end-to-end test synchronization to wait for the correct
metadata responses using more specific request identifiers.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-09 17:09:03 +08:00

61 lines
2.0 KiB
TypeScript

import pgMeta, { type PGPolicy } from '@supabase/pg-meta'
import { useQuery } from '@tanstack/react-query'
import { executeSql } from '../sql/execute-sql-mutation'
import { databasePoliciesKeys } from './keys'
import type { Policy } from '@/components/interfaces/Database/Policies/PolicyTableRow/PolicyTableRow.utils'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { PROJECT_STATUS } from '@/lib/constants'
import type { ResponseError, UseCustomQueryOptions } from '@/types'
type DatabasePoliciesVariables = {
projectRef?: string
connectionString?: string | null
schemas?: string[]
}
export async function getDatabasePolicies(
{ projectRef, connectionString, schemas }: DatabasePoliciesVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const { sql } = pgMeta.policies.list({ includedSchemas: schemas })
const { result } = await executeSql(
{
projectRef,
connectionString,
sql,
queryKey: ['policies', schemas],
},
signal
)
return result as PGPolicy[]
}
export type DatabasePoliciesData = Awaited<ReturnType<typeof getDatabasePolicies>>
export type DatabasePoliciesError = ResponseError
function markSavedPolicySafe(policy: DatabasePoliciesData[number]): Policy {
return policy as Policy
}
export const useDatabasePoliciesQuery = <TData = Policy[]>(
{ projectRef, connectionString, schemas }: DatabasePoliciesVariables,
{ enabled = true, ...options }: UseCustomQueryOptions<Policy[], DatabasePoliciesError, TData> = {}
) => {
const { data: project } = useSelectedProjectQuery()
const isActive = project?.status === PROJECT_STATUS.ACTIVE_HEALTHY
return useQuery<Policy[], DatabasePoliciesError, TData>({
queryKey: databasePoliciesKeys.list(projectRef, schemas),
queryFn: ({ signal }) =>
getDatabasePolicies({ projectRef, connectionString, schemas }, signal).then((data) =>
data.map(markSavedPolicySafe)
),
enabled: enabled && typeof projectRef !== 'undefined' && isActive,
...options,
})
}