From fa5317968e50c82c8e72fe747fcab871bf424094 Mon Sep 17 00:00:00 2001 From: Danny White <3104761+dnywh@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:04:47 +1100 Subject: [PATCH] choose a region in the pipeline creation sheet --- .../PipelineConnectionSection.tsx | 4 +- .../PipelineRegionField.platform.test.tsx | 2 +- .../PipelineRegionField.test.tsx | 6 +- .../DestinationForm/PipelineRegionField.tsx | 104 +++++++++++-- .../DestinationForm/index.tsx | 35 ++++- .../DestinationPanel.platform.test.tsx | 138 +++++++++++++++++- .../DestinationPanel/DestinationPanel.tsx | 7 + .../studio/data/replication/clusters-query.ts | 2 + 8 files changed, 269 insertions(+), 29 deletions(-) diff --git a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineConnectionSection.tsx b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineConnectionSection.tsx index d7699498226..9ab4043f976 100644 --- a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineConnectionSection.tsx +++ b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineConnectionSection.tsx @@ -12,18 +12,20 @@ export const PipelineConnectionSection = ({ type, form, children, + regionField, revealInvalidSettings = false, }: { type: DestinationType form: UseFormReturn children: ReactNode + regionField?: ReactNode revealInvalidSettings?: boolean }) => (

Destination connection

- + {regionField ?? }
{children} diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.platform.test.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.platform.test.tsx index dfc6633cd44..ee23e8fe2c2 100644 --- a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.platform.test.tsx +++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.platform.test.tsx @@ -24,7 +24,7 @@ test('shows the API default region as read-only information', async () => { }), }) - customRender() + customRender( {}} />) const region = screen.getByText(PIPELINE_REGION.displayName).closest('[aria-live="polite"]') expect(region).toHaveAttribute('aria-atomic', 'true') diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.test.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.test.tsx index 053943ceeb1..96cee80fd5f 100644 --- a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.test.tsx +++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.test.tsx @@ -12,13 +12,13 @@ import { customRender } from '@/tests/lib/custom-render' describe('getPipelineRegionDescription', () => { test('includes the fixed-region note and destination-specific hint', () => { expect(getPipelineRegionDescription('BigQuery')).toBe( - 'All pipelines run from this region. Choose a nearby BigQuery dataset where possible.' + 'Choose a nearby BigQuery dataset where possible.' ) }) test('falls back to a generic destination hint', () => { expect(getPipelineRegionDescription()).toBe( - 'All pipelines run from this region. Choose a nearby destination region where possible.' + 'Choose a nearby destination region where possible.' ) }) }) @@ -30,7 +30,7 @@ describe('PipelineRegionField', () => { expect(screen.getByText('Pipeline region')).toBeInTheDocument() expect(screen.getByText(PIPELINE_REGION.displayName)).toBeInTheDocument() expect(screen.getByText(PIPELINE_REGION.code)).toBeInTheDocument() - expect(screen.getByText(/All pipelines run from this region/)).toBeInTheDocument() + expect(screen.getByText(/Choose a nearby BigQuery dataset where possible/)).toBeInTheDocument() expect(screen.getByText(/nearby BigQuery dataset/)).toBeInTheDocument() expect(screen.queryByRole('combobox')).not.toBeInTheDocument() }) diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.tsx index 2b9ea214903..969abd28c9c 100644 --- a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.tsx +++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/PipelineRegionField.tsx @@ -1,15 +1,18 @@ import { useQuery } from '@tanstack/react-query' import { AWS_REGIONS } from 'shared-data' -import { cn } from 'ui' +import { cn, Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import type { DestinationType } from '../DestinationPanel.types' +import { toRequestClusterId } from '@/components/interfaces/Database/Replication/PipelineClusterScope.utils' import { RegionFlag } from '@/components/ui/RegionFlag' -import { replicationClustersQueryOptions } from '@/data/replication/clusters-query' +import { + replicationClustersQueryOptions, + type ReplicationRegion, +} from '@/data/replication/clusters-query' import { IS_STAGING_OR_LOCAL } from '@/lib/constants' import { useReplicationClusterId } from '@/state/replication-cluster' -// Keep the existing environment region as a fallback when discovery has no region code. export const PIPELINE_REGION = IS_STAGING_OR_LOCAL ? AWS_REGIONS.SOUTHEAST_ASIA : AWS_REGIONS.CENTRAL_EU @@ -21,21 +24,37 @@ const DESTINATION_REGION_HINT: Record = { ClickHouse: 'Choose a nearby ClickHouse cluster where possible.', } -export const getPipelineRegionDescription = (destinationType?: DestinationType) => { - const destinationHint = destinationType +export const getPipelineRegionDescription = (destinationType?: DestinationType) => + destinationType ? DESTINATION_REGION_HINT[destinationType] : 'Choose a nearby destination region where possible.' - return `All pipelines run from this region. ${destinationHint}` -} - export const PipelineRegionField = ({ destinationType, className, + onRegionChange, + preferredClusterId, + disabled, }: { destinationType?: DestinationType className?: string + /** Makes the region selectable when there is more than one region. */ + onRegionChange?: (clusterId: string | undefined) => void + preferredClusterId?: string + disabled?: boolean }) => { + const { data } = useQuery(replicationClustersQueryOptions()) + const hasMultipleRegions = (data?.regions.length ?? 0) > 1 + const clusterId = useReplicationClusterId() + const canSelectRegion = hasMultipleRegions && onRegionChange !== undefined && data !== undefined + const regions = data?.regions ?? [] + const firstRegion = regions.find( + (region) => region.cluster_id === (preferredClusterId ?? data?.default_cluster_id) + ) + const orderedRegions = firstRegion + ? [firstRegion, ...regions.filter((region) => region.cluster_id !== firstRegion.cluster_id)] + : regions + return ( - + {canSelectRegion ? ( + + onRegionChange(toRequestClusterId(value, data.default_cluster_id)) + } + /> + ) : ( + + )} ) } +const PipelineRegionSelect = ({ + regions, + value, + onValueChange, + className, + disabled, +}: { + regions: ReplicationRegion[] + value: string + onValueChange: (clusterId: string) => void + className?: string + disabled?: boolean +}) => ( + +) + export const PipelineRegionReadonly = ({ className }: { className?: string }) => { const selectedClusterId = useReplicationClusterId() const { data } = useQuery(replicationClustersQueryOptions()) const clusterId = selectedClusterId ?? data?.default_cluster_id - const cluster = data?.regions.find((region) => region.cluster_id === clusterId) - const region = cluster?.region_code ? cluster : undefined - const regionCode = region?.region_code ?? PIPELINE_REGION.code + const cluster = data?.regions.find((item) => item.cluster_id === clusterId) + const regionCode = cluster ? cluster.region_code : PIPELINE_REGION.code return (
className )} > - + {regionCode && } - {region?.region_name ?? PIPELINE_REGION.displayName} + {cluster?.region_name ?? PIPELINE_REGION.displayName} - {regionCode} + {regionCode && ( + {regionCode} + )}
) } diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/index.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/index.tsx index dd6b4b8eb37..ee6b74eb613 100644 --- a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/index.tsx +++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationForm/index.tsx @@ -56,6 +56,7 @@ import { useDestinationForm } from './useDestinationForm' import { ValidationFailuresSection } from './ValidationFailuresSection' import { ValidationWarningsDialog } from './ValidationWarningsDialog' import { usePipelineCreationPreview } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext' +import { useReplicationClusters } from '@/components/interfaces/Database/Replication/useReplicationClusters' import { AlertError } from '@/components/ui/AlertError' import { useReplicationDestinationByIdQuery } from '@/data/replication/destination-by-id-query' import { useReplicationPipelineByIdQuery } from '@/data/replication/pipeline-by-id-query' @@ -75,7 +76,9 @@ interface DestinationFormProps { checkIsDirtyRef?: RefObject<() => boolean> onClose: () => void onCancel?: () => void + onRegionChange?: (clusterId: string | undefined) => void isPreparingRegion?: boolean + preferredClusterId?: string } export const DestinationForm = ({ @@ -86,7 +89,9 @@ export const DestinationForm = ({ checkIsDirtyRef, onClose, onCancel = onClose, + onRegionChange, isPreparingRegion = false, + preferredClusterId, }: DestinationFormProps) => { const { ref: projectRef } = useParams() const clusterId = useReplicationClusterId() @@ -116,6 +121,9 @@ export const DestinationForm = ({ const validationSectionRef = useRef(null) const editMode = !!existingDestination + const { hasMultipleRegions } = useReplicationClusters() + // Publications are listed through the region's source, so the region is chosen first. + const canSelectRegion = !editMode && hasMultipleRegions && onRegionChange !== undefined const { isEnabled: isCreationPreviewEnabled } = usePipelineCreationPreview() const shouldShowConnectionSection = isCreationPreviewEnabled && !editMode @@ -253,8 +261,8 @@ export const DestinationForm = ({ const isSubmitDisabled = isSaving || - isPreparingRegion || (isManagedDuckLake && isDuckLakeProjectBlocked) || + isPreparingRegion || !isExistingConfigReady || !isSuccessPublicationNames || (!!publicationName && !isSuccessPublication) || @@ -479,6 +487,22 @@ export const DestinationForm = ({ ) + const regionField = ( + { + resetValidation() + onRegionChange(clusterId) + } + : undefined + } + disabled={isPreparingRegion || isSaving} + /> + ) + return ( <> @@ -488,7 +512,11 @@ export const DestinationForm = ({
{shouldShowConnectionSection && ( <> - + {destinationFields} @@ -515,12 +543,13 @@ export const DestinationForm = ({
+ {canSelectRegion && regionField} setPublicationPanelVisible(true)} /> - + {!canSelectRegion && regionField}
diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationPanel.platform.test.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationPanel.platform.test.tsx index 75a8866db4a..fefc3b9bf1e 100644 --- a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationPanel.platform.test.tsx +++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationPanel.platform.test.tsx @@ -1,5 +1,5 @@ import { QueryClient } from '@tanstack/react-query' -import { screen } from '@testing-library/react' +import { fireEvent, screen, waitFor, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import type { components } from 'api-types' import { FeatureFlagContext } from 'common' @@ -10,6 +10,7 @@ import { expect, test } from 'vitest' import { DestinationPanel } from './DestinationPanel' import { organizationKeys } from '@/data/organizations/keys' import type { OrganizationsData } from '@/data/organizations/organizations-query' +import type { ReplicationCostEstimateData } from '@/data/replication/cost-estimate-query' import { ReplicationClusterProvider } from '@/state/replication-cluster' import { PipelineRequestStatusProvider } from '@/state/replication-pipeline-request-status' import { createMockProject } from '@/tests/helpers' @@ -130,6 +131,13 @@ const renderPanel = (clusterId?: string) => { ) } +const findRegionSelector = () => screen.findByRole('combobox', { name: 'Pipeline region' }) + +const choosePublication = async (name: string) => { + await userEvent.click(await screen.findByText('Select publication')) + await userEvent.click(await screen.findByText(name)) +} + test('retries source discovery and opens creation when only another region is enabled', async () => { mockRegions() mockProject(FRANKFURT.region_code) @@ -153,16 +161,135 @@ test('opens the panel with a source only in a non-default region', async () => { mockPanelResources(new Set([FRANKFURT.cluster_id])) renderPanel() - expect(await screen.findByText('Frankfurt')).toBeVisible() + expect(await findRegionSelector()).toHaveTextContent('Frankfurt (eu-central-1)') }) -test('uses the default region when the project region lookup fails', async () => { +test('prepares the environment default from a non-default region context', async () => { + mockRegions() + mockProject(FRANKFURT.region_code) + const enabledClusters = new Set([FRANKFURT.cluster_id]) + mockPanelResources(enabledClusters) + const writes: (string | null)[] = [] + addAPIMock({ + method: 'post', + path: '/platform/replication/:ref/tenants-sources', + response: ({ request }) => { + const cluster = clusterFrom(request) + writes.push(cluster) + enabledClusters.add(cluster) + return HttpResponse.json({ + tenant_id: 'default', + source_id: 1, + }) + }, + }) + renderPanel(FRANKFURT.cluster_id) + + const selector = await findRegionSelector() + expect(selector).toHaveTextContent('Frankfurt (eu-central-1)') + await waitFor(() => expect(selector).toBeEnabled()) + await userEvent.click(selector) + const options = within(await screen.findByRole('listbox')).getAllByRole('option') + expect(options.map((option) => option.textContent)).toEqual([ + 'Frankfurt (eu-central-1)', + 'Singapore (ap-southeast-1)', + ]) + await userEvent.click(options[1]) + await waitFor(() => expect(selector).toBeEnabled()) + await userEvent.click(await screen.findByText('Select publication')) + expect(await screen.findByText('singapore')).toBeVisible() + expect(selector).toHaveTextContent('Singapore (ap-southeast-1)') + expect(writes).toEqual([null]) +}) + +test('keeps region selection available when the project region lookup fails', async () => { mockRegions() mockProject() mockPanelResources(new Set([null])) renderPanel() - expect(await screen.findByText('Singapore')).toBeVisible() + const selector = await findRegionSelector() + expect(selector).toHaveTextContent('Singapore (ap-southeast-1)') + await waitFor(() => expect(selector).toBeEnabled()) +}) + +test('locks the region selector while saving the pipeline', async () => { + mockRegions() + mockProject(SINGAPORE.region_code) + mockPanelResources(new Set([null])) + addAPIMock({ + method: 'get', + path: '/platform/replication/v2/:ref/sources/:source_id/publications/:publication_name', + response: { + name: 'singapore', + config: { + type: 'tables', + tables: [], + operations: ['insert'], + publish_via_partition_root: false, + }, + tables: [], + }, + }) + addAPIMock({ + method: 'get', + path: '/platform/replication/:ref/sources/:source_id/publications/:publication_name/cost-estimate', + response: () => + HttpResponse.json({ + currency: 'usd', + pipeline: { hourly_cost: 0.05, monthly_cost: 36.5 }, + streaming: { rate_per_gb: 3 }, + table_copy: { rate_per_gb: 0.6, total_bytes: 0, total_cost: 0, tables: [] }, + }), + }) + addAPIMock({ + method: 'post', + path: '/platform/replication/:ref/destinations/validate', + response: { validation_failures: [] }, + }) + addAPIMock({ + method: 'post', + path: '/platform/replication/:ref/pipelines/validate', + response: { validation_failures: [] }, + }) + const pendingSave = Promise.withResolvers() + const saveRequested = Promise.withResolvers() + addAPIMock({ + method: 'post', + path: '/platform/replication/:ref/destinations-pipelines', + response: async () => { + saveRequested.resolve() + await pendingSave.promise + return HttpResponse.json({ message: 'Create failed' }, { status: 500 }) + }, + }) + renderPanel() + + try { + const selector = await findRegionSelector() + await choosePublication('singapore') + const fields = { + 'Pipeline name': 'Warehouse', + 'Project ID': 'my-project', + 'Dataset ID': 'dataset', + 'Service account key': '{}', + } + for (const [name, value] of Object.entries(fields)) { + fireEvent.change(screen.getByLabelText(name), { target: { value } }) + } + const submit = screen.getByRole('button', { name: 'Start pipeline' }) + await waitFor(() => expect(submit).toBeEnabled()) + fireEvent.click(submit) + const confirm = await screen.findByRole('button', { name: 'Create and start pipeline' }) + await waitFor(() => expect(confirm).toBeEnabled()) + await userEvent.click(confirm) + await saveRequested.promise + await waitFor(() => expect(selector).toBeDisabled()) + pendingSave.resolve() + await waitFor(() => expect(selector).toBeEnabled()) + } finally { + pendingSave.resolve() + } }) test('prepares the source in the preferred region before loading its publications', async () => { @@ -186,9 +313,8 @@ test('prepares the source in the preferred region before loading its publication }) renderPanel() - expect(await screen.findByText('Frankfurt')).toBeVisible() + expect(await findRegionSelector()).toHaveTextContent('Frankfurt (eu-central-1)') await userEvent.click(await screen.findByText('Select publication')) expect(await screen.findByText('frankfurt')).toBeVisible() expect(writes).toEqual([FRANKFURT.cluster_id]) - expect(screen.queryByRole('combobox', { name: 'Pipeline region' })).not.toBeInTheDocument() }) diff --git a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationPanel.tsx b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationPanel.tsx index f4dfc04116b..5b3b0baa9db 100644 --- a/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationPanel.tsx +++ b/apps/studio/components/interfaces/Database/Replication/DestinationPanel/DestinationPanel.tsx @@ -133,6 +133,11 @@ export const DestinationPanel = () => { error: regionSourceError, reset: resetRegionSource, } = useEnsurePipelineRegionSource(projectRef) + const handleRegionChange = (clusterId: string | undefined) => { + setChosenClusterId(clusterId) + resetRegionSource() + void ensureRegionSource(clusterId) + } // A preselected region may need the project's source, just like an explicit pick. const attemptedPreferredClusterRef = useRef(null) @@ -294,6 +299,8 @@ export const DestinationPanel = () => { checkIsDirtyRef={checkIsDirtyRef} onClose={onClose} onCancel={confirmOnClose} + preferredClusterId={preferredCluster.clusterId} + onRegionChange={handleRegionChange} isPreparingRegion={isCreatingSource} /> diff --git a/apps/studio/data/replication/clusters-query.ts b/apps/studio/data/replication/clusters-query.ts index b9295a007c8..6a1cc90c333 100644 --- a/apps/studio/data/replication/clusters-query.ts +++ b/apps/studio/data/replication/clusters-query.ts @@ -17,6 +17,8 @@ export type ReplicationClustersData = Awaited queryOptions({ queryKey: replicationKeys.clusters(),