Files
Riccardo Busetti edec85d1ca fix(pipelines): Make pipeline actions and status updates reliable (#50085)
## Summary

Make pipeline actions and status feedback reliable while requests are
running or fail. Let the backend coordinate table resets and restarts,
keep stopped pipelines stopped after resets or settings changes, and
refresh the UI from confirmed backend state.

## Pipeline actions and recovery

- Reset one table, all errored tables, or all tables through the
rollback endpoint without separate frontend stop/start requests. Explain
which destination data is deleted, which rows are copied again, initial
sync charges, and the skip-initial-sync setting.
- Keep pending feedback until the action and a fresh status read finish,
including across navigation and polling errors. Prevent overlapping
actions and disable start/stop controls when status is unavailable or
transitioning.
- Close the creation form once the pipeline is created. If its initial
start fails, users can retry Start on the existing pipeline without
creating a duplicate.
- Wait for confirmed shutdown before deletion; a shutdown error or
timeout leaves deletion retryable. Keep failed version updates open and
avoid reporting success.
- Clarify recovery guidance and pending labels, suppress duplicate error
toasts, and hide stale table errors during transitions.

## Status updates and shared UI

- Poll pipeline status and table metrics one second after each response,
share in-flight reads, pause dashboard polling in background tabs, and
respect rate-limit backoff. The shutdown waiter continues in the
background.
- Refresh metadata after mutations even when an older read is in flight,
while preserving shared polling requests. Refresh affected data after
failures that may follow a committed reset or settings change.
- Move pending request state into the shared, project-keyed
`DatabaseLayout` so the list, detail page, and diagram stay consistent.
The surrounding database-page changes update named imports in both
Next.js and TanStack routes.
- Simplify action, status, and form rendering; announce status changes
to assistive technology; and sort table statuses without mutating cached
data.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Danny White <3104761+dnywh@users.noreply.github.com>
2026-09-18 11:32:48 +08:00

348 lines
11 KiB
TypeScript

import { QueryClient } from '@tanstack/react-query'
import { act, fireEvent, screen, waitFor } from '@testing-library/react'
import type { components } from 'api-types'
import { HttpResponse } from 'msw'
import { describe, expect, it, vi } from 'vitest'
import { BatchRestartDialog } from './BatchRestartDialog'
import { getStatusName } from './Pipeline.utils'
import { PipelineStatePill } from './PipelineStatePill'
import { RestartTableDialog } from './RestartTableDialog'
import { replicationKeys } from '@/data/replication/keys'
import type { ReplicationPipelineTableStatus } from '@/data/replication/pipeline-replication-status-query'
import {
useReplicationPipelineStatusQuery,
type ReplicationPipelineStatusResponse,
} from '@/data/replication/pipeline-status-query'
import {
PipelineRequestStatusProvider,
usePipelineRequestStatus,
} from '@/state/replication-pipeline-request-status'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
vi.mock('common', async (importOriginal) => ({
...(await importOriginal<typeof import('common')>()),
useParams: () => ({ ref: 'default', pipelineId: '9' }),
}))
vi.mock('./RestartCostEstimate', () => ({
RestartCostEstimate: ({ tables }: { tables: { schema: string; name: string }[] }) => (
<div data-testid="copy-targets">
{tables.map(({ schema, name }) => `${schema}.${name}`).join(',')}
</div>
),
}))
const table = (
id: number,
state: ReplicationPipelineTableStatus['state']
): ReplicationPipelineTableStatus => ({
id,
schema: 'public',
name: `table_${id}`,
table_id: id,
table_name: `public.table_${id}`,
state,
})
describe('BatchRestartDialog', () => {
it('describes every table reset by the all-errored backend target', async () => {
const tables = [
table(1, { name: 'error', reason: 'manual', retry_policy: { policy: 'manual_retry' } }),
table(2, { name: 'error', reason: 'terminal', retry_policy: { policy: 'no_retry' } }),
table(3, {
name: 'error',
reason: 'timed',
retry_policy: { policy: 'timed_retry', next_retry: '2026-07-21T12:00:00Z' },
}),
table(4, { name: 'following_wal' }),
]
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/status',
response: () =>
HttpResponse.json<ReplicationPipelineStatusResponse>({
pipeline_id: 9,
status: { name: 'stopped' },
}),
})
const requests: unknown[] = []
const onOpenChange = vi.fn()
addAPIMock({
method: 'post',
path: '/platform/replication/:ref/pipelines/:pipeline_id/rollback-tables',
response: async ({ request }) => {
requests.push(await request.json())
return HttpResponse.json<components['schemas']['RollbackTablesResponse_Output']>({
pipeline_id: 9,
tables: [1, 2, 3].map((table_id) => ({ table_id, new_state: { name: 'queued' } })),
})
},
})
customRender(
<PipelineRequestStatusProvider>
<BatchRestartDialog
open
onOpenChange={onOpenChange}
mode="errored"
tables={tables}
tableSyncCopy={{ type: 'include_tables', table_ids: [1, 2] }}
/>
</PipelineRequestStatusProvider>
)
expect(screen.getByText(/This resets 3 failed tables/)).toBeInTheDocument()
expect(
screen.getByText(
/Existing rows sync again for 2 of 3 tables, while the remaining table skips initial sync/
)
).toBeInTheDocument()
expect(screen.getByTestId('copy-targets')).toHaveTextContent('public.table_1,public.table_2')
await act(async () => {
fireEvent.click(screen.getByRole('button', { name: 'Reset failed tables' }))
})
await waitFor(() => expect(onOpenChange).toHaveBeenCalledWith(false))
expect(requests).toEqual([{ target: { type: 'all_errored_tables' } }])
})
it('uses singular copy when resetting the only table', () => {
customRender(
<PipelineRequestStatusProvider>
<BatchRestartDialog
open
onOpenChange={vi.fn()}
mode="all"
tables={[table(1, { name: 'following_wal' })]}
tableSyncCopy={{ type: 'include_tables', table_ids: [1] }}
/>
</PipelineRequestStatusProvider>
)
expect(
screen.getByText(
'This resets the table, deletes its destination data, and syncs existing rows again.'
)
).toBeInTheDocument()
})
it.each([
{
target: 'all',
initialStatus: 'started',
optimisticLabel: 'Stopping',
nextStatus: 'starting',
nextLabel: 'Starting',
},
{
target: 'single',
initialStatus: 'started',
optimisticLabel: 'Stopping',
nextStatus: 'starting',
nextLabel: 'Starting',
},
{
target: 'all',
initialStatus: 'stopped',
optimisticLabel: 'Stopped',
nextStatus: 'stopped',
nextLabel: 'Stopped',
},
{
target: 'single',
initialStatus: 'stopped',
optimisticLabel: 'Stopped',
nextStatus: 'stopped',
nextLabel: 'Stopped',
},
] as const)(
'resets $target tables while honoring a $initialStatus pipeline',
async ({ target, initialStatus, optimisticLabel, nextStatus, nextLabel }) => {
const onOpenChange = vi.fn()
const onResetStart = vi.fn()
const onResetComplete = vi.fn()
const requests: unknown[] = []
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
let backendStatus: ReplicationPipelineStatusResponse['status']['name'] = initialStatus
let complete = () => {}
const response = new Promise<void>((resolve) => {
complete = resolve
})
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/status',
response: () =>
HttpResponse.json<ReplicationPipelineStatusResponse>({
pipeline_id: 9,
status: { name: backendStatus },
}),
})
addAPIMock({
method: 'post',
path: '/platform/replication/:ref/pipelines/:pipeline_id/rollback-tables',
response: async ({ request }) => {
requests.push(await request.json())
await response
return HttpResponse.json<components['schemas']['RollbackTablesResponse_Output']>({
pipeline_id: 9,
tables: [{ table_id: 1, new_state: { name: 'queued' } }],
})
},
})
customRender(
<PipelineRequestStatusProvider>
<RestartDialogWithStatus
target={target}
onOpenChange={onOpenChange}
onResetStart={onResetStart}
onResetComplete={onResetComplete}
/>
</PipelineRequestStatusProvider>,
{ queryClient }
)
await screen.findByText(initialStatus === 'started' ? 'Running' : 'Stopped')
expect(
screen.getByText(
initialStatus === 'started'
? 'This resets the table, deletes its destination data, and syncs existing rows again. The pipeline restarts automatically to apply the reset.'
: 'This resets the table, deletes its destination data, and syncs existing rows again.'
)
).toBeInTheDocument()
fireEvent.click(
screen.getByRole('button', {
name: target === 'all' ? 'Reset all tables' : 'Reset table',
})
)
expect(onResetStart).toHaveBeenCalledWith(target === 'all' ? [1] : 1)
expect(screen.getByText(optimisticLabel)).toBeInTheDocument()
backendStatus = nextStatus
await act(async () => {
await queryClient.invalidateQueries(
{ queryKey: replicationKeys.pipelinesStatus('default', 9) },
{ cancelRefetch: false }
)
})
expect(screen.getByText(optimisticLabel)).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Resetting…' })).toBeDisabled()
await act(async () => {
complete()
})
await waitFor(() => expect(onOpenChange).toHaveBeenCalledWith(false))
expect(onResetComplete).toHaveBeenCalledWith(target === 'all' ? [1] : 1)
await waitFor(() => expect(screen.getByText(nextLabel)).toBeInTheDocument())
expect(requests).toEqual([
{
target: target === 'all' ? { type: 'all_tables' } : { type: 'single_table', table_id: 1 },
},
])
}
)
it.each(['all', 'single'] as const)(
'keeps the $target reset dialog open after an error',
async (target) => {
const onOpenChange = vi.fn()
const onResetStart = vi.fn()
const onResetComplete = vi.fn()
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/status',
response: () =>
HttpResponse.json<ReplicationPipelineStatusResponse>({
pipeline_id: 9,
status: { name: 'started' },
}),
})
addAPIMock({
method: 'post',
path: '/platform/replication/:ref/pipelines/:pipeline_id/rollback-tables',
response: () =>
HttpResponse.json<APIErrorBody>({ message: 'Unable to reset tables' }, { status: 500 }),
})
customRender(
<PipelineRequestStatusProvider>
<RestartDialogWithStatus
target={target}
onOpenChange={onOpenChange}
onResetStart={onResetStart}
onResetComplete={onResetComplete}
/>
</PipelineRequestStatusProvider>
)
await screen.findByText('Running')
fireEvent.click(
screen.getByRole('button', {
name: target === 'all' ? 'Reset all tables' : 'Reset table',
})
)
await waitFor(() => {
expect(onResetComplete).toHaveBeenCalledWith(target === 'all' ? [1] : 1)
})
expect(onOpenChange).not.toHaveBeenCalled()
expect(
screen.getByRole('button', {
name: target === 'all' ? 'Reset all tables' : 'Reset table',
})
).toBeEnabled()
}
)
})
const RestartDialogWithStatus = ({
target,
onOpenChange,
onResetStart,
onResetComplete,
}: {
target: 'single' | 'all'
onOpenChange: (open: boolean) => void
onResetStart: (tableIds: number[] | number) => void
onResetComplete: (tableIds: number[] | number) => void
}) => {
const { data, error, isPending, isError, isSuccess } = useReplicationPipelineStatusQuery({
projectRef: 'default',
pipelineId: 9,
})
const { getRequestStatus } = usePipelineRequestStatus()
const pipelineStatusName = getStatusName(data?.status)
return (
<>
<PipelineStatePill
pipelineStatus={data?.status}
error={error}
isLoading={isPending}
isError={isError}
isSuccess={isSuccess}
requestStatus={getRequestStatus(9)}
/>
{target === 'all' ? (
<BatchRestartDialog
open
mode="all"
tables={[table(1, { name: 'following_wal' })]}
pipelineStatusName={pipelineStatusName}
onOpenChange={onOpenChange}
onResetStart={onResetStart}
onResetComplete={onResetComplete}
/>
) : (
<RestartTableDialog
open
table={table(1, { name: 'following_wal' })}
pipelineStatusName={pipelineStatusName}
onOpenChange={onOpenChange}
onResetStart={onResetStart}
onResetComplete={onResetComplete}
/>
)}
</>
)
}