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

143 lines
4.7 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 { Button } from 'ui'
import { describe, expect, test, vi } from 'vitest'
import { PipelineStatePill } from './PipelineStatePill'
import { UpdateVersionModal } from './UpdateVersionModal'
import { replicationKeys } from '@/data/replication/keys'
import {
useReplicationPipelineStatusQuery,
type ReplicationPipelineStatusResponse,
} from '@/data/replication/pipeline-status-query'
import type { Pipeline } from '@/data/replication/pipelines-query'
import {
PipelineRequestStatusProvider,
usePipelineRequestStatus,
} from '@/state/replication-pipeline-request-status'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
const pipeline: Pipeline = {
id: 9,
tenant_id: 'test',
source_id: 1,
source_name: 'main',
destination_id: 1,
destination_name: 'Analytics',
replicator_id: 1,
config: { publication_name: 'analytics' },
}
const StatusView = () => {
const { data, error, isPending, isError, isSuccess } = useReplicationPipelineStatusQuery({
projectRef: 'default',
pipelineId: 9,
})
const { getRequestStatus, isRequestPending } = usePipelineRequestStatus()
return (
<>
<PipelineStatePill
pipelineStatus={data?.status}
error={error}
isLoading={isPending}
isError={isError}
isSuccess={isSuccess}
requestStatus={getRequestStatus(9)}
/>
<Button disabled={isRequestPending(9)}>Another action</Button>
</>
)
}
describe('pipeline version updates', () => {
test.each([
{
status: 'started',
initialLabel: 'Running',
confirmLabel: 'Update and restart',
pendingLabel: 'Stopping',
},
{
status: 'stopped',
initialLabel: 'Stopped',
confirmLabel: 'Update version',
pendingLabel: 'Stopped',
},
{
status: 'unknown',
initialLabel: 'Unknown',
confirmLabel: 'Update version',
pendingLabel: 'Unknown',
},
] as const)(
'honors the backend lifecycle for $status',
async ({ status, initialLabel, confirmLabel, pendingLabel }) => {
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
const onClose = vi.fn()
const updates: unknown[] = []
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: status },
}),
})
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/version',
response: () =>
HttpResponse.json<components['schemas']['PipelineVersionResponse_Output']>({
pipeline_id: 9,
version: { id: 1, name: 'v1' },
new_version: { id: 2, name: 'v2' },
}),
})
addAPIMock({
method: 'post',
path: '/platform/replication/:ref/pipelines/:pipeline_id/version',
response: async ({ request }) => {
updates.push(await request.json())
await response
return HttpResponse.json<Record<string, never>>({})
},
})
customRender(
<PipelineRequestStatusProvider>
<StatusView />
<UpdateVersionModal visible pipeline={pipeline} onClose={onClose} />
</PipelineRequestStatusProvider>,
{ queryClient }
)
await screen.findByText(initialLabel)
await screen.findByText('v2')
fireEvent.click(screen.getByRole('button', { name: confirmLabel }))
expect(screen.getByText(pendingLabel)).toBeInTheDocument()
expect(screen.getByText('Another action').closest('button')).toBeDisabled()
await act(async () => {
await queryClient.invalidateQueries(
{ queryKey: replicationKeys.pipelinesStatus('default', 9) },
{ cancelRefetch: false }
)
})
expect(screen.getByText(pendingLabel)).toBeInTheDocument()
expect(screen.getByText('Another action').closest('button')).toBeDisabled()
await act(async () => {
complete()
})
await waitFor(() => expect(onClose).toHaveBeenCalledOnce())
expect(screen.getByText('Another action').closest('button')).toBeEnabled()
// There are deliberately no start/stop/restart handlers: the update endpoint owns this.
expect(updates).toEqual([{ version_id: 2 }])
}
)
})