InitializeForeignSchemaDialog.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { zodResolver } from '@hookform/resolvers/zod'
  2. import { useParams } from 'common'
  3. import { useState } from 'react'
  4. import { SubmitHandler, useForm } from 'react-hook-form'
  5. import { toast } from 'sonner'
  6. import {
  7. Button,
  8. Dialog,
  9. DialogContent,
  10. DialogFooter,
  11. DialogHeader,
  12. DialogSection,
  13. DialogSectionSeparator,
  14. DialogTitle,
  15. DialogTrigger,
  16. Form,
  17. FormField,
  18. Input,
  19. } from 'ui'
  20. import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
  21. import z from 'zod'
  22. import { getAnalyticsBucketFDWServerName } from './AnalyticsBucketDetails.utils'
  23. import { DocsButton } from '@/components/ui/DocsButton'
  24. import { useSchemaCreateMutation } from '@/data/database/schema-create-mutation'
  25. import { useSchemasQuery } from '@/data/database/schemas-query'
  26. import { useFDWImportForeignSchemaMutation } from '@/data/fdw/fdw-import-foreign-schema-mutation'
  27. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  28. import { DOCS_URL } from '@/lib/constants'
  29. // Create foreign tables for namespace tables
  30. export const InitializeForeignSchemaDialog = ({ namespace }: { namespace: string }) => {
  31. const { ref: projectRef, bucketId } = useParams()
  32. const { data: project } = useSelectedProjectQuery()
  33. const { data: schemas } = useSchemasQuery({ projectRef })
  34. const [isOpen, setIsOpen] = useState(false)
  35. const [isCreating, setIsCreating] = useState(false)
  36. const serverName = getAnalyticsBucketFDWServerName(bucketId ?? '')
  37. const FormSchema = z.object({
  38. schema: z
  39. .string()
  40. .trim()
  41. .min(1, 'Schema name is required')
  42. .refine((val) => !schemas?.find((s) => s.name === val), {
  43. message: 'This schema already exists. Please specify a unique schema name.',
  44. }),
  45. })
  46. const form = useForm<z.infer<typeof FormSchema>>({
  47. resolver: zodResolver(FormSchema as any),
  48. defaultValues: { schema: '' },
  49. })
  50. const { mutateAsync: createSchema } = useSchemaCreateMutation()
  51. const { mutateAsync: importForeignSchema } = useFDWImportForeignSchemaMutation()
  52. const onSubmit: SubmitHandler<z.infer<typeof FormSchema>> = async (values) => {
  53. if (!projectRef) return console.error('Project ref is required')
  54. try {
  55. setIsCreating(true)
  56. await createSchema({
  57. projectRef,
  58. connectionString: project?.connectionString,
  59. name: values.schema,
  60. })
  61. await importForeignSchema({
  62. projectRef,
  63. connectionString: project?.connectionString,
  64. serverName: serverName,
  65. sourceSchema: namespace,
  66. targetSchema: values.schema,
  67. })
  68. toast.success(
  69. `Successfully created "${values.schema}" schema! Data from tables in the "${namespace}" namespace can now be queried from there.`
  70. )
  71. setIsOpen(false)
  72. } catch (error: any) {
  73. toast.error(`Failed to expose tables: ${error.message}`)
  74. } finally {
  75. setIsCreating(false)
  76. }
  77. }
  78. return (
  79. <Dialog open={isOpen} onOpenChange={setIsOpen}>
  80. <DialogTrigger asChild>
  81. <Button type="default">Query from Postgres</Button>
  82. </DialogTrigger>
  83. <DialogContent size="medium" aria-describedby={undefined}>
  84. <Form {...form}>
  85. <form onSubmit={form.handleSubmit(onSubmit)}>
  86. <DialogHeader>
  87. <DialogTitle>Query this namespace from Postgres</DialogTitle>
  88. </DialogHeader>
  89. <DialogSectionSeparator />
  90. <DialogSection className="flex flex-col gap-y-4">
  91. <p className="text-sm">
  92. Iceberg data can be queried from Postgres with the Iceberg Foreign Data Wrapper.
  93. Create a Postgres schema to expose tables from the "{namespace}" namespace as
  94. foreign tables.
  95. </p>
  96. <FormField
  97. control={form.control}
  98. name="schema"
  99. render={({ field }) => (
  100. <FormItemLayout layout="vertical" label="Schema name">
  101. <Input {...field} placeholder="Provide a name for your schema" />
  102. </FormItemLayout>
  103. )}
  104. />
  105. </DialogSection>
  106. <DialogFooter className="justify-between!">
  107. <DocsButton href={`${DOCS_URL}/guides/storage/analytics/query-with-postgres`} />
  108. <div className="flex items-center gap-x-2">
  109. <Button type="default" disabled={isCreating} onClick={() => setIsOpen(false)}>
  110. Cancel
  111. </Button>
  112. <Button htmlType="submit" type="primary" loading={isCreating}>
  113. Create schema
  114. </Button>
  115. </div>
  116. </DialogFooter>
  117. </form>
  118. </Form>
  119. </DialogContent>
  120. </Dialog>
  121. )
  122. }