The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
chore: use @supabase/pg-meta for schema mutation (#22619)
Bobbie Soedirgof327f2b234a1
diff --git a/apps/studio/data/database/hooks-enable-mutation.ts b/apps/studio/data/database/hooks-enable-mutation.tsindex 504e891968e..68563763610 100644--- a/apps/studio/data/database/hooks-enable-mutation.ts+++ b/apps/studio/data/database/hooks-enable-mutation.ts@@ -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 } = variablesawait onSuccess?.(data, variables, context)- await queryClient.invalidateQueries(databaseKeys.schemaList(ref))+ await invalidateSchemasQuery(queryClient, ref)},async onError(data, variables, context) {if (onError === undefined) {diff --git a/apps/studio/data/database/keys.ts b/apps/studio/data/database/keys.tsindex 605386f9616..f80f1063f18 100644--- a/apps/studio/data/database/keys.ts+++ b/apps/studio/data/database/keys.ts@@ -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,diff --git a/apps/studio/data/database/schema-create-mutation.ts b/apps/studio/data/database/schema-create-mutation.tsindex 7e69126af6a..1bd5a6cab6a 100644--- a/apps/studio/data/database/schema-create-mutation.ts+++ b/apps/studio/data/database/schema-create-mutation.ts@@ -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) {diff --git a/apps/studio/data/database/schemas-query.ts b/apps/studio/data/database/schemas-query.tsindex b9bfaee5713..e93928dabc1 100644--- a/apps/studio/data/database/schemas-query.ts+++ b/apps/studio/data/database/schemas-query.ts@@ -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']))}diff --git a/packages/pg-meta/src/pg-meta-schemas.ts b/packages/pg-meta/src/pg-meta-schemas.tsindex 8458eeedfe0..83a636eb2fc 100644--- a/packages/pg-meta/src/pg-meta-schemas.ts+++ b/packages/pg-meta/src/pg-meta-schemas.ts@@ -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 {diff --git a/packages/pg-meta/src/sql/schemas.ts b/packages/pg-meta/src/sql/schemas.tsindex 1f70f7830f8..4ed70a12840 100644--- a/packages/pg-meta/src/sql/schemas.ts+++ b/packages/pg-meta/src/sql/schemas.ts@@ -1,4 +1,4 @@-export const SCHEMAS_SQL = `+export const SCHEMAS_SQL = /* SQL */ `-- Adapted from information_schema.schemataselect