Files
supabase/apps/studio/data/database-integrations/stripe/sync-state-query.ts
Charis 116faefcda studio: convert more executeSql callers to SafeSqlFragment (#45645)
## 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 -->
2026-05-06 12:21:48 -04:00

66 lines
2.0 KiB
TypeScript

import { safeSql } from '@supabase/pg-meta/src/pg-format'
import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query'
import { z } from 'zod'
import { stripeSyncKeys } from './keys'
import { executeSql, ExecuteSqlError } from '@/data/sql/execute-sql-query'
export type DbConnection = {
projectRef: string
connectionString?: string | null
}
const StripeSyncStateSchema = z
.object({
started_at: z.string().nullable(),
closed_at: z.string().nullable(),
status: z.enum(['running', 'pending', 'complete', 'error']).nullable(),
})
.nullable()
export type StripeSyncState = z.infer<typeof StripeSyncStateSchema>
export type StripeSyncStateData = z.infer<typeof StripeSyncStateSchema>
export type StripeSyncStateError = ExecuteSqlError
export async function getStripeSyncState(
{ projectRef, connectionString }: DbConnection,
signal?: AbortSignal
) {
const { result } = await executeSql(
{
projectRef,
connectionString,
sql: safeSql`
SELECT started_at, closed_at, status FROM stripe.sync_runs WHERE status != 'pending' ORDER BY started_at DESC LIMIT 1;
`,
queryKey: stripeSyncKeys.syncState(projectRef),
},
signal
)
return result.length > 0 ? StripeSyncStateSchema.parse(result[0]) : null
}
export const useStripeSyncingState = <TData = StripeSyncStateData>(
{ projectRef, connectionString }: DbConnection,
{
enabled = true,
...options
}: Omit<
UseQueryOptions<StripeSyncStateData, StripeSyncStateError, TData>,
'queryKey' | 'queryFn'
> = {}
) => {
return useQuery<StripeSyncStateData, StripeSyncStateError, TData>({
queryKey: stripeSyncKeys.syncState(projectRef),
queryFn: ({ signal }) => getStripeSyncState({ projectRef, connectionString }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})
}
export function invalidateStripeSyncStateQuery(client: QueryClient, projectRef: string) {
return client.invalidateQueries({ queryKey: stripeSyncKeys.syncState(projectRef) })
}