Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Remove schema selector in DB extensions enable dialog (#51440)

Joshen Lima76b861b505d
diff --git a/apps/studio/components/interfaces/Database/Extensions/EnableExtensionModal.tsx b/apps/studio/components/interfaces/Database/Extensions/EnableExtensionModal.tsx
index c114c804d11..9bc089786e8 100644
--- a/apps/studio/components/interfaces/Database/Extensions/EnableExtensionModal.tsx
+++ b/apps/studio/components/interfaces/Database/Extensions/EnableExtensionModal.tsx
@@ -1,10 +1,12 @@
-import { zodResolver } from '@hookform/resolvers/zod'
-import { useMemo } from 'react'
-import { useForm, useWatch } from 'react-hook-form'
import { toast } from 'sonner'
import {
- Badge,
Button,
+ Card,
+ CardContent,
+ cn,
+ Collapsible,
+ CollapsibleContent,
+ CollapsibleTrigger,
Dialog,
DialogContent,
DialogFooter,
@@ -12,44 +14,19 @@ import {
DialogSection,
DialogSectionSeparator,
DialogTitle,
- Form,
- FormControl,
- FormField,
- Input,
- Select,
- SelectContent,
- SelectItem,
- SelectSeparator,
- SelectTrigger,
- SelectValue,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
-import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
-import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
-import * as z from 'zod'
+import { CodeBlock } from 'ui-patterns/CodeBlock'
-import { extensionsWithRecommendedSchemas } from './Extensions.constants'
import { DocsButton } from '@/components/ui/DocsButton'
+import { InlineLinkClassName } from '@/components/ui/InlineLink'
import { useDatabaseExtensionEnableMutation } from '@/data/database-extensions/database-extension-enable-mutation'
import { type DatabaseExtension } from '@/data/database-extensions/database-extensions-query'
-import { useSchemasQuery } from '@/data/database/schemas-query'
-import { useSchemasFilteredForHighAvailability } from '@/hooks/misc/useHighAvailability'
import { useIsOrioleDb, useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
-import { useProtectedSchemas } from '@/hooks/useProtectedSchemas'
import { DOCS_URL } from '@/lib/constants'
const orioleExtCallOuts = ['vector', 'postgis']
-const FormSchema = z.object({ name: z.string(), schema: z.string() }).superRefine((val, ctx) => {
- if (val.schema === 'custom' && val.name.length === 0) {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- path: ['name'],
- message: 'Please provide a name for the schema',
- })
- }
-})
-
interface EnableExtensionModalProps {
visible: boolean
extension: DatabaseExtension
@@ -62,28 +39,8 @@ export const EnableExtensionModal = ({
onCancel,
}: EnableExtensionModalProps) => {
const isOrioleDb = useIsOrioleDb()
- const { data: project } = useSelectedProjectQuery()
- const { data: protectedSchemas } = useProtectedSchemas({ excludeSchemas: ['extensions'] })
- const recommendedSchema = extensionsWithRecommendedSchemas[extension.name]
-
- const { data: schemas = [], isPending: isLoading } = useSchemasQuery(
- {
- projectRef: project?.ref,
- connectionString: project?.connectionString,
- },
- { enabled: visible }
- )
- const visibleSchemas = useSchemasFilteredForHighAvailability(schemas)
- const availableSchemas = useMemo(
- () =>
- visibleSchemas.filter(
- (schema) =>
- schema.name === recommendedSchema ||
- !protectedSchemas.some((protectedSchema) => protectedSchema.name === schema.name)
- ),
- [visibleSchemas, recommendedSchema, protectedSchemas]
- )
+ const { data: project } = useSelectedProjectQuery()
// [Joshen] Hard-coding pg_cron here as this is enforced on our end (Not via pg_available_extension_versions)
const defaultSchema =
@@ -99,33 +56,16 @@ export const EnableExtensionModal = ({
},
})
- const defaultValues = { name: extension.name, schema: recommendedSchema ?? 'extensions' }
- const form = useForm<z.infer<typeof FormSchema>>({
- mode: 'onBlur',
- reValidateMode: 'onBlur',
- resolver: zodResolver(FormSchema),
- defaultValues,
- })
- const schema = useWatch({ control: form.control, name: 'schema' })
-
- const onSubmit = async (values: z.infer<typeof FormSchema>) => {
+ const onConfirmEnable = async () => {
if (project === undefined) return console.error('Project is required')
- const schema =
- defaultSchema !== undefined && defaultSchema !== null
- ? defaultSchema
- : values.schema === 'custom'
- ? values.name
- : values.schema
-
enableExtension({
projectRef: project.ref,
connectionString: project?.connectionString,
- schema,
+ schema: defaultSchema ?? 'extensions',
name: extension.name,
version: extension.default_version,
cascade: true,
- createSchema: !schema.startsWith('pg_'),
})
}
@@ -138,7 +78,7 @@ export const EnableExtensionModal = ({
>
<DialogContent size="small" aria-describedby={undefined}>
<DialogHeader>
- <DialogTitle>Enable {extension.name}</DialogTitle>
+ <DialogTitle>Confirm to enable {extension.name}</DialogTitle>
</DialogHeader>
<DialogSectionSeparator />
@@ -157,113 +97,59 @@ export const EnableExtensionModal = ({
</Admonition>
)}
- <DialogSection>
- <Form {...form}>
- <form id="enable-extensions-form" onSubmit={form.handleSubmit(onSubmit)}>
- {isLoading ? (
- <div className="space-y-2">
- <ShimmeringLoader />
- <div className="w-3/4">
- <ShimmeringLoader />
- </div>
- </div>
- ) : !!defaultSchema ? (
- <div className="flex flex-col gap-y-2">
- <FormItemLayout
- isReactForm={false}
- label="Select a schema to enable the extension for"
- >
- <Input disabled value={defaultSchema} />
- </FormItemLayout>
- <p className="text-sm text-foreground-light">
- Extension must be installed in the "{defaultSchema}" schema.
- </p>
- </div>
- ) : (
- <div className="flex flex-col gap-y-2">
- <FormField
- key="schema"
- name="schema"
- control={form.control}
- render={({ field }) => (
- <FormItemLayout
- name="schema"
- label="Select a schema to enable the extension for"
- >
- <FormControl>
- <Select
- value={field.value}
- onValueChange={field.onChange}
- disabled={!!defaultSchema}
- >
- <SelectTrigger>
- <SelectValue placeholder="Select a schema" />
- </SelectTrigger>
- <SelectContent>
- <SelectItem value="custom">
- Create a new schema{' '}
- <code className="text-code-inline">{extension.name}</code>
- </SelectItem>
- <SelectSeparator />
- {availableSchemas.map((schema) => {
- return (
- <SelectItem key={schema.id} value={schema.name}>
- {schema.name}
- {schema.name === recommendedSchema ? (
- <Badge className="ml-2" variant="success">
- Recommended
- </Badge>
- ) : !defaultSchema && schema.name === 'extensions' ? (
- <Badge className="ml-2">Default</Badge>
- ) : null}
- </SelectItem>
- )
- })}
- </SelectContent>
- </Select>
- </FormControl>
- </FormItemLayout>
- )}
- />
-
- {!!recommendedSchema && (
- <p className="text-sm text-foreground-light">
- Use the "{recommendedSchema}" schema for full compatibility with related
- features.
- </p>
- )}
-
- {schema === 'custom' && (
- <FormField
- key="name"
- name="name"
- control={form.control}
- render={({ field }) => (
- <FormItemLayout label="Schema name">
- <FormControl>
- <Input {...field} />
- </FormControl>
- </FormItemLayout>
- )}
- />
- )}
- </div>
+ <DialogSection className="flex flex-col gap-y-4">
+ <p className="text-sm text-foreground-light">
+ The following database extension will be enabled
+ </p>
+
+ <Card>
+ <CardContent className="divide-y text-sm p-0">
+ <div className="flex items-center justify-between px-4 py-2">
+ <p className="text-foreground-lighter">Extension</p>
+ <p className="text-foreground">{extension.name}</p>
+ </div>
+ <div className="flex items-center justify-between px-4 py-2">
+ <p className="text-foreground-lighter">Schema</p>
+ <p data-testid="enable-extension-schema" className="text-foreground">
+ {defaultSchema ?? 'extensions'}
+ </p>
+ </div>
+ </CardContent>
+ </Card>
+
+ <Collapsible>
+ <CollapsibleTrigger
+ className={cn(
+ InlineLinkClassName,
+ 'text-xs text-foreground-lighter data-open:text-foreground-light'
)}
- </form>
- </Form>
+ >
+ Need to install this in a different schema?
+ </CollapsibleTrigger>
+ <CollapsibleContent className="[overflow-y:clip] data-closed:animate-collapsible-up data-open:animate-collapsible-down">
+ <div className="my-2 text-xs text-foreground-light flex flex-col gap-y-1">
+ <p>
+ Installing in the extensions schema is highly recommended since some extensions
+ are hard to move after installation.
+ </p>
+ <p>To use a different schema, run this in the SQL Editor:</p>
+ </div>
+ <CodeBlock
+ language="pgsql"
+ hideLineNumbers
+ wrapperClassName={cn('[&_pre]:px-3 [&_pre]:py-3')}
+ className="[&_code]:text-xs"
+ value={`create extension if not exists ${extension.name} schema target_schema;`}
+ />
+ </CollapsibleContent>
+ </Collapsible>
</DialogSection>
<DialogFooter>
<Button disabled={isEnabling} onClick={() => onCancel()}>
Cancel
</Button>
- <Button
- variant="primary"
- type="submit"
- form="enable-extensions-form"
- loading={isEnabling}
- disabled={isLoading || isEnabling}
- >
+ <Button variant="primary" loading={isEnabling} onClick={() => onConfirmEnable()}>
Enable extension
</Button>
</DialogFooter>
diff --git a/e2e/studio/features/database.spec.ts b/e2e/studio/features/database.spec.ts
index eca44cfe1cd..8a056f397c2 100644
--- a/e2e/studio/features/database.spec.ts
+++ b/e2e/studio/features/database.spec.ts
@@ -900,9 +900,13 @@ test.describe('Database Extensions', () => {
const dialog = page.getByRole('dialog')
await expect(dialog, 'Enable extension dialog should be visible').toBeVisible()
await expect(
- dialog.getByText(`Enable ${EXTENSION_NAME}`),
+ dialog.getByText(`Confirm to enable ${EXTENSION_NAME}`),
'Dialog title should match extension name'
).toBeVisible()
+ await expect(
+ dialog.getByTestId('enable-extension-schema'),
+ 'Dialog should show the extension will be installed in the extensions schema'
+ ).toHaveText('extensions')
const enableWait = createApiResponseWaiter(page, 'pg-meta', ref, 'query?key=extension-create')
const refetchWait = createApiResponseWaiter(
@@ -977,113 +981,7 @@ test.describe('Database Extensions', () => {
).not.toBeChecked()
})
- test('can enable an extension in a different existing schema', async ({ page, ref }) => {
- await query(`DROP EXTENSION IF EXISTS ${EXTENSION_NAME} CASCADE;`)
-
- const extensionsWait = createApiResponseWaiter(
- page,
- 'pg-meta',
- ref,
- 'query?key=database-extensions'
- )
- await page.goto(toUrl(`/project/${env.PROJECT_REF}/database/extensions`))
- await extensionsWait
-
- await page.getByPlaceholder('Search for an extension').fill(EXTENSION_NAME)
-
- const row = page.getByRole('row').filter({ hasText: EXTENSION_NAME }).first()
- await expect(row, 'Extension row should be visible').toBeVisible()
- await row.getByRole('switch').click()
-
- const dialog = page.getByRole('dialog')
- await expect(dialog, 'Enable extension dialog should be visible').toBeVisible()
-
- // Change schema to 'public'
- await dialog.getByRole('combobox').click()
- await page.getByRole('option', { name: 'public', exact: true }).click()
-
- const enableWait = createApiResponseWaiter(page, 'pg-meta', ref, 'query?key=extension-create')
- const refetchWait = createApiResponseWaiter(
- page,
- 'pg-meta',
- ref,
- 'query?key=database-extensions'
- )
- await dialog.getByRole('button', { name: 'Enable extension' }).click()
- await enableWait
- await refetchWait
-
- await expect(
- page.getByText(`Extension "${EXTENSION_NAME}" is now enabled`),
- 'Success toast should appear after enabling in public schema'
- ).toBeVisible({ timeout: 15000 })
-
- await expect(
- row.getByRole('switch'),
- 'Extension switch should be checked after enabling in public schema'
- ).toBeChecked()
-
- // Cleanup
- await query(`DROP EXTENSION IF EXISTS ${EXTENSION_NAME} CASCADE;`)
- })
-
- test('can enable an extension in a new schema', async ({ page, ref }) => {
- await query(`DROP EXTENSION IF EXISTS ${EXTENSION_NAME} CASCADE;`)
-
- const extensionsWait = createApiResponseWaiter(
- page,
- 'pg-meta',
- ref,
- 'query?key=database-extensions'
- )
- await page.goto(toUrl(`/project/${env.PROJECT_REF}/database/extensions`))
- await extensionsWait
-
- await page.getByPlaceholder('Search for an extension').fill(EXTENSION_NAME)
-
- const row = page.getByRole('row').filter({ hasText: EXTENSION_NAME }).first()
- await expect(row, 'Extension row should be visible').toBeVisible()
- await row.getByRole('switch').click()
-
- const dialog = page.getByRole('dialog')
- await expect(dialog, 'Enable extension dialog should be visible').toBeVisible()
-
- // Select 'Create a new schema pgtap'
- await dialog.getByRole('combobox').click()
- await page.getByRole('option', { name: /Create a new schema/ }).click()
-
- const enableWait = createApiResponseWaiter(page, 'pg-meta', ref, 'query?key=extension-create')
- const refetchWait = createApiResponseWaiter(
- page,
- 'pg-meta',
- ref,
- 'query?key=database-extensions'
- )
- await dialog.getByRole('button', { name: 'Enable extension' }).click()
- await enableWait
- await refetchWait
-
- await expect(
- page.getByText(`Extension "${EXTENSION_NAME}" is now enabled`),
- 'Success toast should appear after enabling in new schema'
- ).toBeVisible({ timeout: 15000 })
-
- await expect(
- row.getByRole('switch'),
- 'Extension switch should be checked after enabling in new schema'
- ).toBeChecked()
-
- // Note: the created schema is owned by supabase_admin and cannot be dropped
- // by the test query helper (runs as postgres). The schema is left empty after
- // the extension is dropped and will be reused on subsequent runs via
- // CREATE SCHEMA IF NOT EXISTS in the enable SQL.
- await query(`DROP EXTENSION IF EXISTS ${EXTENSION_NAME} CASCADE;`)
- })
-
- test('cannot change the schema for extensions with a fixed default schema', async ({
- page,
- ref,
- }) => {
+ test('shows the fixed default schema for extensions that require one', async ({ page, ref }) => {
const FIXED_SCHEMA_EXTENSION = 'pgmq'
const FIXED_SCHEMA = 'pgmq'
@@ -1107,26 +1005,17 @@ test.describe('Database Extensions', () => {
const dialog = page.getByRole('dialog')
await expect(dialog, 'Enable extension dialog should be visible').toBeVisible()
- // Schema selector (combobox) should NOT be present for fixed-schema extensions
+ // Schema selector (combobox) should NOT be present
await expect(
dialog.getByRole('combobox'),
- 'Schema selector should not be present for extensions with a fixed default schema'
+ 'Schema selector should not be present'
).not.toBeVisible()
- // A disabled input showing the fixed schema should be present instead
- const schemaInput = dialog.getByRole('textbox')
- await expect(schemaInput, 'Fixed schema input should be visible').toBeVisible()
- await expect(schemaInput, 'Fixed schema input should be disabled').toBeDisabled()
+ // The fixed schema should be shown as the schema the extension will be installed in
await expect(
- schemaInput,
- 'Fixed schema input should display the required schema name'
- ).toHaveValue(FIXED_SCHEMA)
-
- // Helper text confirming the schema is required
- await expect(
- dialog.getByText(`Extension must be installed in the "${FIXED_SCHEMA}" schema.`),
- 'Helper text should indicate the schema is required'
- ).toBeVisible()
+ dialog.getByTestId('enable-extension-schema'),
+ 'Dialog should display the required schema name'
+ ).toHaveText(FIXED_SCHEMA)
// Cancel without enabling
await dialog.getByRole('button', { name: 'Cancel' }).click()