mirror of
https://github.com/supabase/supabase.git
synced 2026-06-11 15:10:18 +08:00
## Summary Part 4 of the SafeSql migration stack ([#45897](https://github.com/supabase/supabase/pull/45897), [#45903](https://github.com/supabase/supabase/pull/45903), [#45990](https://github.com/supabase/supabase/pull/45990), this PR, …). Converts the remaining reports, query performance, observability, index advisor, and privileges call sites of `executeSql` to produce `SafeSqlFragment` values. The `ReportQuery.sql` field flips from `string` to `SafeSqlFragment`, which cascades into every consumer — landed here atomically so each branch typechecks cleanly. Touched areas: - `interfaces/Reports/*` — `ReportQuery.sql: SafeSqlFragment`, plus all report definitions/utilities updated - `interfaces/QueryPerformance/useQueryPerformanceQuery.ts` - `interfaces/Database/IndexAdvisor/*` and `data/database/{table-index-advisor,retrieve-index-advisor-result}-query.ts` - `data/privileges/{table-api-access,update-exposed-entities}-mutation.ts` - `interfaces/Storage/StoragePolicies/StoragePolicies.tsx` - `hooks/analytics/useDbQuery.tsx` - `Observability/useSlowQueriesCount.ts` + `useQueryInsightsIssues.utils.test.ts` ## Test plan - [x] `pnpm typecheck` passes - [x] `useQueryInsightsIssues.utils.test.ts` passes - [x] Dev-server smoke test: reports pages, query performance, index advisor, storage policies <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Reworked SQL construction and typings across reporting, query performance, index advisor, and privilege features to use safer SQL fragments, improving reliability and preventing query composition issues. * **Types** * Reporting query types were split to distinguish database vs. logs queries, enabling correct handling and validation. * **Docs/Utils** * Added a helper to consistently generate logs SQL for report hooks. * **Tests** * Updated tests to exercise the new SQL-building API. <!-- review_stack_entry_start --> [](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/45998) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
import { safeSql, type SafeSqlFragment } from '@supabase/pg-meta'
|
|
import { useMemo } from 'react'
|
|
|
|
import useDbQuery from '@/hooks/analytics/useDbQuery'
|
|
|
|
export function buildSlowQueriesCountSql(): SafeSqlFragment {
|
|
return safeSql`
|
|
-- observability-slow-queries-count
|
|
set search_path to public, extensions;
|
|
|
|
SELECT
|
|
CASE
|
|
WHEN EXISTS (
|
|
SELECT 1 FROM pg_extension WHERE extname = 'pg_stat_statements'
|
|
)
|
|
THEN (
|
|
SELECT count(*)::int
|
|
FROM pg_stat_statements
|
|
WHERE total_exec_time + total_plan_time > 1000
|
|
)
|
|
ELSE 0
|
|
END AS slow_queries_count;
|
|
`
|
|
}
|
|
|
|
export const useSlowQueriesCount = (_projectRef?: string, refreshKey: number = 0) => {
|
|
// refreshKey is used in useMemo to force recomputation when refresh is triggered
|
|
const sql = useMemo(
|
|
() => buildSlowQueriesCountSql(),
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
[refreshKey]
|
|
)
|
|
|
|
const { data, isLoading, error } = useDbQuery({
|
|
sql,
|
|
})
|
|
|
|
const slowQueriesCount = useMemo(() => {
|
|
if (!data || !Array.isArray(data) || data.length === 0) {
|
|
return 0
|
|
}
|
|
return data[0]?.slow_queries_count ?? 0
|
|
}, [data])
|
|
|
|
return {
|
|
slowQueriesCount,
|
|
isLoading,
|
|
error,
|
|
}
|
|
}
|