Files
supabase/apps/studio/data/storage/iceberg-namespaces-query.ts
T
Ivan VasilovandJoshen Lim 32c3eeb389 feat: Create new schema when creating a FDW with the schema option (#37205)
* Change the import foreign schema to always create a schema.

* Fix the Iceberg wrapper.

* Refactor the create wrapper to always create a new schema.

* Remove unneeded props.

* Smol fixes

* Prevent double error toasts

* Smol fix

* Fix the wrapper creation to include the correct api key.

* Fix a bug with the new api keys.

* Handle both types of keys when fetching the iceberg namespaces.

* Add a field to all wrappers to hold the schema name which can import foreign schema.

* sq

* When importing a foreign schema, save the schema in a special field.

* Fix a type error.

* Fix importing foreign schema overriding wrapper server options with unencrypted values

* Add comment

* Handle duplicate and empty schemas when importing a foreign schema.

* Update the copy in the foreign schema wrappers.

* Remove unnecessary code.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-07-25 10:54:17 +02:00

65 lines
1.8 KiB
TypeScript

import { UseQueryOptions, useQuery } from '@tanstack/react-query'
import { constructHeaders, fetchHandler, handleError } from 'data/fetchers'
import type { ResponseError } from 'types'
import { storageKeys } from './keys'
type GetNamespacesVariables = {
catalogUri: string
warehouse: string
token: string
}
async function getNamespaces({ catalogUri, warehouse, token }: GetNamespacesVariables) {
let headers = new Headers()
// handle both secret key and service role key
if (token.startsWith('sb_secret_')) {
headers = await constructHeaders({
'Content-Type': 'application/json',
apikey: `${token}`,
})
headers.delete('Authorization')
} else {
headers = await constructHeaders({
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
})
}
const url = `${catalogUri}/v1/${warehouse}/namespaces`.replaceAll(/(?<!:)\/\//g, '/')
try {
const response = await fetchHandler(url, {
headers,
method: 'GET',
})
const result = await response.json()
if (result.error) {
if (result.error.message) {
throw new Error(result.error.message)
}
throw new Error('Failed to get iceberg namespaces')
}
const r = result as { namespaces: string[][] }
return r.namespaces.flat()
} catch (error) {
handleError(error)
}
}
type IcebergNamespacesData = Awaited<ReturnType<typeof getNamespaces>>
export type IcebergNamespacesError = ResponseError
export const useIcebergNamespacesQuery = <TData = IcebergNamespacesData>(
params: GetNamespacesVariables,
{ ...options }: UseQueryOptions<IcebergNamespacesData, IcebergNamespacesError, TData> = {}
) => {
return useQuery<IcebergNamespacesData, IcebergNamespacesError, TData>(
storageKeys.icebergNamespaces(params.catalogUri, params.warehouse),
() => getNamespaces(params),
{ ...options }
)
}