Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: use v2 endpoints for snippets (#32521)

Alaister Younga6776a0fbea1
diff --git a/apps/studio/components/interfaces/SQLEditor/MoveQueryModal.tsx b/apps/studio/components/interfaces/SQLEditor/MoveQueryModal.tsx
index 2e244e0fb95..1841d7d6fdb 100644
--- a/apps/studio/components/interfaces/SQLEditor/MoveQueryModal.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/MoveQueryModal.tsx
@@ -7,7 +7,7 @@ import * as z from 'zod'
import { useParams } from 'common'
import { getContentById } from 'data/content/content-id-query'
-import { useContentUpsertV2Mutation } from 'data/content/content-upsert-v2-mutation'
+import { useContentUpsertMutation } from 'data/content/content-upsert-mutation'
import { useSQLSnippetFolderCreateMutation } from 'data/content/sql-folder-create-mutation'
import { Snippet } from 'data/content/sql-folders-query'
import {
@@ -69,7 +69,7 @@ export const MoveQueryModal = ({ visible, snippets = [], onClose }: MoveQueryMod
const { mutateAsync: createFolder, isLoading: isCreatingFolder } =
useSQLSnippetFolderCreateMutation()
- const { mutateAsync: moveSnippetAsync, isLoading: isMovingSnippet } = useContentUpsertV2Mutation({
+ const { mutateAsync: moveSnippetAsync, isLoading: isMovingSnippet } = useContentUpsertMutation({
onError: (error) => {
toast.error(`Failed to move query: ${error.message}`)
},
diff --git a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx
index 6ee9cc784f6..31e1163ecb4 100644
--- a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx
+++ b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx
@@ -13,6 +13,7 @@ import { createSqlSnippetSkeletonV2 } from 'components/interfaces/SQLEditor/SQLE
import { useContentCountQuery } from 'data/content/content-count-query'
import { useContentDeleteMutation } from 'data/content/content-delete-mutation'
import { getContentById } from 'data/content/content-id-query'
+import { useContentUpsertMutation } from 'data/content/content-upsert-mutation'
import { useSQLSnippetFoldersDeleteMutation } from 'data/content/sql-folders-delete-mutation'
import { Snippet, SnippetFolder, useSQLSnippetFoldersQuery } from 'data/content/sql-folders-query'
import { useSqlSnippetsQuery } from 'data/content/sql-snippets-query'
@@ -24,6 +25,7 @@ import {
useSnippetFolders,
useSqlEditorV2StateSnapshot,
} from 'state/sql-editor-v2'
+import { SqlSnippets } from 'types'
import { cn, Separator, TreeView } from 'ui'
import {
InnerSideBarEmptyPanel,
@@ -34,10 +36,8 @@ import {
} from 'ui-patterns'
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
import SQLEditorLoadingSnippets from './SQLEditorLoadingSnippets'
-import { ROOT_NODE, formatFolderResponseForTreeView, getLastItemIds } from './SQLEditorNav.utils'
+import { formatFolderResponseForTreeView, getLastItemIds, ROOT_NODE } from './SQLEditorNav.utils'
import { SQLEditorTreeViewItem } from './SQLEditorTreeViewItem'
-import { useContentUpsertV2Mutation } from 'data/content/content-upsert-v2-mutation'
-import { SqlSnippets } from 'types'
interface SQLEditorNavProps {
searchText: string
@@ -331,7 +331,7 @@ export const SQLEditorNav = ({
// Snippet mutations from RQ
// ==========================
- const { mutate: upsertContent, isLoading: isUpserting } = useContentUpsertV2Mutation({
+ const { mutate: upsertContent, isLoading: isUpserting } = useContentUpsertMutation({
onError: (error) => {
toast.error(`Failed to update query: ${error.message}`)
},
diff --git a/apps/studio/data/content/content-upsert-mutation.ts b/apps/studio/data/content/content-upsert-mutation.ts
index f0a2655afc0..2831298e4b7 100644
--- a/apps/studio/data/content/content-upsert-mutation.ts
+++ b/apps/studio/data/content/content-upsert-mutation.ts
@@ -25,17 +25,8 @@ export async function upsertContent(
) {
const { data, error } = await put('/platform/projects/{ref}/content', {
params: { path: { ref: projectRef } },
- body: {
- id: payload.id,
- name: payload.name,
- description: payload.description,
- project_id: payload.project_id,
- owner_id: payload.owner_id,
- type: payload.type,
- visibility: payload.visibility,
- content: payload.content as any,
- folder_id: payload.folder_id,
- },
+ body: payload,
+ headers: { Version: '2' },
signal,
})
if (error) handleError(error)
diff --git a/apps/studio/data/content/content-upsert-v2-mutation.ts b/apps/studio/data/content/content-upsert-v2-mutation.ts
deleted file mode 100644
index e456c1a7fce..00000000000
--- a/apps/studio/data/content/content-upsert-v2-mutation.ts
+++ /dev/null
@@ -1,72 +0,0 @@
-import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
-import { toast } from 'sonner'
-
-import type { components } from 'data/api'
-import { handleError, put } from 'data/fetchers'
-import type { ResponseError } from 'types'
-import type { Content } from './content-query'
-import { contentKeys } from './keys'
-
-export type UpsertContentPayloadV2 = Omit<
- components['schemas']['UpsertContentBodyDto'],
- 'content'
-> & {
- content: Content['content']
-}
-
-export type UpsertContentVariables = {
- projectRef: string
- payload: UpsertContentPayloadV2
-}
-
-export async function upsertContent(
- { projectRef, payload }: UpsertContentVariables,
- signal?: AbortSignal
-) {
- const { data, error } = await put('/platform/projects/{ref}/content', {
- params: { path: { ref: projectRef } },
- body: payload as any,
- headers: { Version: '2' },
- signal,
- })
-
- if (error) handleError(error)
- return data
-}
-
-export type UpsertContentData = Awaited<ReturnType<typeof upsertContent>>
-
-export const useContentUpsertV2Mutation = ({
- onError,
- onSuccess,
- invalidateQueriesOnSuccess = true,
- ...options
-}: Omit<
- UseMutationOptions<UpsertContentData, ResponseError, UpsertContentVariables>,
- 'mutationFn'
-> & {
- invalidateQueriesOnSuccess?: boolean
-} = {}) => {
- const queryClient = useQueryClient()
-
- return useMutation<UpsertContentData, ResponseError, UpsertContentVariables>(
- (args) => upsertContent(args),
- {
- async onSuccess(data, variables, context) {
- const { projectRef } = variables
- if (invalidateQueriesOnSuccess) {
- await queryClient.invalidateQueries(contentKeys.allContentLists(projectRef))
- }
- await onSuccess?.(data, variables, context)
- },
- async onError(data, variables, context) {
- if (onError === undefined) {
- toast.error(`Failed to upsert content: ${data.message}`)
- } else {
- onError(data, variables, context)
- }
- },
- ...options,
- }
- )
-}
diff --git a/apps/studio/state/sql-editor-v2.ts b/apps/studio/state/sql-editor-v2.ts
index f5b3d2f72ba..287eb1620c8 100644
--- a/apps/studio/state/sql-editor-v2.ts
+++ b/apps/studio/state/sql-editor-v2.ts
@@ -4,15 +4,14 @@ import { proxy, snapshot, subscribe, useSnapshot } from 'valtio'
import { devtools, proxySet } from 'valtio/utils'
import { DiffType } from 'components/interfaces/SQLEditor/SQLEditor.types'
-import { getContentById } from 'data/content/content-id-query'
-import { UpsertContentPayloadV2, upsertContent } from 'data/content/content-upsert-v2-mutation'
+import { upsertContent, UpsertContentPayload } from 'data/content/content-upsert-mutation'
import { contentKeys } from 'data/content/keys'
import { createSQLSnippetFolder } from 'data/content/sql-folder-create-mutation'
import { updateSQLSnippetFolder } from 'data/content/sql-folder-update-mutation'
import { Snippet, SnippetFolder } from 'data/content/sql-folders-query'
import { getQueryClient } from 'data/query-client'
-import { SqlSnippets } from 'types'
import { useMemo } from 'react'
+import { SqlSnippets } from 'types'
export type StateSnippetFolder = {
projectRef: string
@@ -285,7 +284,7 @@ export const useSnippets = (projectRef: string) => {
// ## Below are all the asynchronous saving logic for the SQL Editor
// ========================================================================
-async function upsertSnippet(id: string, projectRef: string, payload: UpsertContentPayloadV2) {
+async function upsertSnippet(id: string, projectRef: string, payload: UpsertContentPayload) {
try {
sqlEditorState.savingStates[id] = 'UPDATING'
await upsertContent({ projectRef, payload })
@@ -305,7 +304,7 @@ async function upsertSnippet(id: string, projectRef: string, payload: UpsertCont
const memoizedUpdateSnippet = memoize((_id: string) => debounce(upsertSnippet, 1000))
-const debouncedUpdateSnippet = (id: string, projectRef: string, payload: UpsertContentPayloadV2) =>
+const debouncedUpdateSnippet = (id: string, projectRef: string, payload: UpsertContentPayload) =>
memoizedUpdateSnippet(id)(id, projectRef, payload)
async function upsertFolder(id: string, projectRef: string, name: string) {