import { zodResolver } from '@hookform/resolvers/zod' import { useParams } from 'common' import { useState } from 'react' import { SubmitHandler, useForm } from 'react-hook-form' import { toast } from 'sonner' import { Button, Dialog, DialogContent, DialogFooter, DialogHeader, DialogSection, DialogSectionSeparator, DialogTitle, DialogTrigger, Form, FormField, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, Tooltip, TooltipContent, TooltipTrigger, } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import z from 'zod' import { getAnalyticsBucketFDWServerName } from './AnalyticsBucketDetails.utils' import { useAnalyticsBucketAssociatedEntities } from './useAnalyticsBucketAssociatedEntities' import { DocsButton } from '@/components/ui/DocsButton' import { InlineLinkClassName } from '@/components/ui/InlineLink' import { useFDWImportForeignSchemaMutation } from '@/data/fdw/fdw-import-foreign-schema-mutation' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { DOCS_URL } from '@/lib/constants' export const UpdateForeignSchemaDialog = ({ namespace, tables, }: { namespace: string tables: string[] }) => { const { ref: projectRef, bucketId } = useParams() const { data: project } = useSelectedProjectQuery() const [isOpen, setIsOpen] = useState(false) const { icebergWrapper } = useAnalyticsBucketAssociatedEntities({ bucketId }) const connectedForeignTablesForNamespace = (icebergWrapper?.tables ?? []).filter((x) => x.options[0].startsWith(`table=${namespace}.`) ) const schemasAssociatedWithNamespace = [ ...new Set(connectedForeignTablesForNamespace.map((x) => x.schema)), ] const serverName = getAnalyticsBucketFDWServerName(bucketId ?? '') const FormSchema = z.object({ schema: z.string().trim().min(1, 'Schema name is required'), }) const form = useForm>({ resolver: zodResolver(FormSchema as any), defaultValues: { schema: schemasAssociatedWithNamespace[0] }, values: { schema: schemasAssociatedWithNamespace[0] }, }) const { mutateAsync: importForeignSchema, isPending: isUpdating } = useFDWImportForeignSchemaMutation() const onSubmit: SubmitHandler> = async (values) => { if (!projectRef) return console.error('Project ref is required') try { await importForeignSchema({ projectRef, connectionString: project?.connectionString, serverName: serverName, sourceSchema: namespace, targetSchema: values.schema, }) toast.success(`Successfully updated "${values.schema}" schema!`) setIsOpen(false) } catch (error: any) { toast.error(`Failed to update schema: ${error.message}`) } } return (
Update schema to expose foreign tables

{tables.length > 1 ? ( <> There are{' '} {tables.length} tables {tables.join(', ')} {' '} that aren't included in the Iceberg Foreign Data Wrapper. Update the wrapper to create foreign tables for all unexposed tables. This will let you query the tables from Postgres. ) : ( `The table "${tables[0]}" in the "${namespace}" namespace is not yet included in the Iceberg Foreign Data Wrapper. The schema can be updated to expose this table as a foreign table.` )}

{schemasAssociatedWithNamespace.length > 1 ? ( ( )} /> ) : (

Confirm to update foreign schema on the "{namespace}" namespace?

)}
) }