From 492e2f49c76ec9fe936a6b7ee1ebd63d760a56f5 Mon Sep 17 00:00:00 2001 From: Hydra Date: Thu, 12 Nov 2026 08:10:18 +0200 Subject: [PATCH] fix(migration): improve platform import setup and cancellation --- .../modules/migration/migration.controller.ts | 5 +- .../migration/platform-import-cancel.test.ts | 170 ++++++++++++++ .../migration/cloud-migration-http.test.ts | 15 ++ .../components/credentials/CredentialForm.tsx | 61 +++-- .../migration/PlatformImportProgress.tsx | 47 +++- .../migration/PlatformImportReview.tsx | 10 +- .../migration/PlatformImportServices.tsx | 11 +- .../migration/PlatformMigrationWizard.tsx | 210 +++++++++++------- .../migration/platform-import-flow.test.tsx | 84 ++++++- docs/dashboard-design.md | 15 +- packages/contracts/src/credentials.ts | 1 + .../contracts/src/migration-operations.ts | 2 + packages/contracts/src/migration-types.ts | 2 + packages/core/src/credentials.ts | 6 + .../migration/migration.orchestrator.ts | 70 ++++-- 15 files changed, 569 insertions(+), 140 deletions(-) create mode 100644 apps/api/src/modules/migration/platform-import-cancel.test.ts diff --git a/apps/api/src/modules/migration/migration.controller.ts b/apps/api/src/modules/migration/migration.controller.ts index 3ed413f27..9323735ff 100644 --- a/apps/api/src/modules/migration/migration.controller.ts +++ b/apps/api/src/modules/migration/migration.controller.ts @@ -789,8 +789,9 @@ export async function getMigration(c: Context) { } /** Mask serviceEnv inside a migration run's inputSnapshot (see #336). */ -function maskMigrationRunEnv(run: T): Omit { - const { recovery: _recovery, ...publicRun } = run; +function maskMigrationRunEnv(run: T): Omit & { cancelRequested: boolean } { + const { recovery, ...rest } = run; + const publicRun = { ...rest, cancelRequested: (recovery as { cancelRequested?: boolean } | undefined)?.cancelRequested === true }; const snap = run.inputSnapshot as { serviceEnv?: Record> } | null; if (!snap?.serviceEnv) { const imported = ( diff --git a/apps/api/src/modules/migration/platform-import-cancel.test.ts b/apps/api/src/modules/migration/platform-import-cancel.test.ts new file mode 100644 index 000000000..6d1b23878 --- /dev/null +++ b/apps/api/src/modules/migration/platform-import-cancel.test.ts @@ -0,0 +1,170 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { repos } from "@repo/db"; +import type { ExecutionContext } from "@repo/platform"; +import type { PrivateImportPlan } from "@repo/platform/engine/modules/migration/platform-import-plan"; + +const h = vi.hoisted(() => ({ execute: vi.fn(), cleanup: vi.fn(), revoke: vi.fn() })); +vi.mock("@repo/platform/engine/modules/migration/platform-import-worker", () => ({ + executePlatformImport: h.execute, + pauseFailedPlatformImport: h.cleanup, +})); +vi.mock("@repo/platform/engine/modules/migration/providers/railway-data", () => ({ + revokeRailwayImportKey: h.revoke, +})); +vi.mock("@repo/platform/engine/lib/provision-lock", async () => { + const { withKeyedMutex, tryWithKeyedMutex } = await import("@repo/core"); + return { + createProvisionLock: (key: string) => ({ + run: (work: () => Promise) => withKeyedMutex(key, work), + }), + tryWithProvisionLock: (key: string, work: () => Promise) => + tryWithKeyedMutex(key, work), + }; +}); +import { migrationOrchestrator } from "@repo/platform/engine/modules/migration/migration.orchestrator"; + +type Run = NonNullable>>; +const engine = migrationOrchestrator as unknown as { + runWorker(id: string, work: () => Promise): Promise; + runPlatformImport(ctx: ExecutionContext, id: string, plan: PrivateImportPlan): Promise; +}; +const ctx = { organizationId: "org", userId: "user" } as ExecutionContext; +let run: Run; +let signal: AbortSignal; +function deferred() { + let resolve!: () => void; + const promise = new Promise((done) => { + resolve = done; + }); + return { promise, resolve }; +} +beforeEach(() => { + vi.useFakeTimers(); + vi.resetAllMocks(); + run = { + id: "import", + organizationId: "org", + mode: "provider_import", + status: "moving_data", + sourceServerId: null, + targetServerId: null, + deploymentId: null, + executionStartedAt: new Date(), + executionFinishedAt: null, + recovery: {}, + } as unknown as Run; + vi.spyOn(repos.dockerMigrationRun, "findById").mockImplementation(async () => run); + vi.spyOn(repos.dockerMigrationRun, "requestCancel").mockImplementation(async () => { + run.recovery.cancelRequested = true; + return true; + }); + vi.spyOn(repos.dockerMigrationRun, "transition").mockImplementation( + async (_id, status, patch) => { + run = { ...run, status, ...patch } as Run; + }, + ); + vi.spyOn(repos.dockerMigrationRun, "updateLogs").mockResolvedValue(); + vi.spyOn(repos.dockerMigrationRun, "acknowledgeExecutionFinished").mockImplementation( + async () => { + run.executionFinishedAt = new Date(); + }, + ); + h.execute.mockImplementation(async (_ctx, _id, _plan, lifecycle) => { + signal = lifecycle.signal; + await new Promise((_resolve, reject) => { + signal.addEventListener("abort", () => reject(signal.reason), { once: true }); + }); + }); + h.cleanup.mockResolvedValue(undefined); + h.revoke.mockResolvedValue(undefined); +}); +afterEach(() => { + vi.restoreAllMocks(); + vi.useRealTimers(); +}); + +describe("stopping provider imports", () => { + it("interrupts deployment polling immediately rather than waiting for its interval", async () => { + vi.spyOn(repos.deployment, "findById").mockResolvedValue({ + id: "deployment", + status: "building", + } as never); + h.execute.mockImplementation(async (_ctx, _id, _plan, lifecycle) => { + await lifecycle.wait("deployment"); + }); + const worker = engine.runWorker(run.id, () => + engine.runPlatformImport(ctx, run.id, {} as PrivateImportPlan), + ); + await vi.advanceTimersByTimeAsync(0); + expect(repos.deployment.findById).toHaveBeenCalledWith("deployment"); + try { + await migrationOrchestrator.cancel(run.id, "org"); + await vi.advanceTimersByTimeAsync(0); + expect(h.cleanup).toHaveBeenCalledWith(run.id); + } finally { + await vi.advanceTimersByTimeAsync(5000); + await worker; + } + expect(run.status).toBe("rolled_back"); + }); + + it("recovers a recently abandoned worker immediately under execution ownership", async () => { + run.lastEventAt = new Date(); + expect(await migrationOrchestrator.cancel(run.id, "org")).toEqual({ ok: true }); + await vi.advanceTimersByTimeAsync(0); + expect(h.execute).not.toHaveBeenCalled(); + expect(h.cleanup).toHaveBeenCalledWith(run.id); + expect(run.status).toBe("rolled_back"); + expect(run.executionFinishedAt).toBeInstanceOf(Date); + await expect( + engine.runWorker(run.id, async () => { + throw new Error("late worker ran"); + }), + ).rejects.toThrow("already finished or was recovered"); + }); + + it.each(["same replica", "another replica"])( + "aborts on %s and waits for recovery before reporting stopped", + async (replica) => { + const cleanup = deferred(); + h.cleanup.mockReturnValue(cleanup.promise); + const worker = engine.runWorker(run.id, () => + engine.runPlatformImport(ctx, run.id, {} as PrivateImportPlan), + ); + await vi.advanceTimersByTimeAsync(0); + expect(signal.aborted).toBe(false); + const canceller = + replica === "same replica" + ? migrationOrchestrator + : new (migrationOrchestrator.constructor as new () => typeof migrationOrchestrator)(); + try { + expect(await canceller.cancel(run.id, "org")).toEqual({ ok: true }); + await vi.advanceTimersByTimeAsync(replica === "same replica" ? 0 : 1000); + expect(signal.aborted).toBe(true); + expect(h.cleanup).toHaveBeenCalledWith(run.id); + expect(run.recovery.cancelRequested).toBe(true); + expect(run.status).toBe("moving_data"); + expect(run.executionFinishedAt).toBeNull(); + } finally { + cleanup.resolve(); + await worker; + } + expect(run.status).toBe("rolled_back"); + expect(run.errorMessage).toContain("Import stopped"); + expect(run.executionFinishedAt).toBeInstanceOf(Date); + }, + ); + + it("retains the pending stop when recovery cannot finish", async () => { + h.cleanup.mockRejectedValue(new Error("source unavailable")); + const worker = engine.runWorker(run.id, () => + engine.runPlatformImport(ctx, run.id, {} as PrivateImportPlan), + ); + const completion = expect(worker).rejects.toThrow("source unavailable"); + await vi.advanceTimersByTimeAsync(0); + await migrationOrchestrator.cancel(run.id, "org"); + await completion; + expect(run.recovery.cancelRequested).toBe(true); + expect(run.status).not.toBe("rolled_back"); + }); +}); diff --git a/apps/api/test/modules/migration/cloud-migration-http.test.ts b/apps/api/test/modules/migration/cloud-migration-http.test.ts index 078fbae39..28065582c 100644 --- a/apps/api/test/modules/migration/cloud-migration-http.test.ts +++ b/apps/api/test/modules/migration/cloud-migration-http.test.ts @@ -64,6 +64,21 @@ async function runFixture() { return { owner, auth, source, target, project, run: run! }; } +it("returns the durable stop request without exposing private recovery data", async () => { + const { auth, run } = await runFixture(); + await repos.dockerMigrationRun.updateRecovery(run.id, { transferRunTag: "" }); + await repos.dockerMigrationRun.transition(run.id, "moving_data"); + const stopped = await request("POST", `/migrations/${run.id}/cancel`, auth, {}); + expect(stopped.status).toBe(200); + const detail = await request("GET", `/migrations/${run.id}`, auth); + expect(detail.status).toBe(200); + expect(detail.body.run.cancelRequested).toBe(true); + expect(detail.body.run.status).toBe("moving_data"); + expect(detail.body.run).not.toHaveProperty("recovery"); + expect(JSON.stringify(detail.body)).not.toContain("fixture-runtime-secret"); + expect(network.connect).not.toHaveBeenCalled(); +}); + beforeEach(() => { vi.clearAllMocks(); network.exec.mockResolvedValue(""); diff --git a/apps/dashboard/src/components/credentials/CredentialForm.tsx b/apps/dashboard/src/components/credentials/CredentialForm.tsx index dbc78e17f..2a3e5eb06 100644 --- a/apps/dashboard/src/components/credentials/CredentialForm.tsx +++ b/apps/dashboard/src/components/credentials/CredentialForm.tsx @@ -1,6 +1,7 @@ "use client"; import { useEffect, useId, useRef, useState } from "react"; +import { createPortal } from "react-dom"; import { normalizeCredentialSelector, type CredentialProvider } from "@repo/core"; import { Icon } from "@repo/ui/icons"; import { credentialsApi, type Credential } from "@/lib/api/credentials"; @@ -22,6 +23,7 @@ export function CredentialForm({ onSaved, onBusyChange, submitLabel, + actionsContainer, }: { provider: CredentialProvider; existing?: Credential; @@ -31,6 +33,8 @@ export function CredentialForm({ onBusyChange?: (busy: boolean) => void; /** Contextual label; saving always uses the same provider verification. */ submitLabel?: string; + /** Optional external footer; native form ownership keeps submission/validation shared. */ + actionsContainer?: HTMLElement | null; }) { const { t } = useI18n(); const copy = t.settings.credentials; @@ -199,8 +203,44 @@ export function CredentialForm({ } } + const cancelButton = ( + + ); + const submitButton = ( + + ); + const actions = ( +
+ {actionsContainer === undefined ? ( + <> + {cancelButton} + {submitButton} + + ) : ( + <> + {submitButton} + {cancelButton} + + )} +
+ ); + return (
{ event.preventDefault(); @@ -278,6 +318,7 @@ export function CredentialForm({ const fieldLoc = pLoc?.fields?.[field.key]; const fieldLabel = fieldLoc?.label ?? field.label; const fieldHelp = fieldLoc?.help ?? field.help; + const inputType = field.inputType ?? (field.type === "secret" ? "password" : "text"); return (