Files
supabase/apps/studio/data/storage/iceberg-namespace-tables-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

76 lines
2.1 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 GetNamespaceTablesVariables = {
catalogUri: string
warehouse: string
token: string
namespace: string
}
async function getNamespaceTables({
catalogUri,
warehouse,
token,
namespace,
}: GetNamespaceTablesVariables) {
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/${namespace}/tables`.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 namespace')
}
const r = result as { identifiers: { name: string; namespace: string[] }[] }
return r.identifiers.map((i) => i.name)
} catch (error) {
handleError(error)
}
}
type IcebergNamespaceTablesData = Awaited<ReturnType<typeof getNamespaceTables>>
export type IcebergNamespaceTablesError = ResponseError
export const useIcebergNamespaceTablesQuery = <TData = IcebergNamespaceTablesData>(
params: GetNamespaceTablesVariables,
{
...options
}: UseQueryOptions<IcebergNamespaceTablesData, IcebergNamespaceTablesError, TData> = {}
) => {
return useQuery<IcebergNamespaceTablesData, IcebergNamespaceTablesError, TData>(
storageKeys.icebergNamespaceTables(params.catalogUri, params.warehouse, params.namespace),
() => getNamespaceTables(params),
{ ...options }
)
}