mirror of
https://github.com/supabase/supabase.git
synced 2026-10-09 03:15:06 +03:00
There is an edge case interaction between the Postgres query parser and MSSQL foreign tables, where the query parser may drop sort clauses that are redundant with applied filters. This leads to invalid MSSQL syntax, because the resulting query has a `limit` but no `sort`, and the user sees a confusing error message. This PR detects this edge case on MSSQL foreign tables. There are three cases: 1. The user filters by a column, but there are still other columns available for sorting. The default search for a sorting column will leave out the filtered column. 2. The user filters by a column/several columns, and there are no more columns that can be used for sorting. We stop the query and show an admonition. 3. The user filters by a column, then tries to sort by the same column. We stop the query and show an admonition.
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import type { ResponseError, UseCustomMutationOptions } from 'types'
|
|
import { tableKeys } from './keys'
|
|
|
|
export type CreateTableBody = {
|
|
name: string
|
|
schema?: string
|
|
comment?: string | null
|
|
}
|
|
|
|
export type TableCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
// the schema is required field
|
|
payload: CreateTableBody & { schema: string }
|
|
}
|
|
|
|
export async function createTable({ projectRef, connectionString, payload }: TableCreateVariables) {
|
|
const { sql } = pgMeta.tables.create(payload)
|
|
|
|
const { result } = await executeSql<void>({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['table', 'create'],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type TableCreateData = Awaited<ReturnType<typeof createTable>>
|
|
|
|
export const useTableCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<TableCreateData, ResponseError, TableCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<TableCreateData, ResponseError, TableCreateVariables>({
|
|
mutationFn: (vars) => createTable(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef, payload } = variables
|
|
|
|
await Promise.all([
|
|
queryClient.invalidateQueries({
|
|
queryKey: tableKeys.list(projectRef, payload.schema, true),
|
|
}),
|
|
queryClient.invalidateQueries({
|
|
queryKey: tableKeys.list(projectRef, payload.schema, false),
|
|
}),
|
|
])
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create database table: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|