mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 09:55:06 +03:00
Stacked on #48305. ## What PR 3 of the stacked SQL-editor query-source series (Database vs Logs). Stacked on the PR 2 branch `charislam/log-sql-content-shape`. Turns `SnippetWithContent` into a discriminated union on `type` and makes all snippet writes source-aware: - `data/content/sql-folders-query.ts`: `SnippetWithContent` is now `{ type: 'sql'; content?: SqlSnippets.Content } | { type: 'log_sql'; content?: LogSqlSnippets.Content } | { type: 'report'; content?: never }`. `report` is kept (the content endpoints' wire type carries it) but has no SQL content — its body is `Dashboards.Content`, loaded through the separate `Content` union. - `setSql` brands per type (`untrustedLogSql` vs `untrustedSql`). - `buildUpsertPayload` persists `snippet.type` (no longer hardcoded `'sql'`). - `createSqlSnippetSkeletonV2({ source })` emits the matching type + content shape with the `as any` cast removed. - New `components/interfaces/SQLEditor/querySource.ts`: `SqlSnippetSource` + `getSnippetSource`. - `seedSnippet` test helper gains a `source` arg. - New `remapWireSnippet` boundary helper in `content-remap.ts` concentrates the single wire->domain assertion, so `content-id-query` / `content-upsert-mutation` call sites are cast-free (no `as unknown as`). - Collateral: query result types aligned to the union; `updateSnippet` no longer accepts `type` (source is immutable); db-only editor read paths narrow away `log_sql`. ## Why Impossible-states-impossible typing: a snippet's brand follows its content type, so logs SQL and database SQL can never cross execution paths. No behavior change for existing database snippets. ## Testing - \`pnpm typecheck\` — clean - \`pnpm --filter studio run lint:ratchet\` — no new warnings - \`pnpm test:studio\` (data/content, SQLEditor, state/sql-editor) — passing, including new tests for \`getSnippetSource\`, source-aware \`setSql\`, type-aware \`buildUpsertPayload\`, and both skeleton shapes. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added source-aware creation for SQL editor snippets, including log-based SQL snippets. * Introduced backend source mapping so log snippets are treated as log_sql. * **Bug Fixes** * Improved SQL retrieval/prettification so log snippets no longer use the wrong fallback content. * Ensured log snippets are sanitized and preserve correct type, content, identifiers, and statuses during save/upsert flows. * **Tests** * Expanded unit and integration coverage for log snippet creation, source mapping, editing, prettification, and upsert payloads. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import type { Content } from './content-query'
|
|
import { remapWireSnippet, unmapSqlContentField } from './content-remap'
|
|
import { contentKeys } from './keys'
|
|
import type { SnippetWithContent } from './sql-folders-query'
|
|
import type { components } from '@/data/api'
|
|
import { handleError, put } from '@/data/fetchers'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type UpsertContentPayload = Omit<components['schemas']['UpsertContentBody'], 'content'> & {
|
|
id: string
|
|
content: Partial<Content['content']>
|
|
favorite?: boolean
|
|
}
|
|
|
|
export type UpsertContentVariables = {
|
|
projectRef: string
|
|
payload: UpsertContentPayload
|
|
}
|
|
|
|
export async function upsertContent(
|
|
{ projectRef, payload }: UpsertContentVariables,
|
|
signal?: AbortSignal
|
|
): Promise<SnippetWithContent | null> {
|
|
const { data, error } = await put('/platform/projects/{ref}/content', {
|
|
params: { path: { ref: projectRef } },
|
|
body: unmapSqlContentField(payload),
|
|
headers: { Version: '2' },
|
|
signal,
|
|
})
|
|
if (error) handleError(error)
|
|
if (!data) return null
|
|
|
|
return remapWireSnippet(data, 'saved')
|
|
}
|
|
|
|
export type UpsertContentData = Awaited<ReturnType<typeof upsertContent>>
|
|
|
|
export const useContentUpsertMutation = ({
|
|
onError,
|
|
onSuccess,
|
|
invalidateQueriesOnSuccess = true,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<UpsertContentData, ResponseError, UpsertContentVariables>,
|
|
'mutationFn'
|
|
> & {
|
|
invalidateQueriesOnSuccess?: boolean
|
|
} = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<UpsertContentData, ResponseError, UpsertContentVariables>({
|
|
mutationFn: (args) => upsertContent(args),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
if (invalidateQueriesOnSuccess) {
|
|
await Promise.all([
|
|
queryClient.invalidateQueries({ queryKey: contentKeys.allContentLists(projectRef) }),
|
|
queryClient.invalidateQueries({ queryKey: contentKeys.infiniteList(projectRef) }),
|
|
])
|
|
}
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to insert content: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|