chore: use @supabase/pg-meta for schema mutation (#22619)

* chore: use @supabase/pg-meta for schema mutation

* fix schemas query invalidation

---------

Co-authored-by: Alaister Young <a@alaisteryoung.com>
This commit is contained in:
Bobbie SoedirgoandAlaister Young authored and GitHub committed 2024-04-17 11:04:21 +07:00
1 parent 9159bcf455
commit f327f2b234
6 files changed
+17 -24

No files matched your search

@@ -3,7 +3,7 @@ import { toast } from 'react-hot-toast'
import { post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { databaseKeys } from './keys'
import { invalidateSchemasQuery } from './schemas-query'
export type HooksEnableVariables = {
ref: string
@@ -34,7 +34,7 @@ export const useHooksEnableMutation = ({
async onSuccess(data, variables, context) {
const { ref } = variables
await onSuccess?.(data, variables, context)
await queryClient.invalidateQueries(databaseKeys.schemaList(ref))
await invalidateSchemasQuery(queryClient, ref)
},
async onError(data, variables, context) {
if (onError === undefined) {
-1
View File
@@ -1,5 +1,4 @@
export const databaseKeys = {
schemaList: (projectRef: string | undefined) => ['projects', projectRef, 'schemas'] as const,
backups: (projectRef: string | undefined) => [projectRef, 'database', 'backups'] as const,
poolingConfiguration: (projectRef: string | undefined) =>
[projectRef, 'database', 'pooling-configuration'] as const,
@@ -1,9 +1,11 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import pgMeta from '@supabase/pg-meta'
import { toast } from 'react-hot-toast'
import { post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { databaseKeys } from './keys'
import { executeSql } from 'data/sql/execute-sql-query'
import { invalidateSchemasQuery } from './schemas-query'
export type SchemaCreateVariables = {
name: string
@@ -12,22 +14,14 @@ export type SchemaCreateVariables = {
}
export async function createSchema({ name, projectRef, connectionString }: SchemaCreateVariables) {
if (!projectRef) throw new Error('projectRef is required')
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
const { data, error } = await post('/platform/pg-meta/{ref}/schemas', {
params: {
header: { 'x-connection-encrypted': connectionString! },
path: { ref: projectRef },
},
body: { name, owner: 'postgres' },
headers: Object.fromEntries(headers),
const sql = pgMeta.schemas.create({ name, owner: 'postgres' }).sql
const { result } = await executeSql({
projectRef,
connectionString,
sql,
queryKey: ['schema', 'create'],
})
if (error) throw error
return data
return result
}
type SchemaCreateData = Awaited<ReturnType<typeof createSchema>>
@@ -46,7 +40,7 @@ export const useSchemaCreateMutation = ({
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries(databaseKeys.schemaList(projectRef))
await invalidateSchemasQuery(queryClient, projectRef)
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
+2 -2
View File
@@ -3,7 +3,7 @@ import { QueryClient, UseQueryOptions } from '@tanstack/react-query'
import { z } from 'zod'
import { ExecuteSqlData, useExecuteSqlQuery } from 'data/sql/execute-sql-query'
import { databaseKeys } from './keys'
import { sqlKeys } from 'data/sql/keys'
export type SchemasVariables = {
projectRef?: string
@@ -37,5 +37,5 @@ export const useSchemasQuery = <TData = SchemasData>(
)
export function invalidateSchemasQuery(client: QueryClient, projectRef: string | undefined) {
return client.invalidateQueries(databaseKeys.schemaList(projectRef))
return client.invalidateQueries(sqlKeys.query(projectRef, ['schemas', 'list']))
}
+1 -1
View File
@@ -41,7 +41,7 @@ function list({
function retrieve({ id }: { id: number }): { sql: string; zod: typeof pgSchemaOptionalZod }
function retrieve({ name }: { name: string }): { sql: string; zod: typeof pgSchemaOptionalZod }
function retrieve({ id, name }: { id?: number; name?: string }) {
function retrieve({ id, name }: { id?: number; name?: string }): { sql: string; zod: typeof pgSchemaOptionalZod } {
if (id) {
const sql = `${SCHEMAS_SQL} and n.oid = ${literal(id)};`
return {
+1 -1
View File
@@ -1,4 +1,4 @@
export const SCHEMAS_SQL = `
export const SCHEMAS_SQL = /* SQL */ `
-- Adapted from information_schema.schemata
select