Skip to content

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

refactor(studio): extract replication fields for wizard reuse (#51538)

Danny White188f5a743bc1
diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/AdvancedSettings.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/AdvancedSettings.tsx
index 8a339f5183d..b0dfa838610 100644
--- a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/AdvancedSettings.tsx
+++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/AdvancedSettings.tsx
@@ -1,40 +1,14 @@
-import type { ChangeEvent } from 'react'
import type { UseFormReturn } from 'react-hook-form'
-import {
- Accordion,
- AccordionContent,
- AccordionItem,
- AccordionTrigger,
- FormControl,
- FormField,
- FormInputGroupInput,
- InputGroup,
- InputGroupAddon,
- InputGroupText,
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
-} from 'ui'
-import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
+import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from 'ui'
import { DestinationType } from '../DestinationPanel.types'
import { BigQueryConnectionFields } from './BigQuery/ConnectionFields'
import { TableOptions } from './BigQuery/TableOptions'
-import {
- DEFAULT_MAX_COPY_CONNECTIONS_PER_TABLE,
- DEFAULT_MAX_FILL_MS,
- DEFAULT_MAX_TABLE_SYNC_WORKERS,
-} from './DestinationForm.constants'
import { type DestinationPanelSchemaType } from './DestinationForm.schema'
import { DuckLakeConnectionFields } from './DuckLake/ConnectionFields'
+import { ReplicationFields } from './ReplicationFields'
import { SnowflakeConnectionFields } from './Snowflake/ConnectionFields'
-const INVALIDATED_SLOT_BEHAVIOR_LABELS = {
- error: 'Block startup',
- recreate: 'Recreate slot',
-}
-
export const AdvancedSettings = ({
type,
form,
@@ -42,12 +16,6 @@ export const AdvancedSettings = ({
type: DestinationType
form: UseFormReturn<DestinationPanelSchemaType>
}) => {
- const handleNumberChange =
- (field: { onChange: (value: number | '') => void }) => (e: ChangeEvent<HTMLInputElement>) => {
- const parsed = e.target.valueAsNumber
- field.onChange(e.target.value === '' || Number.isNaN(parsed) ? '' : parsed)
- }
-
return (
<div className="w-full">
<Accordion type="single" collapsible>
@@ -65,126 +33,7 @@ export const AdvancedSettings = ({
<AccordionContent className="pb-0! pt-3 [&>div]:flex [&>div]:flex-col [&>div]:gap-y-4 [&>div]:px-5">
{type === 'DuckLake' && <DuckLakeConnectionFields control={form.control} />}
- <FormField
- control={form.control}
- name="maxFillMs"
- render={({ field }) => (
- <FormItemLayout
- layout="horizontal"
- label="Batch wait time"
- description="Maximum time before sending a partially filled batch."
- >
- <FormControl>
- <InputGroup>
- <FormInputGroupInput
- {...field}
- type="number"
- min={0}
- step={1}
- value={field.value ?? ''}
- onChange={handleNumberChange(field)}
- placeholder={String(DEFAULT_MAX_FILL_MS)}
- />
- <InputGroupAddon align="inline-end">
- <InputGroupText>milliseconds</InputGroupText>
- </InputGroupAddon>
- </InputGroup>
- </FormControl>
- </FormItemLayout>
- )}
- />
-
- <FormField
- control={form.control}
- name="maxTableSyncWorkers"
- render={({ field }) => (
- <FormItemLayout
- label="Table sync workers"
- layout="horizontal"
- description="Maximum number of tables synced at the same time."
- >
- <FormControl>
- <InputGroup>
- <FormInputGroupInput
- {...field}
- type="number"
- min={1}
- step={1}
- value={field.value ?? ''}
- onChange={handleNumberChange(field)}
- placeholder={String(DEFAULT_MAX_TABLE_SYNC_WORKERS)}
- />
- <InputGroupAddon align="inline-end">
- <InputGroupText>workers</InputGroupText>
- </InputGroupAddon>
- </InputGroup>
- </FormControl>
- </FormItemLayout>
- )}
- />
-
- <FormField
- control={form.control}
- name="maxCopyConnectionsPerTable"
- render={({ field }) => (
- <FormItemLayout
- label="Initial sync connections per table"
- layout="horizontal"
- description="Maximum number of source connections used to sync existing rows for each table."
- >
- <FormControl>
- <InputGroup>
- <FormInputGroupInput
- {...field}
- type="number"
- min={1}
- step={1}
- value={field.value ?? ''}
- onChange={handleNumberChange(field)}
- placeholder={String(DEFAULT_MAX_COPY_CONNECTIONS_PER_TABLE)}
- />
- <InputGroupAddon align="inline-end">
- <InputGroupText>connections</InputGroupText>
- </InputGroupAddon>
- </InputGroup>
- </FormControl>
- </FormItemLayout>
- )}
- />
-
- <FormField
- control={form.control}
- name="invalidatedSlotBehavior"
- render={({ field }) => (
- <FormItemLayout
- label="Invalidated slot behavior"
- layout="horizontal"
- description="What the pipeline does when its replication slot becomes invalid."
- >
- <FormControl>
- <Select value={field.value ?? 'error'} onValueChange={field.onChange}>
- <SelectTrigger>
- {INVALIDATED_SLOT_BEHAVIOR_LABELS[field.value ?? 'error']}
- </SelectTrigger>
- <SelectContent side="bottom" collisionPadding={16}>
- <SelectItem value="error" className="[&>span]:top-2.5">
- <p>Block startup</p>
- <p className="text-foreground-lighter">
- Blocks startup for manual recovery.
- </p>
- </SelectItem>
- <SelectItem value="recreate" className="[&>span]:top-2.5">
- <p>Recreate slot</p>
- <p className="text-foreground-lighter">
- Replaces destination tables and runs a new, billable initial sync.
- </p>
- </SelectItem>
- </SelectContent>
- </Select>
- </FormControl>
- </FormItemLayout>
- )}
- />
+ <ReplicationFields control={form.control} />
{type === 'BigQuery' && (
<>
diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/ReplicationFields.test.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/ReplicationFields.test.tsx
new file mode 100644
index 00000000000..e6f3011cbcc
--- /dev/null
+++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/ReplicationFields.test.tsx
@@ -0,0 +1,71 @@
+import { fireEvent, screen, waitFor } from '@testing-library/react'
+import { useForm } from 'react-hook-form'
+import { Button, Form } from 'ui'
+import { describe, expect, it, vi } from 'vitest'
+
+import type { DestinationPanelSchemaType } from './DestinationForm.schema'
+import { ReplicationFields } from './ReplicationFields'
+import { customRender } from '@/tests/lib/custom-render'
+
+const defaults = {
+ maxFillMs: 500,
+ maxTableSyncWorkers: 4,
+ maxCopyConnectionsPerTable: 1,
+ invalidatedSlotBehavior: 'error',
+} satisfies Partial<DestinationPanelSchemaType>
+const TestForm = ({ onSubmit }: { onSubmit: (data: DestinationPanelSchemaType) => void }) => {
+ const form = useForm<DestinationPanelSchemaType>({ defaultValues: defaults })
+ return (
+ <Form {...form}>
+ <form onSubmit={form.handleSubmit(onSubmit)}>
+ <ReplicationFields control={form.control} />
+ <Button type="submit">Submit</Button>
+ <Button type="button" onClick={() => form.reset()}>
+ Reset
+ </Button>
+ </form>
+ </Form>
+ )
+}
+
+describe('ReplicationFields', () => {
+ it.each([
+ { label: 'Batch wait time', name: 'maxFillMs', value: 0 },
+ { label: 'Table sync workers', name: 'maxTableSyncWorkers', value: 8 },
+ { label: 'Initial sync connections per table', name: 'maxCopyConnectionsPerTable', value: 3 },
+ ])('preserves editing, clearing and reset for $label', async ({ label, name, value }) => {
+ const onSubmit = vi.fn()
+ customRender(<TestForm onSubmit={onSubmit} />)
+ const input = screen.getByRole('spinbutton', { name: label })
+ fireEvent.change(input, { target: { value: String(value) } })
+ fireEvent.click(screen.getByRole('button', { name: 'Submit' }))
+ await waitFor(() =>
+ expect(onSubmit).toHaveBeenLastCalledWith({ ...defaults, [name]: value }, expect.anything())
+ )
+ fireEvent.change(input, { target: { value: '' } })
+ fireEvent.click(screen.getByRole('button', { name: 'Submit' }))
+ await waitFor(() =>
+ expect(onSubmit).toHaveBeenLastCalledWith({ ...defaults, [name]: '' }, expect.anything())
+ )
+ fireEvent.click(screen.getByRole('button', { name: 'Reset' }))
+ expect(input).toHaveValue(Reflect.get(defaults, name))
+ })
+
+ it('preserves the invalidated-slot choices and submits the selected behaviour', async () => {
+ const onSubmit = vi.fn()
+ customRender(<TestForm onSubmit={onSubmit} />)
+ const trigger = screen.getByRole('combobox')
+ expect(trigger).toHaveTextContent('Block startup')
+ fireEvent.keyDown(trigger, { key: 'ArrowDown' })
+ fireEvent.click(await screen.findByRole('option', { name: /Recreate slot/ }))
+ fireEvent.click(screen.getByRole('button', { name: 'Submit' }))
+ await waitFor(() =>
+ expect(onSubmit).toHaveBeenLastCalledWith(
+ { ...defaults, invalidatedSlotBehavior: 'recreate' },
+ expect.anything()
+ )
+ )
+ fireEvent.click(screen.getByRole('button', { name: 'Reset' }))
+ expect(trigger).toHaveTextContent('Block startup')
+ })
+})
diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/ReplicationFields.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/ReplicationFields.tsx
new file mode 100644
index 00000000000..a67e5484922
--- /dev/null
+++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/ReplicationFields.tsx
@@ -0,0 +1,162 @@
+import type { ChangeEvent } from 'react'
+import type { Control } from 'react-hook-form'
+import {
+ FormControl,
+ FormField,
+ FormInputGroupInput,
+ InputGroup,
+ InputGroupAddon,
+ InputGroupText,
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+} from 'ui'
+import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
+
+import {
+ DEFAULT_MAX_COPY_CONNECTIONS_PER_TABLE,
+ DEFAULT_MAX_FILL_MS,
+ DEFAULT_MAX_TABLE_SYNC_WORKERS,
+} from './DestinationForm.constants'
+import { type DestinationPanelSchemaType } from './DestinationForm.schema'
+
+const INVALIDATED_SLOT_BEHAVIOR_LABELS = {
+ error: 'Block startup',
+ recreate: 'Recreate slot',
+}
+
+export const ReplicationFields = ({
+ control,
+}: {
+ control: Control<DestinationPanelSchemaType>
+}) => {
+ const handleNumberChange =
+ (field: { onChange: (value: number | '') => void }) => (e: ChangeEvent<HTMLInputElement>) => {
+ const parsed = e.target.valueAsNumber
+ field.onChange(e.target.value === '' || Number.isNaN(parsed) ? '' : parsed)
+ }
+
+ return (
+ <>
+ <FormField
+ control={control}
+ name="maxFillMs"
+ render={({ field }) => (
+ <FormItemLayout
+ layout="horizontal"
+ label="Batch wait time"
+ description="Maximum time before sending a partially filled batch."
+ >
+ <FormControl>
+ <InputGroup>
+ <FormInputGroupInput
+ {...field}
+ type="number"
+ min={0}
+ step={1}
+ value={field.value ?? ''}
+ onChange={handleNumberChange(field)}
+ placeholder={String(DEFAULT_MAX_FILL_MS)}
+ />
+ <InputGroupAddon align="inline-end">
+ <InputGroupText>milliseconds</InputGroupText>
+ </InputGroupAddon>
+ </InputGroup>
+ </FormControl>
+ </FormItemLayout>
+ )}
+ />
+
+ <FormField
+ control={control}
+ name="maxTableSyncWorkers"
+ render={({ field }) => (
+ <FormItemLayout
+ label="Table sync workers"
+ layout="horizontal"
+ description="Maximum number of tables synced at the same time."
+ >
+ <FormControl>
+ <InputGroup>
+ <FormInputGroupInput
+ {...field}
+ type="number"
+ min={1}
+ step={1}
+ value={field.value ?? ''}
+ onChange={handleNumberChange(field)}
+ placeholder={String(DEFAULT_MAX_TABLE_SYNC_WORKERS)}
+ />
+ <InputGroupAddon align="inline-end">
+ <InputGroupText>workers</InputGroupText>
+ </InputGroupAddon>
+ </InputGroup>
+ </FormControl>
+ </FormItemLayout>
+ )}
+ />
+
+ <FormField
+ control={control}
+ name="maxCopyConnectionsPerTable"
+ render={({ field }) => (
+ <FormItemLayout
+ label="Initial sync connections per table"
+ layout="horizontal"
+ description="Maximum number of source connections used to sync existing rows for each table."
+ >
+ <FormControl>
+ <InputGroup>
+ <FormInputGroupInput
+ {...field}
+ type="number"
+ min={1}
+ step={1}
+ value={field.value ?? ''}
+ onChange={handleNumberChange(field)}
+ placeholder={String(DEFAULT_MAX_COPY_CONNECTIONS_PER_TABLE)}
+ />
+ <InputGroupAddon align="inline-end">
+ <InputGroupText>connections</InputGroupText>
+ </InputGroupAddon>
+ </InputGroup>
+ </FormControl>
+ </FormItemLayout>
+ )}
+ />
+
+ <FormField
+ control={control}
+ name="invalidatedSlotBehavior"
+ render={({ field }) => (
+ <FormItemLayout
+ label="Invalidated slot behavior"
+ layout="horizontal"
+ description="What the pipeline does when its replication slot becomes invalid."
+ >
+ <FormControl>
+ <Select value={field.value ?? 'error'} onValueChange={field.onChange}>
+ <SelectTrigger>
+ {INVALIDATED_SLOT_BEHAVIOR_LABELS[field.value ?? 'error']}
+ </SelectTrigger>
+ <SelectContent side="bottom" collisionPadding={16}>
+ <SelectItem value="error" className="[&>span]:top-2.5">
+ <p>Block startup</p>
+ <p className="text-foreground-lighter">Blocks startup for manual recovery.</p>
+ </SelectItem>
+ <SelectItem value="recreate" className="[&>span]:top-2.5">
+ <p>Recreate slot</p>
+ <p className="text-foreground-lighter">
+ Replaces destination tables and runs a new, billable initial sync.
+ </p>
+ </SelectItem>
+ </SelectContent>
+ </Select>
+ </FormControl>
+ </FormItemLayout>
+ )}
+ />
+ </>
+ )
+}