Skip to content

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

fix(studio): improve pipeline cost estimate loading and recovery (#52056)

Danny Whited1e251181f29
diff --git a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCostEstimate.test.tsx b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCostEstimate.test.tsx
index 1c097a0cf24..70de5c86dda 100644
--- a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCostEstimate.test.tsx
+++ b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCostEstimate.test.tsx
@@ -42,8 +42,6 @@ describe('PipelineCostEstimate', () => {
customRender(
<PipelineCostEstimate
estimate={estimate}
- isLoading={false}
- isError={false}
publicationTables={publicationTables}
tableSyncCopy={{ type: 'include_tables', table_ids: [101] }}
/>
@@ -57,9 +55,7 @@ describe('PipelineCostEstimate', () => {
})
test('keeps pipeline creation available when the estimate is unavailable', () => {
- customRender(
- <PipelineCostEstimate isLoading={false} isError publicationTables={publicationTables} />
- )
+ customRender(<PipelineCostEstimate publicationTables={publicationTables} />)
expect(
screen.getByText('A cost estimate is unavailable. You can still start the pipeline.')
@@ -71,8 +67,6 @@ test('shows zero initial-sync cost when all existing rows are skipped', () => {
customRender(
<PipelineCostEstimate
estimate={estimate}
- isLoading={false}
- isError={false}
publicationTables={publicationTables}
tableSyncCopy={{ type: 'skip_all_tables' }}
/>
@@ -83,12 +77,23 @@ test('shows zero initial-sync cost when all existing rows are skipped', () => {
test('does not show stale rates while an estimate is loading', () => {
customRender(
- <PipelineCostEstimate
- estimate={estimate}
- isLoading
- isError={false}
- publicationTables={publicationTables}
- />
+ <PipelineCostEstimate estimate={estimate} isLoading publicationTables={publicationTables} />
)
expect(screen.queryByText('$0.05/hour')).not.toBeInTheDocument()
})
+
+test('keeps the status region mounted when loading finishes without an estimate', () => {
+ const { rerender } = customRender(
+ <PipelineCostEstimate isLoading publicationTables={publicationTables} />
+ )
+ const status = screen.getByRole('status')
+ expect(status).toBeEmptyDOMElement()
+
+ rerender(<PipelineCostEstimate publicationTables={publicationTables} />)
+
+ expect(screen.getByRole('status')).toBe(status)
+ expect(screen.queryByRole('alert')).not.toBeInTheDocument()
+ expect(status).toHaveTextContent(
+ 'A cost estimate is unavailable. You can still start the pipeline.'
+ )
+})
diff --git a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCostEstimate.tsx b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCostEstimate.tsx
index 5bb087fc883..2d2c4eef12e 100644
--- a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCostEstimate.tsx
+++ b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCostEstimate.tsx
@@ -9,6 +9,7 @@ import {
TableHeader,
TableRow,
} from 'ui'
+import { Admonition } from 'ui-patterns/Admonition'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
import {
@@ -16,6 +17,7 @@ import {
summarizeTableCopyEstimate,
type ReplicationTableIdentity,
} from '../TableSyncCopy.utils'
+import { SANDWICHED_ADMONITION_CLASS } from './PipelineValidationAdmonition'
import { InlineLink } from '@/components/ui/InlineLink'
import type { ReplicationCostEstimateData } from '@/data/replication/cost-estimate-query'
import type { TableSyncCopyConfig } from '@/data/replication/types'
@@ -26,14 +28,12 @@ const MAX_VISIBLE_TABLES = 10
export const PipelineCostEstimate = ({
estimate,
- isLoading,
- isError,
+ isLoading = false,
publicationTables,
tableSyncCopy,
}: {
estimate?: ReplicationCostEstimateData
- isLoading: boolean
- isError: boolean
+ isLoading?: boolean
publicationTables: ReplicationTableIdentity[]
tableSyncCopy?: TableSyncCopyConfig
}) => {
@@ -54,134 +54,141 @@ export const PipelineCostEstimate = ({
const copyTableCount = copyTargets.length
return (
- <CardContent className="space-y-6 border-b-0">
- <header className="space-y-1">
- <h3 className="text-sm text-foreground">Estimated costs</h3>
- <p className="text-sm text-foreground-light">
- Review one-time and ongoing Supabase charges. Destination-provider charges are separate.
- </p>
- </header>
+ <>
+ <CardContent className="space-y-6 border-b-0">
+ <header className="space-y-1">
+ <h3 className="text-sm text-foreground">Estimated costs</h3>
+ <p className="text-sm text-foreground-light">
+ Review one-time and ongoing Supabase charges. Destination-provider charges are separate.
+ </p>
+ </header>
- {isLoading ? (
- <GenericSkeletonLoader className="w-full" />
- ) : isError || estimate === undefined ? (
- <p className="text-sm text-foreground-light" role="status">
- A cost estimate is unavailable. You can still start the pipeline.
- </p>
- ) : (
- <div className="space-y-6">
- <section className="space-y-2">
- <h4 className="text-sm font-medium text-foreground mb-3">Initial sync</h4>
+ {isLoading && <GenericSkeletonLoader className="w-full" />}
+ {!isLoading && estimate !== undefined && (
+ <div className="space-y-6">
+ <section className="space-y-2">
+ <h4 className="text-sm font-medium text-foreground mb-3">Initial sync</h4>
- {copyTableCount === 0 ? (
- <div className="overflow-hidden rounded-md border">
- <Table>
- <TableBody className="[&_td]:py-2">
- <TableRow>
- <TableCell className="text-sm text-foreground-light">
- No tables will run an initial sync
- </TableCell>
- <TableCell className="text-right font-mono text-sm" translate="no">
- {formatCurrency(0)}
- </TableCell>
- </TableRow>
- </TableBody>
- </Table>
- </div>
- ) : copyEstimate?.isComplete ? (
- <div className="overflow-hidden rounded-md border">
- <Table>
- <TableHeader className="[&_th]:h-auto [&_th]:py-2 [&_th]:text-sm">
- <TableRow>
- <TableHead>Table</TableHead>
- <TableHead className="text-right">Est. volume</TableHead>
- <TableHead className="text-right" translate="no">
- Est. cost ({formatCurrency(estimate.table_copy.rate_per_gb)}/GB)
- </TableHead>
- </TableRow>
- </TableHeader>
- <TableBody className="[&_td]:py-2 [&_td]:text-sm">
- {visibleTables.map((table) => (
- <TableRow key={JSON.stringify([table.schema, table.name])}>
- <TableCell translate="no">
- {table.schema}.{table.name}
- </TableCell>
- <TableCell className="text-right">
- {formatBytes(table.estimated_bytes)}
+ {copyTableCount === 0 ? (
+ <div className="overflow-hidden rounded-md border">
+ <Table>
+ <TableBody className="[&_td]:py-2">
+ <TableRow>
+ <TableCell className="text-sm text-foreground-light">
+ No tables will run an initial sync
</TableCell>
- <TableCell className="text-right font-mono" translate="no">
- {formatCurrency(table.estimated_cost)}
+ <TableCell className="text-right font-mono text-sm" translate="no">
+ {formatCurrency(0)}
</TableCell>
</TableRow>
- ))}
- {hiddenTableCount > 0 && (
+ </TableBody>
+ </Table>
+ </div>
+ ) : copyEstimate?.isComplete ? (
+ <div className="overflow-hidden rounded-md border">
+ <Table>
+ <TableHeader className="[&_th]:h-auto [&_th]:py-2 [&_th]:text-sm">
<TableRow>
- <TableCell colSpan={3} className="text-foreground-lighter">
- +{hiddenTableCount} more {hiddenTableCount === 1 ? 'table' : 'tables'}
+ <TableHead>Table</TableHead>
+ <TableHead className="text-right">Est. volume</TableHead>
+ <TableHead className="text-right" translate="no">
+ Est. cost ({formatCurrency(estimate.table_copy.rate_per_gb)}/GB)
+ </TableHead>
+ </TableRow>
+ </TableHeader>
+ <TableBody className="[&_td]:py-2 [&_td]:text-sm">
+ {visibleTables.map((table) => (
+ <TableRow key={JSON.stringify([table.schema, table.name])}>
+ <TableCell translate="no">
+ {table.schema}.{table.name}
+ </TableCell>
+ <TableCell className="text-right">
+ {formatBytes(table.estimated_bytes)}
+ </TableCell>
+ <TableCell className="text-right font-mono" translate="no">
+ {formatCurrency(table.estimated_cost)}
+ </TableCell>
+ </TableRow>
+ ))}
+ {hiddenTableCount > 0 && (
+ <TableRow>
+ <TableCell colSpan={3} className="text-foreground-lighter">
+ +{hiddenTableCount} more {hiddenTableCount === 1 ? 'table' : 'tables'}
+ </TableCell>
+ </TableRow>
+ )}
+ </TableBody>
+ <TableFooter className="[&_td]:text-sm">
+ <TableRow className="border-b-0">
+ <TableCell className="py-2">Initial sync total</TableCell>
+ <TableCell className="py-2 text-right">
+ {formatBytes(copyEstimate.estimatedBytes)}
+ </TableCell>
+ <TableCell className="py-2 text-right font-mono" translate="no">
+ {formatCurrency(copyEstimate.estimatedCost)}
</TableCell>
</TableRow>
- )}
- </TableBody>
- <TableFooter className="[&_td]:text-sm">
- <TableRow className="border-b-0">
- <TableCell className="py-2">Initial sync total</TableCell>
- <TableCell className="py-2 text-right">
- {formatBytes(copyEstimate.estimatedBytes)}
+ </TableFooter>
+ </Table>
+ </div>
+ ) : (
+ <div className="overflow-hidden rounded-md border">
+ <p className="px-3 py-2 text-sm text-foreground-light">
+ Estimate unavailable for one or more selected tables
+ </p>
+ </div>
+ )}
+
+ <p className="text-sm text-foreground-lighter">
+ Final charges are based on data processed during initial sync.
+ {copyEstimate?.isComplete && copyEstimate.hasRowFilteredTables
+ ? ' Row filters may reduce this amount.'
+ : ''}
+ </p>
+ </section>
+
+ <section className="space-y-2">
+ <h4 className="text-sm font-medium text-foreground mb-3">Ongoing</h4>
+ <div className="overflow-hidden rounded-md border">
+ <Table>
+ <TableBody className="[&_td]:py-2 [&_td]:text-sm">
+ <TableRow>
+ <TableCell className="text-foreground-light">Pipeline runtime</TableCell>
+ <TableCell className="text-right font-mono text-foreground" translate="no">
+ ${estimate.pipeline.hourly_cost}/hour
</TableCell>
- <TableCell className="py-2 text-right font-mono" translate="no">
- {formatCurrency(copyEstimate.estimatedCost)}
+ </TableRow>
+ <TableRow>
+ <TableCell className="text-foreground-light">
+ Replication data processed
+ </TableCell>
+ <TableCell className="text-right font-mono text-foreground" translate="no">
+ {formatCurrency(estimate.streaming.rate_per_gb)}/GB
</TableCell>
</TableRow>
- </TableFooter>
+ </TableBody>
</Table>
</div>
- ) : (
- <div className="overflow-hidden rounded-md border">
- <p className="px-3 py-2 text-sm text-foreground-light">
- Estimate unavailable for one or more selected tables
- </p>
- </div>
- )}
-
- <p className="text-sm text-foreground-lighter">
- Final charges are based on data processed during initial sync.
- {copyEstimate?.isComplete && copyEstimate.hasRowFilteredTables
- ? ' Row filters may reduce this amount.'
- : ''}
- </p>
- </section>
-
- <section className="space-y-2">
- <h4 className="text-sm font-medium text-foreground mb-3">Ongoing</h4>
- <div className="overflow-hidden rounded-md border">
- <Table>
- <TableBody className="[&_td]:py-2 [&_td]:text-sm">
- <TableRow>
- <TableCell className="text-foreground-light">Pipeline runtime</TableCell>
- <TableCell className="text-right font-mono text-foreground" translate="no">
- ${estimate.pipeline.hourly_cost}/hour
- </TableCell>
- </TableRow>
- <TableRow>
- <TableCell className="text-foreground-light">
- Replication data processed
- </TableCell>
- <TableCell className="text-right font-mono text-foreground" translate="no">
- {formatCurrency(estimate.streaming.rate_per_gb)}/GB
- </TableCell>
- </TableRow>
- </TableBody>
- </Table>
- </div>
- <p className="text-sm text-foreground-lighter">
- Replication usage depends on how often published data changes.{' '}
- <InlineLink href={`${DOCS_URL}/guides/platform/manage-your-usage/pipelines`}>
- Learn more
- </InlineLink>
- </p>
- </section>
- </div>
- )}
- </CardContent>
+ <p className="text-sm text-foreground-lighter">
+ Replication usage depends on how often published data changes.{' '}
+ <InlineLink href={`${DOCS_URL}/guides/platform/manage-your-usage/pipelines`}>
+ Learn more
+ </InlineLink>
+ </p>
+ </section>
+ </div>
+ )}
+ </CardContent>
+ <div role="status">
+ {!isLoading && estimate === undefined && (
+ <Admonition
+ role={undefined}
+ type="note"
+ className={SANDWICHED_ADMONITION_CLASS}
+ description="A cost estimate is unavailable. You can still start the pipeline."
+ />
+ )}
+ </div>
+ </>
)
}
diff --git a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCreationFlow.test.tsx b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCreationFlow.test.tsx
index da10ae6121a..ca03b833c47 100644
--- a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCreationFlow.test.tsx
+++ b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCreationFlow.test.tsx
@@ -1,3 +1,4 @@
+import { QueryClient } from '@tanstack/react-query'
import { act, fireEvent, screen, waitFor, within } from '@testing-library/react'
import type { components } from 'api-types'
import { mockAnimationsApi } from 'jsdom-testing-mocks'
@@ -7,6 +8,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import { PipelineCreationFlow as CreationFlow } from './PipelineCreationFlow'
import type { ReplicationCostEstimateData } from '@/data/replication/cost-estimate-query'
+import { replicationKeys } from '@/data/replication/keys'
import { PipelineRequestStatusProvider } from '@/state/replication-pipeline-request-status'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
@@ -135,9 +137,15 @@ const selectPublication = async () => {
const back = () => fireEvent.click(within(stepCard()).getByRole('button', { name: 'Back' }))
describe('PipelineCreationFlow', () => {
- const reachReview = async (onClose = vi.fn(), onUrlUpdate = vi.fn()) => {
+ const reachReview = async (
+ onClose = vi.fn(),
+ onUrlUpdate = vi.fn(),
+ queryClient?: QueryClient,
+ shouldWaitForEstimate = true
+ ) => {
customRender(<PipelineCreationFlow availableDestinations={['BigQuery']} onClose={onClose} />, {
nuqs: { hasMemory: true, rateLimitFactor: 1, onUrlUpdate },
+ queryClient,
})
next()
fillConnection()
@@ -145,6 +153,10 @@ describe('PipelineCreationFlow', () => {
await selectPublication()
next()
await screen.findByText(/Step 4 of 4/)
+ if (shouldWaitForEstimate)
+ await waitFor(() =>
+ expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
+ )
}
const mockCreation = ({ failCreate = false, failStart = false } = {}) => {
@@ -694,7 +706,9 @@ describe('PipelineCreationFlow', () => {
await selectPublication()
next()
await waitForStep(4)
- expect(within(stepCard()).getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
+ await waitFor(() =>
+ expect(within(stepCard()).getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
+ )
expect(screen.getByDisplayValue('Warehouse')).toBeInTheDocument()
})
@@ -870,4 +884,73 @@ describe('PipelineCreationFlow', () => {
fireEvent.click(screen.getByRole('button', { name: 'Close' }))
expect(onClose).toHaveBeenCalledOnce()
})
+
+ it.each(['success', 'error'] as const)(
+ 'waits for the initial cost estimate and permits starting after %s',
+ async (result) => {
+ const { requests } = mockCreation()
+ let finishEstimate = () => {}
+ const gate = new Promise<void>((resolve) => {
+ finishEstimate = resolve
+ })
+ addAPIMock({
+ method: 'get',
+ path: '/platform/replication/:ref/sources/:source_id/publications/:publication_name/cost-estimate',
+ response: async () => {
+ await gate
+ if (result === 'error')
+ return HttpResponse.json<{ message: string }>(
+ { message: 'Estimate unavailable' },
+ { status: 503 }
+ )
+ return HttpResponse.json<ReplicationCostEstimateData>({
+ 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: [] },
+ })
+ },
+ })
+ await reachReview(vi.fn(), vi.fn(), undefined, false)
+ const start = screen.getByRole('button', { name: 'Start pipeline' })
+ expect(start).toBeDisabled()
+ fireEvent.click(start)
+ expect(requests).toHaveLength(0)
+ expect(screen.getByRole('button', { name: 'Back' })).toBeEnabled()
+ finishEstimate()
+ await waitFor(() => expect(start).toBeEnabled())
+ if (result === 'error')
+ expect(screen.getByText(/A cost estimate is unavailable/)).toBeInTheDocument()
+ fireEvent.click(start)
+ await waitFor(() => expect(requests).toHaveLength(1))
+ }
+ )
+
+ it('keeps starting available during a background cost refresh with a cached estimate', async () => {
+ const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
+ const key = replicationKeys.costEstimate('default', 1, 'analytics')
+ queryClient.setQueryData<ReplicationCostEstimateData>(key, {
+ 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: 'get',
+ path: '/platform/replication/:ref/sources/:source_id/publications/:publication_name/cost-estimate',
+ response: async () => {
+ await delay('infinite')
+ return HttpResponse.json<ReplicationCostEstimateData>({
+ 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: [] },
+ })
+ },
+ })
+ await reachReview(vi.fn(), vi.fn(), queryClient)
+ await waitFor(() => expect(queryClient.getQueryState(key)?.fetchStatus).toBe('fetching'))
+ expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
+ expect(screen.getByRole('heading', { name: 'Initial sync' })).toBeInTheDocument()
+ })
})
diff --git a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCreationFlow.tsx b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCreationFlow.tsx
index 60bb9f03fcf..897fc621564 100644
--- a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCreationFlow.tsx
+++ b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineCreationFlow.tsx
@@ -25,6 +25,7 @@ import { usePipelineConnectionValidation } from './usePipelineConnectionValidati
import { usePipelineCreationSubmission } from './usePipelineCreationSubmission'
import { usePipelineDataValidation } from './usePipelineDataValidation'
import { usePipelineNavigationGuard } from './usePipelineNavigationGuard'
+import { usePipelineReviewCosts } from './usePipelineReviewCosts'
import { DiscardChangesConfirmationDialog } from '@/components/ui-patterns/Dialogs/DiscardChangesConfirmationDialog'
import { SteppedFlow, SteppedFlowHeader } from '@/components/ui/SteppedFlow/SteppedFlow'
import { useReplicationSourceId } from '@/data/replication/sources-query'
@@ -90,6 +91,11 @@ export const PipelineCreationFlow = ({
if (step === 'review' && !isDataVerified) return 'data'
return step
})()
+ const costs = usePipelineReviewCosts({
+ control: form.control,
+ publication,
+ enabled: accessibleStep === 'review',
+ })
const handleNext = async () => {
if (accessibleStep === 'connection' && !isConnectionVerified && !(await validateConnection()))
return
@@ -134,6 +140,7 @@ export const PipelineCreationFlow = ({
label: submitLabel,
onClick: handleSubmit,
loading: isSubmitting,
+ disabled: costs.isInitialCostEstimateLoading,
}}
>
<SteppedFlowHeader title={currentStep.title} />
diff --git a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineReviewCosts.tsx b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineReviewCosts.tsx
index efbaca43ec7..a1135fcfef0 100644
--- a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineReviewCosts.tsx
+++ b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/PipelineReviewCosts.tsx
@@ -1,12 +1,9 @@
-import { useParams } from 'common'
-import { useWatch, type Control } from 'react-hook-form'
+import type { Control } from 'react-hook-form'
import type { DestinationPanelSchemaType } from '../DestinationPanel/DestinationForm/DestinationForm.schema'
-import { buildTableSyncCopyConfig } from '../DestinationPanel/DestinationForm/DestinationForm.utils'
import { PipelineCostEstimate } from './PipelineCostEstimate'
-import { useReplicationCostEstimateQuery } from '@/data/replication/cost-estimate-query'
+import { usePipelineReviewCosts } from './usePipelineReviewCosts'
import type { ReplicationPublicationData } from '@/data/replication/publication-query'
-import { useReplicationSourceId } from '@/data/replication/sources-query'
export const PipelineReviewCosts = ({
control,
@@ -15,40 +12,25 @@ export const PipelineReviewCosts = ({
control: Control<DestinationPanelSchemaType>
publication?: ReplicationPublicationData
}) => {
- const { ref: projectRef } = useParams()
- const sourceId = useReplicationSourceId({ projectRef })
- const [publicationName, mode, ids] = useWatch({
- control,
- name: ['publicationName', 'tableSyncCopyMode', 'tableSyncCopyTableIds'],
- })
- const hasPublication = !!publicationName && publication?.name === publicationName
- const { data, isLoading, isError } = useReplicationCostEstimateQuery(
- { projectRef, sourceId, publicationName },
- { enabled: hasPublication }
- )
- if (!hasPublication)
+ const costs = usePipelineReviewCosts({ control, publication })
+ if (costs.status === 'missing-publication')
return (
<div className="p-5 text-sm text-foreground-light">
Select an available publication to estimate costs.
</div>
)
- let tableSyncCopy
- try {
- tableSyncCopy = buildTableSyncCopyConfig({ mode, selectedTableIds: ids })
- } catch {
+ if (costs.status === 'missing-selection')
return (
<div className="p-5 text-sm text-foreground-light">
Complete the initial sync selection to estimate costs.
</div>
)
- }
return (
<PipelineCostEstimate
- estimate={data}
- isLoading={isLoading}
- isError={isError}
- publicationTables={publication.tables}
- tableSyncCopy={tableSyncCopy}
+ estimate={costs.estimate}
+ isLoading={costs.isInitialCostEstimateLoading}
+ publicationTables={costs.publicationTables}
+ tableSyncCopy={costs.tableSyncCopy}
/>
)
}
diff --git a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/usePipelineReviewCosts.test.tsx b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/usePipelineReviewCosts.test.tsx
new file mode 100644
index 00000000000..07f82a32d9d
--- /dev/null
+++ b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/usePipelineReviewCosts.test.tsx
@@ -0,0 +1,169 @@
+import { QueryClient } from '@tanstack/react-query'
+import { act, screen, waitFor } from '@testing-library/react'
+import type { components } from 'api-types'
+import { HttpResponse } from 'msw'
+import { useForm } from 'react-hook-form'
+import { beforeEach, expect, test } from 'vitest'
+
+import type { DestinationPanelSchemaType } from '../DestinationPanel/DestinationForm/DestinationForm.schema'
+import { PipelineReviewCosts } from './PipelineReviewCosts'
+import { usePipelineReviewCosts } from './usePipelineReviewCosts'
+import type { ReplicationCostEstimateData } from '@/data/replication/cost-estimate-query'
+import { replicationKeys } from '@/data/replication/keys'
+import type { ReplicationPublicationData } from '@/data/replication/publication-query'
+import { customRender } from '@/tests/lib/custom-render'
+import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
+
+const publication: ReplicationPublicationData = {
+ name: 'analytics',
+ config: {
+ type: 'tables',
+ tables: [],
+ operations: ['insert'],
+ publish_via_partition_root: false,
+ },
+ tables: [],
+}
+const defaultValues: DestinationPanelSchemaType = {
+ name: 'Analytics',
+ publicationName: 'analytics',
+ tableSyncCopyMode: 'include_all_tables',
+ tableSyncCopyTableIds: [],
+ maxFillMs: 500,
+ maxTableSyncWorkers: 4,
+ maxCopyConnectionsPerTable: 1,
+ maxStalenessMins: 0,
+ projectId: 'example-project',
+ datasetId: 'analytics',
+ serviceAccountKey: '',
+ connectionPoolSize: 5,
+}
+const key = replicationKeys.costEstimate('default', 1, 'analytics')
+const sources: components['schemas']['SourcesResponse_Output'] = {
+ sources: [
+ {
+ id: 1,
+ name: 'default',
+ tenant_id: 'tenant',
+ config: { host: 'localhost', port: 5432, name: 'postgres', username: 'postgres' },
+ },
+ ],
+}
+
+const ReviewCosts = () => {
+ const { control } = useForm<DestinationPanelSchemaType>({ defaultValues })
+ const costs = usePipelineReviewCosts({ control, publication })
+ return (
+ <>
+ <PipelineReviewCosts control={control} publication={publication} />
+ <button tabIndex={0} disabled={costs.isInitialCostEstimateLoading}>
+ Start pipeline
+ </button>
+ </>
+ )
+}
+
+beforeEach(() => {
+ addAPIMock({
+ method: 'get',
+ path: '/platform/replication/:ref/sources',
+ response: () => HttpResponse.json<components['schemas']['SourcesResponse_Output']>(sources),
+ })
+ addAPIMock({
+ method: 'get',
+ path: '/platform/replication/:ref/sources/:source_id/publications/:publication_name/cost-estimate',
+ response: () =>
+ HttpResponse.json<APIErrorBody>({ message: 'Estimate unavailable' }, { status: 503 }),
+ })
+})
+
+test('permits starting and shows the unavailable note after the first failure while retrying', async () => {
+ const queryClient = new QueryClient({
+ defaultOptions: { queries: { retry: 1, retryDelay: 60_000 } },
+ })
+ queryClient.setQueryData(replicationKeys.sources('default'), sources)
+ const { unmount } = customRender(<ReviewCosts />, { queryClient })
+ const status = screen.getByRole('status')
+ expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeDisabled()
+ expect(status).toBeEmptyDOMElement()
+
+ await waitFor(() => expect(status).toHaveTextContent('A cost estimate is unavailable.'))
+
+ expect(screen.getByRole('status')).toBe(status)
+ expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
+ expect(queryClient.getQueryState(key)).toMatchObject({
+ status: 'pending',
+ fetchStatus: 'fetching',
+ fetchFailureCount: 1,
+ })
+ unmount()
+ queryClient.clear()
+})
+
+test('keeps cached costs visible after a background refresh fails', async () => {
+ const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
+ queryClient.setQueryData<ReplicationCostEstimateData>(key, {
+ 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: [] },
+ })
+ queryClient.setQueryData(replicationKeys.sources('default'), sources)
+ const { unmount } = customRender(<ReviewCosts />, { queryClient })
+ await waitFor(() => expect(queryClient.getQueryState(key)?.status).toBe('error'))
+
+ expect(screen.getByText('$0.05/hour')).toBeInTheDocument()
+ expect(screen.getByText('$3.00/GB')).toBeInTheDocument()
+ expect(screen.getByRole('status')).toBeEmptyDOMElement()
+ expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
+ unmount()
+ queryClient.clear()
+})
+
+test('keeps starting available while refetching an estimate that previously failed', async () => {
+ const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
+ queryClient.setQueryData(replicationKeys.sources('default'), sources)
+ const { unmount } = customRender(<ReviewCosts />, { queryClient })
+
+ await waitFor(() => expect(queryClient.getQueryState(key)?.status).toBe('error'))
+ expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
+
+ let finishRefetch = () => {}
+ const refetchResponse = new Promise<void>((resolve) => {
+ finishRefetch = resolve
+ })
+ let markRefetchStarted = () => {}
+ const refetchStarted = new Promise<void>((resolve) => {
+ markRefetchStarted = resolve
+ })
+ addAPIMock({
+ method: 'get',
+ path: '/platform/replication/:ref/sources/:source_id/publications/:publication_name/cost-estimate',
+ response: async () => {
+ markRefetchStarted()
+ await refetchResponse
+ return HttpResponse.json<APIErrorBody>({ message: 'Estimate unavailable' }, { status: 503 })
+ },
+ })
+
+ try {
+ await act(async () => {
+ void queryClient.refetchQueries({ queryKey: key })
+ await refetchStarted
+ await new Promise((resolve) => setTimeout(resolve, 0))
+ })
+
+ expect(queryClient.getQueryState(key)).toMatchObject({
+ status: 'pending',
+ fetchStatus: 'fetching',
+ fetchFailureCount: 0,
+ errorUpdateCount: 1,
+ })
+ expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
+ expect(screen.getByRole('status')).toHaveTextContent('A cost estimate is unavailable.')
+ } finally {
+ finishRefetch()
+ unmount()
+ queryClient.clear()
+ }
+})
diff --git a/apps/studio/components/interfaces/Database/Replication/CreatePipeline/usePipelineReviewCosts.ts b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/usePipelineReviewCosts.ts
new file mode 100644
index 00000000000..8f5b481aa01
--- /dev/null
+++ b/apps/studio/components/interfaces/Database/Replication/CreatePipeline/usePipelineReviewCosts.ts
@@ -0,0 +1,74 @@
+import { useParams } from 'common'
+import { useWatch, type Control } from 'react-hook-form'
+
+import type { DestinationPanelSchemaType } from '../DestinationPanel/DestinationForm/DestinationForm.schema'
+import { buildTableSyncCopyConfig } from '../DestinationPanel/DestinationForm/DestinationForm.utils'
+import {
+ useReplicationCostEstimateQuery,
+ type ReplicationCostEstimateData,
+} from '@/data/replication/cost-estimate-query'
+import type { ReplicationPublicationData } from '@/data/replication/publication-query'
+import { useReplicationSourceId } from '@/data/replication/sources-query'
+import type { TableSyncCopyConfig } from '@/data/replication/types'
+
+type PipelineReviewCostsState = { isInitialCostEstimateLoading: boolean } & (
+ | { status: 'missing-publication' }
+ | { status: 'missing-selection' }
+ | {
+ status: 'estimate'
+ estimate?: ReplicationCostEstimateData
+ publicationTables: ReplicationPublicationData['tables']
+ tableSyncCopy: TableSyncCopyConfig
+ }
+)
+
+const getTableSyncCopy = (
+ params: Parameters<typeof buildTableSyncCopyConfig>[0]
+): TableSyncCopyConfig | undefined => {
+ try {
+ return buildTableSyncCopyConfig(params)
+ } catch {
+ // An incomplete initial sync selection has no estimate to request.
+ return undefined
+ }
+}
+
+export const usePipelineReviewCosts = ({
+ control,
+ publication,
+ enabled = true,
+}: {
+ control: Control<DestinationPanelSchemaType>
+ publication?: ReplicationPublicationData
+ enabled?: boolean
+}): PipelineReviewCostsState => {
+ const { ref: projectRef } = useParams()
+ const sourceId = useReplicationSourceId({ projectRef })
+ const [publicationName, mode, ids] = useWatch({
+ control,
+ name: ['publicationName', 'tableSyncCopyMode', 'tableSyncCopyTableIds'],
+ })
+ const hasPublication = !!publicationName && publication?.name === publicationName
+
+ const tableSyncCopy = getTableSyncCopy({ mode, selectedTableIds: ids })
+
+ const isEstimateEnabled = enabled && hasPublication && tableSyncCopy !== undefined
+ const { data, isLoading, failureCount, errorUpdateCount } = useReplicationCostEstimateQuery(
+ { projectRef, sourceId, publicationName },
+ { enabled: isEstimateEnabled }
+ )
+ const isInitialCostEstimateLoading =
+ isEstimateEnabled && isLoading && failureCount === 0 && errorUpdateCount === 0
+
+ if (!hasPublication) return { status: 'missing-publication', isInitialCostEstimateLoading: false }
+ if (tableSyncCopy === undefined)
+ return { status: 'missing-selection', isInitialCostEstimateLoading: false }
+
+ return {
+ status: 'estimate',
+ isInitialCostEstimateLoading,
+ estimate: data,
+ publicationTables: publication.tables,
+ tableSyncCopy,
+ }
+}