Files
supabase/apps/studio/data/storage/iceberg-wrapper-create-mutation.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

77 lines
3.0 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import { WRAPPERS } from 'components/interfaces/Integrations/Wrappers/Wrappers.constants'
import {
getCatalogURI,
getConnectionURL,
} from 'components/interfaces/Storage/StorageSettings/StorageSettings.utils'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { getKeys, useAPIKeysQuery } from 'data/api-keys/api-keys-query'
import { useProjectSettingsV2Query } from 'data/config/project-settings-v2-query'
import { useProjectStorageConfigQuery } from 'data/config/project-storage-config-query'
import { FDWCreateVariables, useFDWCreateMutation } from 'data/fdw/fdw-create-mutation'
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { snakeCase } from 'lodash'
import { useS3AccessKeyCreateMutation } from './s3-access-key-create-mutation'
export const useIcebergWrapperCreateMutation = () => {
const { project } = useProjectContext()
const { data: apiKeys } = useAPIKeysQuery({ projectRef: project?.ref, reveal: true })
const { secretKey, serviceKey } = getKeys(apiKeys)
const { data: settings } = useProjectSettingsV2Query({ projectRef: project?.ref })
const protocol = settings?.app_config?.protocol ?? 'https'
const endpoint = settings?.app_config?.endpoint
const apiKey = secretKey?.api_key ?? serviceKey?.api_key ?? 'SUPABASE_CLIENT_API_KEY'
const wrapperMeta = WRAPPERS.find((wrapper) => wrapper.name === 'iceberg_wrapper')
const canCreateCredentials = useCheckPermissions(PermissionAction.STORAGE_ADMIN_WRITE, '*')
const { data: config } = useProjectStorageConfigQuery({ projectRef: project?.ref })
const { mutateAsync: createS3AccessKey, isLoading: isCreatingS3AccessKey } =
useS3AccessKeyCreateMutation()
const { mutateAsync: createFDW, isLoading: isCreatingFDW } = useFDWCreateMutation()
const mutateAsync = async ({ bucketName }: { bucketName: string }) => {
const createS3KeyData = await createS3AccessKey({
projectRef: project?.ref,
description: `${snakeCase(bucketName)}_keys`,
})
const wrapperName = `${snakeCase(bucketName)}_fdw`
const params: FDWCreateVariables = {
projectRef: project?.ref,
connectionString: project?.connectionString,
wrapperMeta: wrapperMeta!,
formState: {
wrapper_name: wrapperName,
server_name: `${wrapperName}_server`,
vault_aws_access_key_id: createS3KeyData?.access_key,
vault_aws_secret_access_key: createS3KeyData?.secret_key,
vault_token: apiKey,
warehouse: bucketName,
's3.endpoint': getConnectionURL(project?.ref ?? '', protocol, endpoint),
catalog_uri: getCatalogURI(project?.ref ?? '', protocol, endpoint),
},
mode: 'skip',
tables: [],
sourceSchema: '',
targetSchema: '',
}
await createFDW(params)
}
return {
mutateAsync,
isLoading: isCreatingFDW || isCreatingS3AccessKey,
hasPermission: canCreateCredentials,
}
}