Skip to content

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

feat(studio): show publication partition handling (#50773)

Danny Whitec415f502ed18
diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/NewPublicationPanel.test.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/NewPublicationPanel.test.tsx
index 239d87c29a7..8aaea4bf6e4 100644
--- a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/NewPublicationPanel.test.tsx
+++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/NewPublicationPanel.test.tsx
@@ -136,9 +136,12 @@ describe('NewPublicationPanel', () => {
fireEvent.change(screen.getByPlaceholderText('Name'), {
target: { value: 'MixedCasePublication' },
})
- fireEvent.click(
- screen.getByRole('checkbox', { name: 'Publish partitions as the parent table' })
- )
+ fireEvent.click(screen.getByRole('combobox', { name: 'Postgres partition handling' }))
+ const partitionOption = screen.getByRole('option', {
+ name: 'Replicate each partition separately',
+ })
+ expect(partitionOption).toHaveClass('whitespace-nowrap')
+ fireEvent.click(partitionOption)
fireEvent.click(screen.getByRole('button', { name: 'Create publication' }))
await waitFor(() =>
diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/NewPublicationPanel.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/NewPublicationPanel.tsx
index 18fc7e0eff1..5c3571e4c0b 100644
--- a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/NewPublicationPanel.tsx
+++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/NewPublicationPanel.tsx
@@ -5,11 +5,14 @@ import { useForm } from 'react-hook-form'
import { toast } from 'sonner'
import {
Button,
- Checkbox,
Form,
FormControl,
FormField,
Input,
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
Sheet,
SheetContent,
SheetDescription,
@@ -40,6 +43,11 @@ interface NewPublicationPanelProps {
const FORM_ID = 'publication-editor'
+const PARTITION_HANDLING_OPTIONS = {
+ parent: 'Use parent table identity',
+ partition: 'Replicate each partition separately',
+} as const
+
const FormSchema = z.object({
name: z.string().min(1, 'Name is required'),
tableIds: z.array(z.string()).min(1, 'At least one table is required'),
@@ -201,17 +209,31 @@ export const NewPublicationPanel = ({ visible, onClose }: NewPublicationPanelPro
name="publishViaPartitionRoot"
render={({ field }) => (
<FormItemLayout
- hideMessage
- label="Publish partitions as the parent table"
- description="Changes from partitioned tables appear in one destination table. Turn off to create a table for each partition."
- layout="flex"
+ label="Postgres partition handling"
+ description="Determines whether changes use the parent table or each partition."
+ layout="horizontal"
>
<FormControl>
- <Checkbox
- checked={field.value}
- aria-label="Publish partitions as the parent table"
- onCheckedChange={(checked) => field.onChange(checked === true)}
- />
+ <Select
+ value={field.value ? 'parent' : 'partition'}
+ onValueChange={(value) => field.onChange(value === 'parent')}
+ >
+ <SelectTrigger aria-label="Postgres partition handling">
+ <span className="min-w-0">
+ {field.value
+ ? PARTITION_HANDLING_OPTIONS.parent
+ : PARTITION_HANDLING_OPTIONS.partition}
+ </span>
+ </SelectTrigger>
+ <SelectContent className="w-max">
+ <SelectItem value="parent" className="whitespace-nowrap">
+ {PARTITION_HANDLING_OPTIONS.parent}
+ </SelectItem>
+ <SelectItem value="partition" className="whitespace-nowrap">
+ {PARTITION_HANDLING_OPTIONS.partition}
+ </SelectItem>
+ </SelectContent>
+ </Select>
</FormControl>
</FormItemLayout>
)}
diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PublicationSelection.test.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PublicationSelection.test.tsx
new file mode 100644
index 00000000000..3172c7c4c17
--- /dev/null
+++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PublicationSelection.test.tsx
@@ -0,0 +1,146 @@
+import { screen, waitFor } from '@testing-library/react'
+import type { components } from 'api-types'
+import { HttpResponse } from 'msw'
+import { useForm } from 'react-hook-form'
+import { Form } from 'ui'
+import { describe, expect, it, vi } from 'vitest'
+
+import type { DestinationPanelSchemaType } from './DestinationForm.schema'
+import { PublicationSelection } from './PublicationSelection'
+import { customRender } from '@/tests/lib/custom-render'
+import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
+
+type ReplicationSourcesResponse = components['schemas']['SourcesResponse_Output']
+type PublicationDetailsResponse = components['schemas']['PublicationDetailsResponse_Output']
+type PublicationNamesResponse = components['schemas']['ReadPublicationsResponse_Output']
+
+const mockSources: ReplicationSourcesResponse = {
+ sources: [
+ {
+ id: 1,
+ name: 'default',
+ tenant_id: 'tenant',
+ config: { host: 'db.internal', name: 'main-db', port: 5432, username: 'etl_user' },
+ },
+ ],
+}
+
+const mockPublicationDetails = (publishViaPartitionRoot: boolean): PublicationDetailsResponse => ({
+ name: 'analytics',
+ config: {
+ type: 'tables',
+ tables: [],
+ operations: ['insert', 'update', 'delete', 'truncate'],
+ publish_via_partition_root: publishViaPartitionRoot,
+ },
+ tables: [],
+})
+
+const mockPublicationRequests = (publishViaPartitionRoot: boolean) => {
+ addAPIMock({
+ method: 'get',
+ path: '/platform/replication/:ref/sources',
+ response: () => HttpResponse.json<ReplicationSourcesResponse>(mockSources),
+ })
+ addAPIMock({
+ method: 'get',
+ path: '/platform/replication/v2/:ref/sources/:source_id/publications',
+ response: () =>
+ HttpResponse.json<PublicationNamesResponse>({
+ publications: [{ name: 'analytics' }],
+ }),
+ })
+ addAPIMock({
+ method: 'get',
+ path: '/platform/replication/v2/:ref/sources/:source_id/publications/:publication_name',
+ response: () =>
+ HttpResponse.json<PublicationDetailsResponse>(
+ mockPublicationDetails(publishViaPartitionRoot)
+ ),
+ })
+}
+
+const PublicationSelectionHarness = () => {
+ const form = useForm<DestinationPanelSchemaType>({
+ defaultValues: { publicationName: 'analytics' },
+ })
+
+ return (
+ <Form {...form}>
+ <PublicationSelection form={form} onSelectNewPublication={vi.fn()} />
+ </Form>
+ )
+}
+
+describe('PublicationSelection', () => {
+ it('includes parent-table partition handling in the publication description', async () => {
+ mockPublicationRequests(true)
+ customRender(<PublicationSelectionHarness />)
+
+ expect(screen.getByRole('status')).toHaveTextContent('Loading partition handling...')
+
+ await waitFor(() =>
+ expect(
+ screen.getByText(
+ 'Tables in the selected publication will be replicated to this destination. Partitioned tables use the parent table identity.'
+ )
+ ).toBeInTheDocument()
+ )
+ expect(screen.queryByRole('textbox', { name: 'Postgres partition handling' })).toBeNull()
+ expect(screen.getByRole('status')).toHaveTextContent(
+ 'Partitioned tables use the parent table identity.'
+ )
+ })
+
+ it('includes separate-partition handling in the publication description', async () => {
+ mockPublicationRequests(false)
+ customRender(<PublicationSelectionHarness />)
+
+ await waitFor(() =>
+ expect(
+ screen.getByText(
+ 'Tables in the selected publication will be replicated to this destination. Each partition is replicated separately.'
+ )
+ ).toBeInTheDocument()
+ )
+ expect(screen.getByRole('status')).toHaveTextContent('Each partition is replicated separately.')
+ })
+
+ it('shows unavailable when no source matches the project', async () => {
+ addAPIMock({
+ method: 'get',
+ path: '/platform/replication/:ref/sources',
+ response: () =>
+ HttpResponse.json<ReplicationSourcesResponse>({
+ sources: [{ ...mockSources.sources[0], name: 'another-project' }],
+ }),
+ })
+ customRender(<PublicationSelectionHarness />)
+
+ expect(
+ await screen.findByText(
+ 'Tables in the selected publication will be replicated to this destination. Partition handling could not be loaded.'
+ )
+ ).toBeInTheDocument()
+ expect(screen.getByRole('status')).toHaveTextContent('Partition handling could not be loaded.')
+ })
+
+ it('shows unavailable when the sources query fails', async () => {
+ addAPIMock({
+ method: 'get',
+ path: '/platform/replication/:ref/sources',
+ response: () =>
+ HttpResponse.json<APIErrorBody>(
+ { message: 'replication API URL is not configured' },
+ { status: 503 }
+ ),
+ })
+ customRender(<PublicationSelectionHarness />)
+
+ expect(
+ await screen.findByText(
+ 'Tables in the selected publication will be replicated to this destination. Partition handling could not be loaded.'
+ )
+ ).toBeInTheDocument()
+ })
+})
diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PublicationSelection.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PublicationSelection.tsx
index 4b732594581..63afeaa54c3 100644
--- a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PublicationSelection.tsx
+++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PublicationSelection.tsx
@@ -7,7 +7,8 @@ import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import type { DestinationPanelSchemaType } from './DestinationForm.schema'
import { PublicationsComboBox } from './PublicationsComboBox'
import { useReplicationPublicationNamesQuery } from '@/data/replication/publication-names-query'
-import { useReplicationSourceId } from '@/data/replication/sources-query'
+import { useReplicationPublicationQuery } from '@/data/replication/publication-query'
+import { useReplicationSourcesQuery } from '@/data/replication/sources-query'
type PublicationSelectionProps = {
form: UseFormReturn<DestinationPanelSchemaType>
@@ -21,16 +22,46 @@ export const PublicationSelection = ({
const { ref: projectRef } = useParams()
const publicationName = useWatch({ control: form.control, name: 'publicationName' })
- const sourceId = useReplicationSourceId({ projectRef })
+ const {
+ data: sourcesData,
+ isError: isSourcesError,
+ isSuccess: isSourcesSuccess,
+ } = useReplicationSourcesQuery({ projectRef })
+ const sourceId = sourcesData?.sources.find((source) => source.name === projectRef)?.id
+ const isSourceUnavailable = isSourcesError || (isSourcesSuccess && sourceId === undefined)
const { data: publications, isSuccess: isSuccessPublications } =
useReplicationPublicationNamesQuery({ projectRef, sourceId })
-
+ const { data: selectedPublication, isError: isPublicationError } = useReplicationPublicationQuery(
+ { projectRef, sourceId, publicationName }
+ )
const isSelectedPublicationMissing =
isSuccessPublications &&
!!publicationName &&
!(publications ?? []).some((publication) => publication.name === publicationName)
+ let partitionHandlingMessage = ''
+ let isPartitionHandlingLoading = false
+
+ if (publicationName && !isSelectedPublicationMissing) {
+ if (selectedPublication?.name === publicationName) {
+ partitionHandlingMessage = selectedPublication.config.publish_via_partition_root
+ ? 'Partitioned tables use the parent table identity.'
+ : 'Each partition is replicated separately.'
+ } else if (isPublicationError || isSourceUnavailable) {
+ partitionHandlingMessage = 'Partition handling could not be loaded.'
+ } else {
+ isPartitionHandlingLoading = true
+ }
+ }
+
+ const publicationDescription = [
+ 'Tables in the selected publication will be replicated to this destination.',
+ isPartitionHandlingLoading ? 'Loading partition handling...' : partitionHandlingMessage,
+ ]
+ .filter(Boolean)
+ .join(' ')
+
return (
<FormField
control={form.control}
@@ -39,8 +70,13 @@ export const PublicationSelection = ({
<FormItemLayout
layout="horizontal"
label="Publication"
- description="Tables in the selected publication will be replicated to this destination."
+ description={publicationDescription}
>
+ <span role="status" className="sr-only">
+ {isPartitionHandlingLoading
+ ? 'Loading partition handling...'
+ : partitionHandlingMessage}
+ </span>
<FormControl>
<PublicationsComboBox
field={{