From 5844461364dfa260374b2552a00c05b134fdcf3b Mon Sep 17 00:00:00 2001 From: Hydra Date: Thu, 12 Nov 2026 21:04:11 +0200 Subject: [PATCH 01/37] fix(migration): preserve project services and reuse domain routing --- .github/workflows/ci.yml | 1 + CHANGELOG.md | 6 + .../deployments/compose/build-cancel.test.ts | 6 +- .../deployments/compose/build.service.test.ts | 20 +- .../migration/platform-import-data.test.ts | 28 +- .../migration/platform-import-plan.test.ts | 52 +- .../migration/platform-import-worker.test.ts | 308 ++++++++++-- .../modules/migration/railway-wiring.test.ts | 24 +- .../test/e2e/platform-import-data.e2e.test.ts | 11 +- .../e2e/platform-import-project.e2e.test.ts | 446 ++++++++++++++++++ .../migration/platform-import-domains.test.ts | 185 ++++++++ .../[id]/components/BuildSettings.tsx | 21 +- .../components/environment-scope.test.tsx | 25 +- .../migration/PlatformImportDomains.tsx | 69 +++ .../migration/PlatformImportProgress.tsx | 88 ++-- .../migration/PlatformImportReview.tsx | 13 +- .../migration/PlatformImportServices.tsx | 36 +- .../migration/PlatformMigrationWizard.tsx | 8 + .../migration/platform-import-flow.test.tsx | 172 ++++++- .../src/i18n/locales/ar/migration.json | 14 + .../src/i18n/locales/de/migration.json | 14 + .../src/i18n/locales/en/migration.json | 14 + .../src/i18n/locales/es/migration.json | 14 + .../src/i18n/locales/fr/migration.json | 14 + .../src/i18n/locales/ja/migration.json | 14 + .../src/i18n/locales/pt/migration.json | 14 + .../src/i18n/locales/ru/migration.json | 14 + .../src/i18n/locales/tr/migration.json | 14 + .../src/i18n/locales/zh/migration.json | 14 + .../guides/migrate-from-railway-vercel.mdx | 52 +- apps/web/src/lib/migration-sources.ts | 6 +- docs/dashboard-design.md | 15 +- docs/platform-migrations.md | 61 ++- packages/contracts/src/platform-migration.ts | 1 + packages/db/src/schema/docker-migration.ts | 4 + .../deployments/compose/build.service.ts | 24 +- .../migration/migration.orchestrator.ts | 4 +- .../modules/migration/platform-import-data.ts | 31 +- .../modules/migration/platform-import-plan.ts | 33 +- .../migration/platform-import-project.ts | 125 +++++ .../migration/platform-import-worker.ts | 234 +++++---- .../migration/providers/railway-wiring.ts | 67 +-- 42 files changed, 2009 insertions(+), 307 deletions(-) create mode 100644 apps/api/test/e2e/platform-import-project.e2e.test.ts create mode 100644 apps/api/test/modules/migration/platform-import-domains.test.ts create mode 100644 apps/dashboard/src/components/migration/PlatformImportDomains.tsx create mode 100644 packages/platform/src/engine/modules/migration/platform-import-project.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eaa8f7546..6c3f05b42 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -150,6 +150,7 @@ jobs: command: >- RUN_DOCKER_E2E=1 bun run --cwd apps/api test:e2e test/e2e/platform-import-data.e2e.test.ts + test/e2e/platform-import-project.e2e.test.ts # Separate independent daemons keep real-Docker coverage within the job # budget. Tests remain serial inside each daemon to preserve isolation. - suite: Compose cutover and rollback diff --git a/CHANGELOG.md b/CHANGELOG.md index 850b8fe80..e5daf9cdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,6 +71,12 @@ website. destination review, progress reporting and cancellation. - Keep migration environment fields visible while entering missing secrets, with a plain section heading instead of a nested disclosure that collapses during editing. +- Preserve source project boundaries during platform imports: one project contains + its native services, private DNS, encrypted service environments and separate volumes. + Keep service environment values scoped correctly during source builds too. +- Carry imported custom domains and aliases through the existing migration routing + flow, pending normal verification. Show DNS cutover guidance and replacement + notices for Railway and Vercel URLs in review and saved migration progress. - Restore portable instance exports with their server credentials and workspace ownership. Normalize restored data for the destination schema and keep the instance import and server selection views consistent. diff --git a/apps/api/src/modules/deployments/compose/build-cancel.test.ts b/apps/api/src/modules/deployments/compose/build-cancel.test.ts index 86d366a08..fcb82ccb5 100644 --- a/apps/api/src/modules/deployments/compose/build-cancel.test.ts +++ b/apps/api/src/modules/deployments/compose/build-cancel.test.ts @@ -4,9 +4,11 @@ import { describe, expect, it, vi } from "vitest"; // Same stubs as build.service.test.ts: the DB row read and the SSE broadcast are // side effects this contract doesn't depend on (and importing the real @repo/db // would risk the PGlite single-instance lock). -const { listByProjectMock } = vi.hoisted(() => ({ listByProjectMock: vi.fn() })); +const { listByProjectMock, getEnvMapMock } = vi.hoisted(() => ({ + listByProjectMock: vi.fn(), getEnvMapMock: vi.fn(async () => ({})), +})); vi.mock("@repo/db", () => ({ - repos: { service: { listByProject: listByProjectMock } }, + repos: { project: { getEnvMap: getEnvMapMock }, service: { listByProject: listByProjectMock } }, })); vi.mock("@repo/platform/engine/modules/deployments/session-manager", () => ({ broadcastServiceStatus: vi.fn(), diff --git a/apps/api/src/modules/deployments/compose/build.service.test.ts b/apps/api/src/modules/deployments/compose/build.service.test.ts index f8e2770d9..9023d7848 100644 --- a/apps/api/src/modules/deployments/compose/build.service.test.ts +++ b/apps/api/src/modules/deployments/compose/build.service.test.ts @@ -1,4 +1,5 @@ import { existsSync, readdirSync } from "node:fs"; +import { encrypt } from "@repo/platform/engine/lib/encryption"; import { readFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { isAbsolute, join } from "node:path"; @@ -12,9 +13,11 @@ import { describe, expect, it, vi } from "vitest"; // status. Both are side effects the inline-build materialization contract does not // depend on — stub them so the test exercises ONLY the materialize/COPY path. // (Importing the real @repo/db would also risk the PGlite single-instance lock.) -const { listByProjectMock } = vi.hoisted(() => ({ listByProjectMock: vi.fn() })); +const { listByProjectMock, getEnvMapMock } = vi.hoisted(() => ({ + listByProjectMock: vi.fn(), getEnvMapMock: vi.fn(async () => ({})), +})); vi.mock("@repo/db", () => ({ - repos: { service: { listByProject: listByProjectMock, startServiceDeploymentTiming: vi.fn(async () => {}) } }, + repos: { project: { getEnvMap: getEnvMapMock }, service: { listByProject: listByProjectMock, startServiceDeploymentTiming: vi.fn(async () => {}) } }, })); vi.mock("@repo/platform/engine/modules/deployments/session-manager", () => ({ broadcastServiceStatus: vi.fn(), @@ -567,6 +570,19 @@ describe("buildComposeImages — inline catalog build materialization", () => { * These pin the producer side: which rows get a narrowed context, and which must not. */ describe("buildComposeImages — declared build context", () => { + it("uses each service's encrypted saved environment for its own build", async () => { + getEnvMapMock.mockResolvedValueOnce({ API_SECRET: encrypt("api-only"), PORT: encrypt("3000") }); + getEnvMapMock.mockResolvedValueOnce({ WORKER_SECRET: encrypt("worker-only"), PORT: encrypt("8000") }); + const { captured } = await run([ + repoService({ id: "api", name: "api", build: "." }), + repoService({ id: "worker", name: "worker", build: "." }), + ], undefined, undefined, { SHARED: "shared" }); + expect(captured.map(({ config }) => config.envVars)).toEqual([ + { SHARED: "shared", API_SECRET: "api-only", PORT: "3000" }, + { SHARED: "shared", WORKER_SECRET: "worker-only", PORT: "8000" }, + ]); + }); + it("#795 carries project env into the posted root-Dockerfile setup without duplicate service env", async () => { const projectEnv = { DATABASE_URI: "postgres://db/app", diff --git a/apps/api/src/modules/migration/platform-import-data.test.ts b/apps/api/src/modules/migration/platform-import-data.test.ts index c857a2a19..57c86d2f1 100644 --- a/apps/api/src/modules/migration/platform-import-data.test.ts +++ b/apps/api/src/modules/migration/platform-import-data.test.ts @@ -6,6 +6,8 @@ const h = vi.hoisted(() => ({ pinned: vi.fn(), source: vi.fn(), target: vi.fn(), + list: vi.fn(), + handle: vi.fn(), produce: vi.fn(), restore: vi.fn(), dispose: vi.fn(), @@ -28,7 +30,7 @@ vi.mock("@repo/db", () => ({ activeDeploymentId: "build", }), }, - service: { listByProject: async () => [] }, + service: { listByProject: h.list }, deployment: { findById: async () => ({ containerId: "target-container" }) }, }, })); @@ -52,7 +54,7 @@ vi.mock("@repo/platform/engine/lib/project-runtime-lock", () => ({ withProjectRuntimeLock: async (_id: string, work: () => Promise) => work(), })); vi.mock("@repo/platform/engine/modules/backups/service-handle", () => ({ - serviceHandleFor: vi.fn(), + serviceHandleFor: h.handle, })); vi.mock("@repo/platform/engine/modules/services/service-container", () => ({ containerIdForService: vi.fn(), @@ -107,6 +109,8 @@ let target: BackupExecutor; beforeEach(() => { vi.resetAllMocks(); h.workspaceId = undefined; + h.list.mockResolvedValue([{ id: "db", name: "db", projectId: "project" }]); + h.handle.mockResolvedValue(handle); h.activity.mockImplementation(async (_id: string, work: () => Promise) => work()); h.pinned.mockResolvedValue({ ip: "203.0.113.8" }); target = { @@ -121,12 +125,23 @@ beforeEach(() => { }); }); describe("provider data copy", () => { + it("rejects a missing native target service before opening a source or restoring data", async () => { + h.list.mockResolvedValue([{ id: "other-service", name: "api", projectId: "project" }]); + await expect(copyPlatformData({ + runId: "run", service, projectId: "project", serviceId: "db", deploymentId: "build", + signal: new AbortController().signal, log: vi.fn(), progress: vi.fn(), + })).rejects.toMatchObject({ code: "IMPORT_TARGET_SERVICE_MISSING" }); + expect(h.source).not.toHaveBeenCalled(); + expect(h.restore).not.toHaveBeenCalled(); + expect(h.handle).not.toHaveBeenCalled(); + }); it("leaves a destination changed by the operator untouched", async () => { await expect( copyPlatformData({ runId: "run", service, projectId: "project", + serviceId: "db", deploymentId: "replaced-build", signal: new AbortController().signal, log: vi.fn(), @@ -174,6 +189,10 @@ describe("provider data copy", () => { ).rejects.toMatchObject({ code: "IMPORT_DATABASE_EXTENSIONS_MISSING" }); }); it("streams every byte with backpressure, verifies after restore and closes both transports", async () => { + const targetRow = { id: "db", name: "database", projectId: "project" }; + h.list.mockResolvedValue([ + { id: "api", name: "api", projectId: "project" }, targetRow, + ]); const payload = Buffer.alloc(256 * 1024, 0x41); let producerCompleted = false; h.produce.mockImplementation(async function* () { @@ -197,12 +216,14 @@ describe("provider data copy", () => { runId: "run", service, projectId: "project", + serviceId: "db", deploymentId: "build", signal: new AbortController().signal, log: vi.fn(), progress, }); expect(restored).toEqual(payload); + expect(h.handle).toHaveBeenCalledWith(targetRow, expect.objectContaining({ projectSlug: "app" })); expect(producerCompleted).toBe(true); expect(h.dispose).toHaveBeenCalledOnce(); expect(h.source).not.toHaveBeenCalled(); @@ -228,6 +249,7 @@ describe("provider data copy", () => { runId: "run", service, projectId: "project", + serviceId: "db", deploymentId: "build", signal: new AbortController().signal, log: vi.fn(), @@ -245,6 +267,7 @@ describe("provider data copy", () => { runId: "run", service, projectId: "project", + serviceId: "db", deploymentId: "build", signal: new AbortController().signal, log: vi.fn(), @@ -269,6 +292,7 @@ it("holds the owning Cloud workspace activity lock for the whole transfer", asyn runId: "run", service, projectId: "project", + serviceId: "db", deploymentId: "build", signal, log: vi.fn(), diff --git a/apps/api/src/modules/migration/platform-import-plan.test.ts b/apps/api/src/modules/migration/platform-import-plan.test.ts index 910545e3f..38db65086 100644 --- a/apps/api/src/modules/migration/platform-import-plan.test.ts +++ b/apps/api/src/modules/migration/platform-import-plan.test.ts @@ -134,7 +134,7 @@ async function review( } describe("reviewed platform imports", () => { - it("keeps secrets server-side and replaces provider database addresses with shared project bindings", async () => { + it("keeps secrets server-side and replaces provider database addresses with native service aliases", async () => { const plan = await review(); expect(plan.ready).toBe(true); expect(plan.requiresWritePause).toBe(true); @@ -144,10 +144,14 @@ describe("reviewed platform imports", () => { { sourceId: "db", envKey: "DATABASE_URL", - valueTemplate: "postgres://app:source-secret@${host}/app", }, ]); - expect(privatePlan.services[1].environment).not.toHaveProperty("DATABASE_URL"); + expect(privatePlan.services[1].environment.DATABASE_URL).toBe( + "postgres://app:source-secret@database/app", + ); + expect(privatePlan.project.name).toBe("Project"); + expect(privatePlan.project.services?.map((row) => row.name)).toEqual(["database", "website"]); + expect(JSON.stringify(privatePlan.project)).not.toContain("source-secret"); expect(privatePlan.services[0].environment.PGHOST).toBe( privatePlan.services[0].create.internalAlias, ); @@ -175,6 +179,33 @@ describe("reviewed platform imports", () => { code: "IMPORT_SOURCE_CHANGED", }); }); + it("names storage per service inside one destination project", async () => { + inventory.services[1].volumes = [{ id: "files", mountPath: "/data" }]; + const plan = await loadReviewedImport(ctx, (await review()).planId); + expect(plan.project.services?.flatMap((service) => service.volumes ?? [])).toEqual([ + "database-data-0:/var/lib/postgresql/data", + "website-data-0:/data", + ]); + expect(plan.project.services?.[1].dependsOn).toEqual(["database"]); + }); + it("blocks incompatible Git sources instead of splitting the source project", async () => { + inventory.services = ["one", "two"].map((id) => + service({ + id, + name: id, + image: undefined, + repository: `owner/${id}`, + commit: "a".repeat(40), + }), + ); + const result = await review( + inventory.services.map((service) => ({ id: service.id, name: service.name, data: "empty" })), + ); + expect(result.ready).toBe(false); + expect(result.services.flatMap((service) => service.blockers).join(" ")).toContain( + "one Git source", + ); + }); it("ignores pagination order and changing volume size, but never ignores changed configuration", () => { const next = structuredClone(inventory); next.services.reverse(); @@ -202,16 +233,22 @@ describe("reviewed platform imports", () => { const reviewed = await review(); expect(reviewed.ready).toBe(true); expect( - (await loadReviewedImport(ctx, reviewed.planId)).services[1].bindings[0].valueTemplate, - ).toBe("postgres://app:source-secret@${host}/app"); + (await loadReviewedImport(ctx, reviewed.planId)).services[1].environment.DATABASE_URL, + ).toBe("postgres://app:source-secret@database/app"); }); - it("rejects conflicting project names and enforces destination access before planning", async () => { + it("rejects conflicting service names and enforces destination access before planning", async () => { await expect( review([ { id: "db", name: "Same Name", data: "copy" }, { id: "app", name: "same-name", data: "empty" }, ]), ).rejects.toMatchObject({ code: "IMPORT_NAME_INVALID" }); + await expect( + review([ + { id: "db", name: "api.v2", data: "copy" }, + { id: "app", name: "api v2", data: "empty" }, + ]), + ).rejects.toMatchObject({ code: "IMPORT_NAME_INVALID" }); h.authorize.mockRejectedValue(new Error("forbidden")); await expect(review()).rejects.toThrow("forbidden"); }); @@ -252,7 +289,7 @@ describe("reviewed platform imports", () => { { sourceId: external.id, envKey: "DATABASE_URL" }, ]); expect(privatePlan.services[1].create.services?.[0].volumes).toEqual([ - "data-0:/var/lib/postgresql", + "database-data-0:/var/lib/postgresql", ]); // Supplying secrets does not falsely report provider configuration drift. expect(h.discover).toHaveBeenCalledTimes(2); @@ -281,6 +318,7 @@ it("uses the existing Compose build for a custom Dockerfile and preserves readin readiness: { path: "/health" }, services: [{ build: "apps/api", dockerfile: "docker/production.Dockerfile" }], }); + expect(plan.project.services?.[0].advanced).toMatchObject({ readiness: { path: "/health", port: 3000 } }); expect(h.info).not.toHaveBeenCalled(); }); it("blocks an unavailable custom Dockerfile before import", async () => { diff --git a/apps/api/src/modules/migration/platform-import-worker.test.ts b/apps/api/src/modules/migration/platform-import-worker.test.ts index 2f4eaf5e9..f932056a4 100644 --- a/apps/api/src/modules/migration/platform-import-worker.test.ts +++ b/apps/api/src/modules/migration/platform-import-worker.test.ts @@ -1,20 +1,25 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; -import { AppError } from "@repo/core"; +import { AppError, ConflictError } from "@repo/core"; import { withProjectRuntimeLock } from "@repo/platform/engine/lib/project-runtime-lock"; import type { ExecutionContext } from "@repo/platform"; import type { PrivateImportPlan, PlannedImportService, } from "@repo/platform/engine/modules/migration/platform-import-plan"; +import { platformImportProject } from "@repo/platform/engine/modules/migration/platform-import-project"; const h = vi.hoisted(() => ({ ensure: vi.fn(), merge: vi.fn(), - connect: vi.fn(), + list: vi.fn(), + project: vi.fn(), + sourceService: vi.fn(), + rows: [] as Array<{ id: string; projectId: string; name: string }>, view: vi.fn(), deploy: vi.fn(), copy: vi.fn(), preflight: vi.fn(), prepareTarget: vi.fn(), + routes: vi.fn(), authorize: vi.fn(), disable: vi.fn(), bind: vi.fn(), @@ -29,7 +34,8 @@ const h = vi.hoisted(() => ({ vi.mock("@repo/db", () => ({ withAdvisoryLock: async (_key: string, fn: () => Promise) => fn(), repos: { - project: { update: h.update, findById: async (id: string) => ({ id, workspaceId: null }) }, + service: { listByProject: h.list }, + project: { update: h.update, findById: h.project }, dockerMigrationRun: { bindImportedProject: h.bind, findById: h.find, @@ -40,11 +46,11 @@ vi.mock("@repo/db", () => ({ vi.mock("@repo/platform/engine/modules/projects/project-crud.service", () => ({ ensureProject: h.ensure, })); -vi.mock("@repo/platform/engine/modules/projects/project-env.service", () => ({ - mergeEnvVars: h.merge, +vi.mock("@repo/platform/engine/modules/services/service-environment-state", () => ({ + mergeServiceEnvVars: h.merge, })); -vi.mock("@repo/platform/engine/modules/projects/project-connection.service", () => ({ - createConnection: h.connect, +vi.mock("@repo/platform/engine/modules/services/service.service", () => ({ + createService: h.sourceService, })); vi.mock("@repo/platform/engine/modules/projects/project-runtime.service", () => ({ disableProject: h.disable, @@ -62,6 +68,9 @@ vi.mock("@repo/platform/engine/modules/migration/platform-import-data", () => ({ vi.mock("@repo/platform/engine/modules/migration/migration-runtime", () => ({ prepareMigrationTarget: h.prepareTarget, })); +vi.mock("@repo/platform/engine/modules/migration/migration-routes", () => ({ + saveMigrationRoutes: h.routes, +})); vi.mock("@repo/platform/engine/modules/migration/platform-import-health", () => ({ verifyPlatformDeployment: h.verify, })); @@ -96,7 +105,11 @@ const service = (id: string, kind: "app" | "database" | "worker"): PlannedImport ...(kind === "database" ? { database: "pg_dump" } : { commit: "a".repeat(40), branch: "main" }), }, data: "copy", - create: { name: id }, + create: { + name: id, + internalAlias: id, + services: [{ name: id, image: "image:1", volumes: [`${id}-data:/data`] }], + }, environment: { TOKEN: "secret" }, bindings: [], }); @@ -107,11 +120,18 @@ const planFor = (services: PlannedImportService[]) => userId: "user", targetServerId: "server", services, + project: platformImportProject("imported-project", "server", services), }) as PrivateImportPlan; let row: any; beforeEach(() => { vi.resetAllMocks(); h.events.length = 0; + h.rows.length = 0; + h.routes.mockResolvedValue([]); + h.project.mockImplementation(async (id) => ({ id, workspaceId: null })); + h.list.mockImplementation(async (projectId) => + h.rows.filter((row) => row.projectId === projectId), + ); row = { id: "run", organizationId: "org", @@ -129,18 +149,34 @@ beforeEach(() => { }); h.ensure.mockImplementation(async (body) => { h.events.push(`create:${body.name}`); + h.rows.push( + ...body.services.map((row: { name: string }) => ({ + ...row, + id: `${body.name}-${row.name}`, + projectId: body.name, + })), + ); return { project_id: body.name }; }); + h.sourceService.mockImplementation(async (_ctx, projectId, body) => { + h.rows.push({ ...body, id: `${projectId}-${body.name}`, projectId }); + }); + let deploymentCount = 0; h.deploy.mockImplementation(async (_ctx, input) => { - h.events.push(`deploy:${input.projectId}`); - return { deployment: { id: `${input.projectId}-build` } }; + h.events.push(`deploy:${input.projectId}:${input.serviceIds.length}`); + return { deployment: { id: `${input.projectId}-build-${++deploymentCount}` } }; }); h.copy.mockImplementation(async (input) => { - h.events.push(`copy:${input.projectId}`); - }); - h.view.mockResolvedValue({ - outputs: [{ id: "database-url", internal: true, value: "postgres://new" }], + h.events.push(`copy:${input.service.source.id}`); }); + h.view.mockImplementation(async () => ({ + outputs: h.rows.map((row) => ({ + id: `${row.id}:url`, + sourceServiceId: row.id, + internal: true, + value: "postgres://new", + })), + })); h.prepareSource.mockImplementation(async () => { h.events.push("pause-project"); }); @@ -159,6 +195,60 @@ const lifecycle = (signal = new AbortController().signal) => ({ }), }); describe("provider import through normal project operations", () => { + it("stages custom domains through native migration routing before pausing the source", async () => { + const web = service("web", "app"); + web.create.name = "storefront"; + web.source.domains = [ + { hostname: "shop.example.com", portable: true }, + { hostname: "www.example.com", portable: true }, + { hostname: "shop.up.railway.app", portable: false }, + ]; + const plan = { ...planFor([web]), sourceControl: "railway-project" as const }; + const flow = lifecycle(); + await executePlatformImport(ctx, "run", plan, flow); + expect(h.routes).toHaveBeenCalledWith( + ctx, + "imported-project", + { + storefront: [ + { domainType: "custom", customDomain: "shop.example.com" }, + { domainType: "custom", customDomain: "www.example.com" }, + ], + }, + flow.log, + ); + expect(h.routes.mock.invocationCallOrder[0]).toBeLessThan( + h.prepareSource.mock.invocationCallOrder[0], + ); + expect(h.routes.mock.invocationCallOrder[0]).toBeLessThan(h.copy.mock.invocationCallOrder[0]); + }); + + it("reports domain conflicts without pausing Railway or deploying the destination", async () => { + const app = service("app", "app"); + app.source.domains = [{ hostname: "taken.example.com", portable: true }]; + h.routes.mockRejectedValueOnce( + new ConflictError("The domain is already connected to another project."), + ); + await expect( + executePlatformImport( + ctx, + "run", + { + ...planFor([app]), + sourceControl: "railway-project", + }, + lifecycle(), + ), + ).rejects.toMatchObject({ + statusCode: 409, + code: "IMPORT_DOMAIN_REVIEW_REQUIRED", + message: "The domain is already connected to another project.", + }); + expect(h.prepareSource).not.toHaveBeenCalled(); + expect(h.copy).not.toHaveBeenCalled(); + expect(h.deploy).not.toHaveBeenCalled(); + }); + it("prepares managed capacity before pausing Railway and does not stop the source if provisioning fails", async () => { const plan = { ...planFor([service("app", "app")]), sourceControl: "railway-project" as const }; const flow = lifecycle(); @@ -194,6 +284,135 @@ describe("provider import through normal project operations", () => { expect(h.events.indexOf("pause-project")).toBeLessThan(h.events.indexOf("copy:db")); expect(h.events.indexOf("restore-config")).toBeLessThan(firstDeployment); expect(h.copy.mock.calls.every(([input]) => input.deploymentId === undefined)).toBe(true); + expect(h.ensure).toHaveBeenCalledTimes(1); + expect(h.deploy).toHaveBeenCalledTimes(1); + expect(new Set(h.copy.mock.calls.map(([input]) => input.projectId))).toEqual( + new Set(["imported-project"]), + ); + expect(new Set(h.copy.mock.calls.map(([input]) => input.serviceId)).size).toBe(3); + expect(new Set(Object.values(row.recovery.providerImport.projects))).toEqual( + new Set(["imported-project"]), + ); + }); + it("imports two source projects as two native projects, keeping identical service names separate", async () => { + for (const name of ["first-project", "second-project"]) { + const services = [service("db", "database"), service("app", "app")]; + services.forEach((service) => (service.transfer = "cold-volume")); + const plan = { ...planFor(services), sourceControl: "railway-project" as const }; + plan.project.name = name; + await executePlatformImport(ctx, name, plan, lifecycle()); + expect(new Set(Object.values(row.recovery.providerImport.projects))).toEqual(new Set([name])); + } + expect(h.ensure.mock.calls.map(([body]) => body.name)).toEqual([ + "first-project", + "second-project", + ]); + expect(h.rows.map((row) => row.projectId)).toEqual([ + "first-project", + "first-project", + "second-project", + "second-project", + ]); + expect(h.copy.mock.calls.map(([input]) => [input.projectId, input.serviceId])).toEqual([ + ["first-project", "first-project-db"], + ["first-project", "first-project-app"], + ["second-project", "second-project-db"], + ["second-project", "second-project-app"], + ]); + }); + it("stores each service's environment separately instead of merging it into project-wide values", async () => { + const app = service("app", "app"), + db = service("db", "database"); + app.environment = { PORT: "3000", TOKEN: "app-secret" }; + db.environment = { PORT: "5432", TOKEN: "database-secret" }; + await executePlatformImport(ctx, "run", planFor([app, db]), lifecycle()); + for (const entry of [app, db]) { + expect(h.merge).toHaveBeenCalledWith( + ctx, + "imported-project", + `imported-project-${entry.source.id}`, + { + environment: "production", + deletes: [], + upserts: Object.entries(entry.environment).map(([key, value]) => ({ + key, + value, + isSecret: true, + sourceId: null, + })), + }, + ); + } + }); + it("adds a source-built app through the native service creator in the same project", async () => { + const app = service("app", "app"); + app.create = { + name: "app", + gitOwner: "owner", + gitRepo: "repo", + gitBranch: "main", + framework: "nextjs", + rootDirectory: "apps/web", + installCommand: "bun install", + startCommand: "bun start", + port: 3000, + }; + await executePlatformImport(ctx, "run", planFor([app, service("db", "database")]), lifecycle()); + expect(h.ensure).toHaveBeenCalledTimes(1); + expect(h.sourceService).toHaveBeenCalledWith( + ctx, + "imported-project", + expect.objectContaining({ + name: "app", + kind: "monorepo", + rootDirectory: "apps/web", + framework: "nextjs", + ports: ["3000"], + exposed: false, + }), + ); + expect(h.deploy).toHaveBeenLastCalledWith( + ctx, + expect.objectContaining({ + projectId: "imported-project", + commitSha: "a".repeat(40), + }), + ); + }); + it("rejects reviews from the old per-service-project flow before allocating a destination", async () => { + const plan = planFor([service("app", "app")]); + delete (plan as Partial).project; + await expect(executePlatformImport(ctx, "run", plan, lifecycle())).rejects.toMatchObject({ + code: "IMPORT_REVIEW_EXPIRED", + }); + expect(h.ensure).not.toHaveBeenCalled(); + expect(h.prepareSource).not.toHaveBeenCalled(); + }); + it("pauses a shared destination once before restoring the Railway source", async () => { + const plan = planFor([service("app", "app"), service("db", "database")]); + Object.assign(row.recovery.providerImport, { + planEnc: JSON.stringify(plan), + projects: { app: "shared-project", db: "shared-project" }, + deployments: { app: "shared-build", db: "shared-build" }, + }); + await pauseFailedPlatformImport("run"); + expect(h.disable).toHaveBeenCalledExactlyOnceWith("shared-project", "org", "shared-build"); + expect(h.restoreSource).toHaveBeenCalledTimes(1); + expect(h.disable.mock.invocationCallOrder[0]).toBeLessThan( + h.restoreSource.mock.invocationCallOrder[0], + ); + }); + it("can pause its own database-only predecessor when a complete deployment fails", async () => { + const plan = planFor([service("app", "app"), service("db", "database")]); + Object.assign(row.recovery.providerImport, { + planEnc: JSON.stringify(plan), + projects: { app: "shared", db: "shared" }, + deployments: { app: "failed-build", db: "failed-build" }, + ownedDeploymentIds: ["database-build", "failed-build"], + }); + h.project.mockResolvedValue({ id: "shared", activeDeploymentId: "database-build" }); + await pauseFailedPlatformImport("run"); + expect(h.disable).toHaveBeenCalledExactlyOnceWith("shared", "org", "database-build"); }); it("stops every imported Railway workload before resuming the source on failure", async () => { const plan = { @@ -219,29 +438,43 @@ describe("provider import through normal project operations", () => { const plan = planFor([worker, app, db]); await executePlatformImport(ctx, "run", plan, lifecycle()); expect(h.events).toEqual([ - "create:db", - "create:app", - "create:worker", - "deploy:db", - "healthy:db-build", + "create:imported-project", + "deploy:imported-project:1", + "healthy:imported-project-build-1", "copy:db", "copy:app", - "deploy:app", - "healthy:app-build", "copy:worker", - "deploy:worker", - "healthy:worker-build", + "deploy:imported-project:3", + "healthy:imported-project-build-2", ]); - expect(h.ensure).toHaveBeenCalledWith(app.create, "org", ctx, { mustCreate: true }); - expect(h.connect).toHaveBeenCalledWith( + expect(h.ensure).toHaveBeenCalledExactlyOnceWith(plan.project, "org", ctx, { + mustCreate: true, + }); + expect(h.merge).toHaveBeenCalledWith( ctx, - "app", - { sourceProjectId: "db", outputId: "database-url", envKey: "DATABASE_URL", mode: "internal" }, - { defer: true }, + "imported-project", + "imported-project-app", + expect.objectContaining({ + upserts: [{ key: "DATABASE_URL", value: "postgres://new", sourceId: null, isSecret: true }], + }), ); - expect(h.deploy).toHaveBeenCalledWith( + expect(h.deploy).toHaveBeenNthCalledWith( + 1, ctx, - expect.objectContaining({ projectId: "app", commitSha: "a".repeat(40), branch: "main" }), + expect.objectContaining({ + projectId: "imported-project", + serviceIds: ["imported-project-db"], + strictServiceScope: true, + }), + ); + expect(h.deploy).toHaveBeenNthCalledWith( + 2, + ctx, + expect.objectContaining({ + projectId: "imported-project", + serviceIds: ["imported-project-db", "imported-project-app", "imported-project-worker"], + strictServiceScope: true, + }), ); expect(row.recovery.providerImport.completed).toEqual(["db", "app", "worker"]); }); @@ -253,7 +486,13 @@ describe("provider import through normal project operations", () => { executePlatformImport(ctx, "run", planFor([app, service("db", "database")]), lifecycle()), ).rejects.toThrow("restore failed"); expect(h.deploy).toHaveBeenCalledTimes(1); - expect(h.deploy).toHaveBeenCalledWith(ctx, expect.objectContaining({ projectId: "db" })); + expect(h.deploy).toHaveBeenCalledWith( + ctx, + expect.objectContaining({ + projectId: "imported-project", + serviceIds: ["imported-project-db"], + }), + ); }); it("checkpoints the created deployment before observing a cancelled phase", async () => { const flow = lifecycle(); @@ -263,7 +502,7 @@ describe("provider import through normal project operations", () => { await expect( executePlatformImport(ctx, "run", planFor([service("app", "app")]), flow), ).rejects.toThrow("cancelled"); - expect(row.recovery.providerImport.deployments).toEqual({ app: "app-build" }); + expect(row.recovery.providerImport.deployments).toEqual({ app: "imported-project-build-1" }); }); it("rejects dependency cycles rather than starting a partial recursive import", () => { const a = service("a", "app"), @@ -272,7 +511,7 @@ describe("provider import through normal project operations", () => { b.bindings = [{ sourceId: "a", envKey: "URL" }]; expect(() => orderedImports([a, b])).toThrow(/cycle/); }); - it("retains databases and pauses only the import's own app deployment identities on failure", async () => { + it("pauses imported deployment identities before releasing source recovery", async () => { const plan = planFor([service("app", "app"), service("db", "database")]); Object.assign(row.recovery.providerImport, { planEnc: JSON.stringify(plan), @@ -280,7 +519,8 @@ describe("provider import through normal project operations", () => { deployments: { app: "app-build", db: "db-build" }, }); await pauseFailedPlatformImport("run"); - expect(h.disable).toHaveBeenCalledExactlyOnceWith("p-app", "org", "app-build"); + expect(h.disable).toHaveBeenCalledWith("p-app", "org", "app-build"); + expect(h.disable).toHaveBeenCalledWith("p-db", "org", "db-build"); expect(row.recovery.providerImport.cleanupPending).toBe(false); }); it("leaves the cleanup checkpoint pending if the destination cannot be reached", async () => { diff --git a/apps/api/src/modules/migration/railway-wiring.test.ts b/apps/api/src/modules/migration/railway-wiring.test.ts index f0ab87402..b7b45588a 100644 --- a/apps/api/src/modules/migration/railway-wiring.test.ts +++ b/apps/api/src/modules/migration/railway-wiring.test.ts @@ -58,11 +58,12 @@ describe("Railway project wiring", () => { { sourceId: "store", envKey: "ARBITRARY_SETTING", - valueTemplate: "custom://alice:password@${host}:7878/second-db?option=1", }, ]); expect(store.source.port).toBe(7878); - expect(app.source.env).toEqual({}); + expect(app.source.env).toEqual({ + ARBITRARY_SETTING: "custom://alice:password@import-store:7878/second-db?option=1", + }); }); it("keeps literal shared secrets and rewrites self-hosts without a recursive connection", () => { const store = service("store", { TOKEN: "secret", HOST: "store.railway.internal" }, 7878); @@ -78,4 +79,23 @@ describe("Railway project wiring", () => { expect(app.source.blockers.join(" ")).toMatch(/MISSING/); expect(app.source.blockers.join(" ")).toMatch(/UNRESOLVED/); }); + it("keeps hostname-only and multi-host values on the native project network", () => { + const db = service("db"), + redis = service("redis"); + const app = service("app", { + HOST: "db.railway.internal", + HOSTS: "db.railway.internal,redis.railway.internal", + }); + const bindings = resolveRailwayBindings( + app, + new Map([ + ["db", db], + ["redis", redis], + ["app", app], + ]), + ); + expect(app.source.env).toEqual({ HOST: "import-db", HOSTS: "import-db,import-redis" }); + expect(new Set(bindings.map((binding) => binding.sourceId))).toEqual(new Set(["db", "redis"])); + expect(app.source.blockers).toEqual([]); + }); }); diff --git a/apps/api/test/e2e/platform-import-data.e2e.test.ts b/apps/api/test/e2e/platform-import-data.e2e.test.ts index ef24f936c..ac15215ea 100644 --- a/apps/api/test/e2e/platform-import-data.e2e.test.ts +++ b/apps/api/test/e2e/platform-import-data.e2e.test.ts @@ -23,14 +23,15 @@ import { describeDockerE2E, requireDocker, dockerSocketPath } from "../helpers/d const h = vi.hoisted(() => ({ project: {} as any, source: undefined as unknown, + service: undefined as any, runtime: undefined as unknown, workspaceId: undefined as string | undefined, exportOnly: vi.fn(), })); vi.mock("@repo/db", () => ({ repos: { - project: { findById: async () => h.project }, - service: { listByProject: async () => [] }, + project: { findById: async () => h.project, getEnvMap: async () => ({}) }, + service: { listByProject: async () => h.service ? [h.service] : [] }, }, })); vi.mock("@repo/platform/engine/modules/migration/migration-access", () => ({ @@ -378,10 +379,16 @@ describeDockerE2E("generic stopped-volume provider import", () => { environment: {}, bindings: [], } as PlannedImportService; + h.service = { + id: `${projectId}-service`, projectId, name: fixture.name, + image: fixture.image, environment: {}, + volumes: service.create.volumes, namespaceVolumes: true, + }; const args = { runId: tag, service, projectId, + serviceId: h.service.id, signal: new AbortController().signal, log: () => {}, progress: vi.fn(), diff --git a/apps/api/test/e2e/platform-import-project.e2e.test.ts b/apps/api/test/e2e/platform-import-project.e2e.test.ts new file mode 100644 index 000000000..e90c76343 --- /dev/null +++ b/apps/api/test/e2e/platform-import-project.e2e.test.ts @@ -0,0 +1,446 @@ +import { afterAll, beforeAll, expect, it, vi } from "vitest"; +import { randomUUID } from "node:crypto"; +import { spawn } from "node:child_process"; +import { createConnection } from "node:net"; +import { once } from "node:events"; +import { + BuildLogger, + CommandSourceBackupExecutor, + CloudDockerRuntime, + DockerRuntime, + type CommandExecutor, +} from "@repo/adapters"; +import { CloudServerConnection } from "../../../../packages/adapters/src/runtime/cloud/server-connection"; +import { CloudWorkspaceExecutor } from "../../../../packages/adapters/src/runtime/cloud/workspace-executor"; +import { db, repos, schema } from "@repo/db"; +import type { ExecutionContext } from "@repo/platform"; +import type { ProviderInventory } from "@repo/platform/engine/modules/migration/providers/types"; +import { encrypt, decryptEnvMap } from "@repo/platform/engine/lib/encryption"; +import { deployComposeServices } from "@repo/platform/engine/modules/deployments/compose/deploy.service"; +import { projectServicesToDeployableServices } from "@repo/platform/engine/modules/deployments/compose/project-services"; +import { seedDeployment, seedOrg, setActive } from "../helpers/seed"; +import { describeDockerE2E, requireDocker, dockerSocketPath } from "../helpers/docker-e2e"; + +// Substitute Railway's control/SSH boundary and the asynchronous build queue. +// Review, project creation, encrypted environments, migration checkpoints, +// volume transfer and the ordinary Compose deployment engine remain real. +const h = vi.hoisted(() => ({ + discover: vi.fn(), + prepare: vi.fn(), + assertStopped: vi.fn(), + openSource: vi.fn(), + runtime: vi.fn(), + deploy: vi.fn(), +})); +vi.mock("@repo/platform/engine/modules/credentials/connection-authorization", () => ({ + getProviderAccess: async () => ({ provider: "railway", publicFields: {}, secrets: {} }), +})); +vi.mock("@repo/platform/engine/modules/migration/providers/railway", () => ({ + railwayMigrationProvider: { discover: h.discover }, +})); +vi.mock("@repo/platform/engine/modules/migration/providers/railway-project-transfer", () => ({ + prepareRailwayProjectTransfer: h.prepare, + assertRailwayExportOnly: h.assertStopped, + restoreRailwayProjectTransfer: async () => {}, +})); +vi.mock("@repo/platform/engine/modules/migration/providers/railway-data", () => ({ + openRailwayDataSource: h.openSource, +})); +vi.mock("@repo/platform/engine/modules/migration/migration-runtime", async (original) => ({ + ...(await original()), + createMigrationDockerRuntime: h.runtime, +})); +vi.mock("@repo/platform/engine/modules/deployments/build.service", () => ({ + triggerDeployment: h.deploy, +})); +import { + discoverPlatform, + reviewPlatform, + loadReviewedImport, +} from "@repo/platform/engine/modules/migration/platform-import-plan"; +import { executePlatformImport } from "@repo/platform/engine/modules/migration/platform-import-worker"; + +describeDockerE2E("Railway projects retain native service boundaries", () => { + const tag = `import-groups-${randomUUID().slice(0, 8)}`; + const containers = new Set(); + const volumes = new Set(); + const projects: Array<{ id: string; slug: string }> = []; + const sources = new Map(); + let runtime: DockerRuntime; + const exec = async (id: string, command: string) => + (await (await runtime.inContainerExecutor(id)).exec(command)).trim(); + + beforeAll(async () => { + await requireDocker(); + runtime = await DockerRuntime.create({ transport: "socket" }); + await runtime.pullImage("redis:7-alpine"); + await runtime.pullImage("alpine:3"); + const host = await runtime.docker.createContainer({ + Image: "alpine:3", + Cmd: ["sleep", "infinity"], + Labels: { "openship.test": tag }, + HostConfig: { NetworkMode: "host" }, + }); + containers.add(host.id); + await host.start(); + vi.spyOn(CloudServerConnection.prototype, "connectDocker").mockImplementation(async () => { + const socket = createConnection(dockerSocketPath); + await once(socket, "connect"); + return socket; + }); + vi.spyOn(CloudServerConnection.prototype, "ensureDocker").mockResolvedValue(); + vi.spyOn(CloudServerConnection.prototype, "publishedPorts").mockResolvedValue([]); + vi.spyOn(CloudWorkspaceExecutor.prototype, "exec").mockImplementation((command) => + exec(host.id, command), + ); + }); + afterAll(async () => { + if (!runtime) return; + for (const project of projects) { + for (const id of await runtime.listProjectContainerIds(project.id)) containers.add(id); + } + for (const id of containers) { + const container = runtime.docker.getContainer(id); + for (const mount of (await container.inspect()).Mounts) { + if (mount.Type === "volume" && mount.Name) volumes.add(mount.Name); + } + await container.remove({ force: true }); + } + for (const project of projects) await runtime.removeNetwork(project.slug); + for (const volume of (await runtime.docker.listVolumes()).Volumes ?? []) { + if (projects.some((project) => volume.Labels?.["openship.project"] === project.id)) + volumes.add(volume.Name); + } + for (const name of volumes) await runtime.docker.getVolume(name).remove(); + await runtime.dispose(); + vi.restoreAllMocks(); + }); + + async function start(image: string, volume: string, command: string[]) { + const container = await runtime.docker.createContainer({ + Image: image, + Cmd: command, + Labels: { "openship.test": tag }, + HostConfig: { Binds: [`${volume}:/data`] }, + }); + containers.add(container.id); + await container.start(); + return container.id; + } + + it.each(["socket", "cloud"] as const)( + "imports two three-service projects through %s, isolating DNS, env and persisted data", + async (transport) => { + const owner = await seedOrg({ ownsHost: true }); + const ctx = { + ...owner, + role: "owner", + user: { id: owner.userId, name: "Test", email: "test@example.test" }, + sessionKind: "cookie", + } as ExecutionContext; + const serverId = `${tag}-${transport}`; + await db.insert(schema.servers).values({ + id: serverId, + organizationId: owner.organizationId, + isLocal: true, + sshHost: "localhost", + sshUser: "root", + sshAuthMethod: "key", + }); + const openRuntime = () => + DockerRuntime.create( + transport === "socket" + ? { transport: "socket" } + : { + transport: "cloud", + // The real managed Docker transport, with only its provider tunnel + // replaced. Host execution must never be used for copying data. + cloudConnection: async () => { + const socket = createConnection(dockerSocketPath); + await once(socket, "connect"); + return socket; + }, + executor: { + exec: async () => { + throw new Error("Unexpected API-host execution"); + }, + dispose: async () => {}, + } as unknown as CommandExecutor, + }, + ); + h.runtime.mockImplementation(openRuntime); + h.prepare.mockImplementation(async (_run, plan) => { + for (const service of plan.services) { + const source = sources.get(service.source.id); + if (source) await runtime.docker.getContainer(source.container).stop({ t: 10 }); + } + }); + h.assertStopped.mockImplementation(async () => { + for (const source of sources.values()) { + expect( + (await runtime.docker.getContainer(source.container).inspect()).State.Running, + ).toBe(false); + } + }); + h.openSource.mockImplementation(async (_run, service) => { + const source = sources.get(service.id)!; + const channel = { + rawExec: async (command: string) => { + const child = spawn("docker", ["exec", source.exporter, "sh", "-c", command]); + return { + stdout: child.stdout, + stderr: child.stderr, + kill: () => { + child.kill("SIGKILL"); + }, + onClose: new Promise((resolve, reject) => { + child.once("error", reject); + child.once("close", (code) => resolve(code ?? 1)); + }), + }; + }, + } as CommandExecutor; + return { + executor: new CommandSourceBackupExecutor(channel, ["/data"]), + dispose: async () => {}, + }; + }); + h.deploy.mockImplementation(async (_ctx, input) => { + // This isolated daemon uses its private project networks; no edge or + // published host ports are needed for this container-IP routing fixture. + await repos.project.update(input.projectId, { routeStrategy: "container-ip" }); + const project = (await repos.project.findById(input.projectId))!; + const services = await repos.service.listByProject(project.id); + const deployment = await seedDeployment(project, { + status: "deploying", + trigger: "manual", + imageRef: "compose", + containerId: "compose", + meta: { + runtimeMode: "docker", + deployTarget: "local", + composeServices: projectServicesToDeployableServices(services), + }, + }); + const target = + transport === "socket" + ? await openRuntime() + : await CloudDockerRuntime.forWorkspace( + { + workspaces: { + get: async () => ({ + id: serverId, + namespace: owner.organizationId, + status: "running", + }), + }, + } as unknown as Parameters[0], + { + workspaceId: serverId, + ownerWorkspaceId: serverId, + projectId: project.id, + namespace: owner.organizationId, + provisionLock: { run: (work) => work() }, + resolveRegistryAuth: async () => undefined, + }, + ); + try { + const result = await deployComposeServices( + project, + deployment, + target, + new BuildLogger(() => {}), + { + executor: null, + localHost: false, + preparedLocalImages: new Map(services.map((s) => [s.id, s.image!])), + targetServiceIds: new Set(input.serviceIds), + }, + ); + expect(result.status, JSON.stringify(result)).toBe("ready"); + await repos.deployment.updateStatus(deployment.id, "ready"); + await setActive(project.id, deployment.id); + await result.cutover?.commit(); + } finally { + await target.dispose(); + } + return { deployment }; + }); + + const imported: Array<{ id: string; marker: string }> = []; + for (const projectNumber of [1, 2]) { + const marker = `${tag}-${transport}-${projectNumber}`; + const services: ProviderInventory["services"] = []; + for (const name of ["db", "cache"]) { + const id = `${marker}-${name}`; + const volume = `${id}-source`; + await runtime.docker.createVolume({ Name: volume }); + volumes.add(volume); + const container = await start("redis:7-alpine", volume, [ + "redis-server", + "--appendonly", + "yes", + ]); + await expect + .poll(() => exec(container, "redis-cli PING"), { timeout: 10_000 }) + .toBe("PONG"); + await exec(container, `redis-cli SET marker '${marker}-${name}'`); + const exporter = await start("alpine:3", volume, ["sleep", "infinity"]); + sources.set(id, { container, exporter }); + services.push({ + id, + name, + kind: "database", + database: "redis_rdb", + image: "redis:7-alpine", + instanceId: container, + privateDomain: `${name}.railway.internal`, + port: 6379, + env: { SERVICE_SECRET: `${marker}-${name}` }, + rawEnv: {}, + variables: [], + volumes: [{ id: volume, mountPath: "/data" }], + domains: [], + warnings: [], + blockers: [], + startCommand: "redis-server --appendonly yes", + }); + } + services.push({ + id: `${marker}-worker`, + name: "worker", + kind: "worker", + image: "redis:7-alpine", + instanceId: "source-worker", + port: 8080, + env: { + SERVICE_SECRET: `${marker}-worker`, + PRIMARY_HOST: "db.railway.internal", + CACHE_HOST: "cache.railway.internal", + BOTH_HOSTS: "db.railway.internal,cache.railway.internal", + }, + rawEnv: {}, + variables: [], + volumes: [], + domains: [ + { hostname: `${marker}.example.com`, portable: true }, + { hostname: `www.${marker}.example.com`, portable: true }, + { hostname: `${marker}.up.railway.app`, portable: false }, + ], + warnings: [], + blockers: [], + startCommand: + "sh -c 'while true; do printf \"HTTP/1.1 200 OK\\r\\nContent-Length: 6\\r\\nConnection: close\\r\\n\\r\\nready\\n\" | nc -l -p 8080; done'", + }); + const inventory: ProviderInventory = { + provider: "railway", + source: { connectionId: "fixture", projectId: marker, environmentId: "production" }, + projectName: marker, + environmentName: "Production", + services, + warnings: [], + }; + h.discover.mockImplementation(async () => structuredClone(inventory)); + const discovery = await discoverPlatform(ctx, inventory.source); + const review = await reviewPlatform(ctx, { + draftId: discovery.draftId, + targetServerId: serverId, + services: discovery.services.map((s) => ({ id: s.id, name: s.name, data: "copy" })), + }); + expect(review.ready, JSON.stringify(review)).toBe(true); + const plan = await loadReviewedImport(ctx, review.planId); + const runId = `${marker}-run`; + await repos.dockerMigrationRun.create({ + id: runId, + organizationId: owner.organizationId, + targetServerId: serverId, + projectName: marker, + mode: "provider_import", + status: "adopting", + inputSnapshot: { provider: "railway", source: inventory.source }, + recovery: { + providerImport: { + planId: plan.id, + planEnc: encrypt(JSON.stringify(plan)), + connectionId: "fixture", + projects: {}, + completed: [], + cleanupPending: true, + }, + }, + }); + try { + await executePlatformImport(ctx, runId, plan, { + signal: new AbortController().signal, + log: () => {}, + progress: () => {}, + phase: async () => {}, + wait: async (id) => (await repos.deployment.findById(id))?.status === "ready", + }); + } finally { + const saved = await repos.project.listByOrganization(owner.organizationId); + for (const p of saved.rows) + if (!projects.some((known) => known.id === p.id)) projects.push(p); + } + const checkpoint = (await repos.dockerMigrationRun.findById(runId))!.recovery + .providerImport!; + const ids = [...new Set(Object.values(checkpoint.projects))]; + expect(ids).toHaveLength(1); + const id = ids[0]; + expect(await repos.service.listByProject(id)).toHaveLength(3); + expect(checkpoint.completed).toHaveLength(3); + expect((await repos.project.listByOrganization(owner.organizationId)).total).toBe( + projectNumber, + ); + expect(await repos.project.getEnvMap(id, "production", null)).toEqual({}); + imported.push({ id, marker }); + } + + // Check both projects AFTER the second import: duplicate service names + // must not redirect the first project's DNS or overwrite its storage. + for (const { id, marker } of imported) { + const project = (await repos.project.findById(id))!; + const rows = await repos.service.listByProject(id); + const deployed = await repos.service.listByDeployment(project.activeDeploymentId!); + for (const service of rows) { + const env = decryptEnvMap(await repos.project.getEnvMap(id, "production", service.id)); + expect(env.SERVICE_SECRET).toBe(`${marker}-${service.name}`); + const container = deployed.find((r) => r.serviceId === service.id)!.containerId!; + expect(await exec(container, 'printf %s "$SERVICE_SECRET"')).toBe(env.SERVICE_SECRET); + if (service.name === "worker") { + expect(service.exposed).toBe(true); + expect(service.publicEndpoints).toEqual([ + expect.objectContaining({ port: 8080, customDomain: `${marker}.example.com` }), + ]); + const domains = await repos.domain.listByProject(id); + expect(domains.map((domain) => domain.hostname).sort()).toEqual( + [`${marker}.example.com`, `www.${marker}.example.com`].sort(), + ); + expect( + domains.every( + (domain) => + domain.serviceId === service.id && domain.targetPort === 8080 && !domain.verified, + ), + ).toBe(true); + expect(await exec(container, "wget -qO- http://127.0.0.1:8080")).toBe("ready"); + expect(await exec(container, 'printf %s "$BOTH_HOSTS"')).toBe("db,cache"); + expect(await exec(container, 'redis-cli -h "$PRIMARY_HOST" GET marker')).toBe( + `${marker}-db`, + ); + expect(await exec(container, 'redis-cli -h "$CACHE_HOST" GET marker')).toBe( + `${marker}-cache`, + ); + } else { + expect(await exec(container, "redis-cli GET marker")).toBe(`${marker}-${service.name}`); + await exec(container, "redis-cli SET new-write survives"); + await runtime.docker.getContainer(container).restart({ t: 10 }); + await expect + .poll(() => exec(container, "redis-cli GET marker"), { timeout: 10_000 }) + .toBe(`${marker}-${service.name}`); + expect(await exec(container, "redis-cli GET new-write")).toBe("survives"); + } + } + } + }, + ); +}); diff --git a/apps/api/test/modules/migration/platform-import-domains.test.ts b/apps/api/test/modules/migration/platform-import-domains.test.ts new file mode 100644 index 000000000..26c5800b8 --- /dev/null +++ b/apps/api/test/modules/migration/platform-import-domains.test.ts @@ -0,0 +1,185 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { db, eq, repos, schema, type Service } from "@repo/db"; +import type { ExecutionContext } from "@repo/platform"; +import type { PlannedImportService } from "@repo/platform/engine/modules/migration/platform-import-plan"; +import { platformImportRoutes } from "@repo/platform/engine/modules/migration/platform-import-project"; +import { saveMigrationRoutes } from "@repo/platform/engine/modules/migration/migration-routes"; +import { seedOrg, seedProject, seedService } from "../../helpers/seed"; + +const live = vi.hoisted(() => ({ reconcile: vi.fn() })); +vi.mock("@repo/platform/engine/lib/route-apply.service", async (original) => ({ + ...(await original()), + reconcileProjectRoutes: live.reconcile, +})); + +function recipe( + service: Service, + domains: PlannedImportService["source"]["domains"], + data: PlannedImportService["data"] = "empty", +): PlannedImportService { + return { + source: { + id: `source-${service.id}`, + name: "Provider name", + kind: "app", + domains, + env: {}, + rawEnv: {}, + variables: [], + volumes: [], + warnings: [], + blockers: [], + }, + data, + create: { name: service.name }, + environment: {}, + bindings: [], + }; +} + +describe("provider imports share native pending-domain persistence", () => { + const organizations: string[] = []; + let ctx: ExecutionContext; + beforeEach(async () => { + vi.clearAllMocks(); + ctx = (await seedOrg()) as ExecutionContext; + organizations.push(ctx.organizationId); + }); + afterEach(async () => { + for (const id of organizations.splice(0)) { + await db.delete(schema.project).where(eq(schema.project.organizationId, id)); + await db.delete(schema.servers).where(eq(schema.servers.organizationId, id)); + await db.delete(schema.cloudWorkspace).where(eq(schema.cloudWorkspace.organizationId, id)); + await db.delete(schema.organization).where(eq(schema.organization.id, id)); + } + }); + + it.each(["server", "cloud"])( + "saves custom aliases on their services without verifying or applying live routes (%s)", + async (target) => { + const managed = + target === "cloud" + ? await repos.cloudWorkspace.create({ + organizationId: ctx.organizationId, + name: "Import destination", + }) + : null; + const server = managed + ? await repos.server.findByWorkspace(managed.id, ctx.organizationId) + : null; + const project = await seedProject(ctx.organizationId, { + framework: "docker-compose", + serverId: server?.id, + }); + const web = await seedService(project.id, { + name: "storefront", + ports: ["8080"], + exposed: false, + }); + const api = await seedService(project.id, { + name: "api", + kind: "monorepo", + ports: ["3000"], + exposed: false, + }); + const internal = await seedService(project.id, { + name: "worker", + ports: ["9000"], + exposed: false, + }); + const plans = [ + recipe(web, [ + { hostname: "shop.example.com", portable: true }, + { hostname: "www.example.com", portable: true }, + { hostname: "shop.up.railway.app", portable: false }, + ]), + recipe(api, [ + { hostname: "api.example.com", portable: true }, + { hostname: "api.vercel.app", portable: false }, + ]), + recipe(internal, [{ hostname: "worker.up.railway.app", portable: false }]), + recipe(internal, [{ hostname: "external.example.com", portable: true }], "external"), + ]; + const routes = platformImportRoutes(plans); + await saveMigrationRoutes(ctx, project.id, routes, () => {}); + // Re-saving uses the existing rows, including verification tokens and aliases. + const original = await repos.domain.listByProject(project.id); + await saveMigrationRoutes(ctx, project.id, routes, () => {}); + const domains = await repos.domain.listByProject(project.id); + expect(domains.map((domain) => domain.id).sort()).toEqual( + original.map((domain) => domain.id).sort(), + ); + expect(domains).toHaveLength(3); + for (const [hostname, serviceId, targetPort] of [ + ["shop.example.com", web.id, 8080], + ["www.example.com", web.id, 8080], + ["api.example.com", api.id, 3000], + ] as const) { + const domain = domains.find((item) => item.hostname === hostname)!; + expect(domain).toMatchObject({ + projectId: project.id, + serviceId, + targetPort, + verified: false, + status: "pending", + }); + expect(domain.verificationToken).toBeTruthy(); + expect(domain.verificationToken).toBe( + original.find((item) => item.id === domain.id)?.verificationToken, + ); + } + expect((await repos.service.findById(web.id))?.publicEndpoints).toEqual([ + expect.objectContaining({ port: 8080, customDomain: "shop.example.com" }), + ]); + expect((await repos.project.findById(project.id))?.compositeRoutes).toEqual([ + expect.objectContaining({ + hostname: "www.example.com", + rootServiceId: web.id, + rootPort: 8080, + }), + ]); + expect((await repos.service.findById(internal.id))?.exposed).toBe(false); + expect(live.reconcile).not.toHaveBeenCalled(); + }, + ); + + it("refuses a cross-organization domain before saving any imported route", async () => { + const foreign = (await seedOrg()) as ExecutionContext; + organizations.push(foreign.organizationId); + const existingProject = await seedProject(foreign.organizationId, { + framework: "docker-compose", + }); + const existingService = await seedService(existingProject.id, { + name: "owner", + ports: ["8080"], + }); + await saveMigrationRoutes( + foreign, + existingProject.id, + platformImportRoutes([ + recipe(existingService, [{ hostname: "taken.example.com", portable: true }]), + ]), + () => {}, + ); + const original = await repos.domain.listByProject(existingProject.id); + const project = await seedProject(ctx.organizationId, { framework: "docker-compose" }); + const web = await seedService(project.id, { name: "web", ports: ["8080"], exposed: false }); + await expect( + saveMigrationRoutes( + ctx, + project.id, + platformImportRoutes([ + recipe(web, [ + { hostname: "available.example.com", portable: true }, + { hostname: "taken.example.com", portable: true }, + ]), + ]), + () => {}, + ), + ).rejects.toThrow("already connected to another project"); + expect(await repos.domain.listByProject(project.id)).toEqual([]); + expect((await repos.service.findById(web.id))?.exposed).toBe(false); + expect(await repos.domain.listByProject(existingProject.id)).toEqual(original); + expect(live.reconcile).not.toHaveBeenCalled(); + }); +}); diff --git a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/BuildSettings.tsx b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/BuildSettings.tsx index f3014f53a..8cacf0de2 100644 --- a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/BuildSettings.tsx +++ b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/BuildSettings.tsx @@ -112,9 +112,12 @@ export const BuildSettings = () => { /** One shared editor, accessible here and through workload shortcuts. */ export function ProjectEnvironmentSettings() { - const { id } = useProjectSettings(); + const { id, projectData, servicesData } = useProjectSettings(); const { t } = useI18n(); const [envOpen, setEnvOpen] = useState(false); + const application = projectApplication(projectData, servicesData.services); + const opensApplication = application?.mode === "project" && + !servicesData.services.some((service) => !service.applicationMode); return ( <> + {t.projectDetail.services.detail.tabs.env} + + + ) : ( - } + )} /> - setEnvOpen(false)} /> + {!opensApplication && ( + setEnvOpen(false)} /> + )} ); } diff --git a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/environment-scope.test.tsx b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/environment-scope.test.tsx index a60f81f64..d0ef8b87c 100644 --- a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/environment-scope.test.tsx +++ b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/environment-scope.test.tsx @@ -8,6 +8,7 @@ import { ProjectSettingsProvider, useProjectSettings } from "@/context/ProjectSe import { BuildSettings } from "./BuildSettings"; import { AppConfiguration } from "./AppConfiguration"; import { ApplicationSettingsForm } from "@/components/project-settings/ApplicationSettingsForm"; +import { ProjectEnvironmentPanel } from "@/components/project-settings/ProjectEnvironmentPanel"; import { ProjectApplicationPanel } from "./services/ProjectApplicationPanel"; const api = vi.hoisted(() => ({ @@ -269,8 +270,7 @@ describe("project environment access (GH-881)", () => { ])( "rebuilds saved environment changes for $framework on $deployTarget", async ({ framework, ...extra }) => { - await mountProject(framework, , extra); - await act(async () => button("Edit").click()); + await mountProject(framework, , extra); await editInput( [...document.querySelectorAll("input")].find((input) => input.value === "old")!, "new", @@ -293,8 +293,7 @@ describe("project environment access (GH-881)", () => { }, { framework: "docker-compose", deployTarget: "cloud" as const, options: {} }, ])("preserves runtime refresh for $framework on $deployTarget", async ({ framework, ...extra }) => { - await mountProject(framework, , extra); - await act(async () => button("Edit").click()); + await mountProject(framework, , extra); await editInput( [...document.querySelectorAll("input")].find((input) => input.value === "old")!, "new", @@ -307,8 +306,7 @@ describe("project environment access (GH-881)", () => { it("keeps a static site's saved values available when starting the rebuild fails", async () => { api.trigger.mockRejectedValueOnce(new Error("unavailable")); - await mountProject("static", , { options: { workloadType: "static" } }); - await act(async () => button("Edit").click()); + await mountProject("static", , { options: { workloadType: "static" } }); await editInput( [...document.querySelectorAll("input")].find((input) => input.value === "old")!, "new", @@ -329,6 +327,7 @@ describe("project environment access (GH-881)", () => { it.each(["nextjs", "docker-compose"])( "keeps %s Settings reachable and edits shared project inputs without overwriting secrets", async (framework) => { + api.services.mockResolvedValue({ success: true, services: [{ id: "cache", name: "redis", kind: "compose" }] }); await mountProject(framework); expect(host.querySelector('nav a[href$="/advanced"]')).not.toBeNull(); expect(host.textContent).toContain("Project environment"); @@ -400,15 +399,21 @@ describe("permanent configuration ownership", () => { const saved = { id: "project", name: "Test", slug: "test", description: "", framework: "nextjs", runtimeMode: "bare", ...options, port: 3000 }; - it.each([false, true])("keeps project limits and the shared editor in Settings with companions=%s", async (companions) => { + it.each([false, true])("opens the correct environment scope from Settings with companions=%s", async (companions) => { if (companions) api.services.mockResolvedValue({ success: true, services: [{ id: "db", name: "postgres", kind: "compose" }] }); await mountProject("nextjs", , { options }); expect(host.querySelectorAll('section[aria-label="Machine power"]')).toHaveLength(1); expect(host.textContent).toContain("Project environment"); - expect(host.querySelector('a[href$="/application/env"]')).toBeNull(); expect(host.querySelector('a[href$="/application/settings"]')).not.toBeNull(); - await act(async () => button("Edit").click()); - expect(document.querySelector('[data-modal-surface] input[value="BUILD_VALUE"]')).not.toBeNull(); + if (companions) { + expect(host.querySelector('a[href$="/application/env"]')).toBeNull(); + await act(async () => button("Edit").click()); + expect(document.querySelector('[data-modal-surface] input[value="BUILD_VALUE"]')).not.toBeNull(); + } else { + expect(host.querySelector('a[href="/projects/project/services/application/env"]')?.textContent).toBe("Environment"); + expect(document.querySelector('[data-modal-surface]')).toBeNull(); + expect(api.getEnv).not.toHaveBeenCalled(); + } }); it("saves only edited app configuration inline without touching environment or deploying", async () => { diff --git a/apps/dashboard/src/components/migration/PlatformImportDomains.tsx b/apps/dashboard/src/components/migration/PlatformImportDomains.tsx new file mode 100644 index 000000000..63257c72b --- /dev/null +++ b/apps/dashboard/src/components/migration/PlatformImportDomains.tsx @@ -0,0 +1,69 @@ +"use client"; + +import type { PlatformImportService } from "@repo/contracts"; +import { useI18n } from "@/components/i18n-provider"; + +export type ImportDomainService = Pick; + +/** Discovery and saved runs share the same public domain inventory. */ +export function PlatformImportDomains({ + services, + cutover = false, +}: { + services: ImportDomainService[]; + cutover?: boolean; +}) { + const { t } = useI18n(); + const copy = t.migration.platformDomains; + const domains = services.flatMap((service) => + (service.domains ?? []).map((domain) => ({ ...domain, service })), + ); + if (!cutover && !domains.length) return null; + + return ( +
+
+

{copy.title}

+

{copy.intro}

+
+ {domains.length > 0 ? ( +
    + {domains.map(({ hostname, portable, service }) => ( +
  • +
    +

    + {hostname} +

    +

    {service.name}

    +
    + + {portable ? copy.customAction : copy.providerAction} + +
  • + ))} +
+ ) : ( +

{copy.empty}

+ )} + {!cutover && domains.some((domain) => domain.portable) && ( +

{copy.customHint}

+ )} + {domains.some((domain) => !domain.portable) && ( +

{copy.providerHint}

+ )} + {cutover && domains.length > 0 && ( +
+

{copy.next}

+
    +
  1. {copy.test}
  2. + {domains.some((domain) => domain.portable) &&
  3. {copy.dns}
  4. } +
  5. {copy.verify}
  6. +
+
+ )} +
+ ); +} diff --git a/apps/dashboard/src/components/migration/PlatformImportProgress.tsx b/apps/dashboard/src/components/migration/PlatformImportProgress.tsx index 1f6361e11..6c7efe969 100644 --- a/apps/dashboard/src/components/migration/PlatformImportProgress.tsx +++ b/apps/dashboard/src/components/migration/PlatformImportProgress.tsx @@ -9,6 +9,7 @@ import { InstallStepper, type StepStatus } from "@/components/deploy/InstallStep import { useI18n } from "@/components/i18n-provider"; import { formatBytes } from "@/lib/formatBytes"; import { statusTone } from "./MigrationRunList"; +import { PlatformImportDomains, type ImportDomainService } from "./PlatformImportDomains"; export const importFinished = (status: string) => ["failed", "rolled_back", "succeeded"].includes(status); @@ -48,7 +49,10 @@ export function PlatformImportProgress({ run.status, ); const stopped = run.status === "rolled_back" && run.cancelRequested === true; - const projects = Object.entries(run.importedProjects ?? {}); + const services = (run.inputSnapshot?.services ?? []) as ImportDomainService[]; + const projects = [ + ...new Map(Object.entries(run.importedProjects ?? {}).map(([source, id]) => [id, source])), + ].map(([id, source]) => ({ id, source })); const steps: Array<{ id: string; label: string; status: StepStatus }> = [ { id: "import", @@ -77,7 +81,9 @@ export function PlatformImportProgress({
-

Imported projects

+

+ {projects.length > 1 ? "Imported projects" : "Imported project"} +

@@ -104,47 +110,37 @@ export function PlatformImportProgress({

)}
- {projects.map(([source, id], index) => ( - - - - {( - run.inputSnapshot?.services as Array<{ id: string; name: string }> | undefined - )?.find((s) => s.id === source)?.name ?? `Project ${index + 1}`} - - Open project - - + {projects.map(({ source, id }, index) => ( +
+ + + + {projects.length === 1 + ? run.projectName + : (services.find((s) => s.id === source)?.name ?? `Project ${index + 1}`)} + + Open project + + + {run.status === "awaiting_cutover" && ( + + )} +
))} {!projects.length && (

- Created projects will appear here as the import progresses. + The imported project will appear here as the import progresses.

)}
- {run.status === "awaiting_cutover" && ( -
-
-

Verify before moving traffic

-

- Open each project, check its data, and add a test domain in Domains & Routes. - Then move your existing domains and confirm below. -

-
-

- Keep source writers paused until cutover. Retire the old services at the provider - after verifying the destination. -

- -
- )} {run.status === "succeeded" && (

Migration complete. Your source resources remain at the provider until you retire @@ -163,6 +159,24 @@ export function PlatformImportProgress({

)}
+ {run.status === "awaiting_cutover" && ( + <> + run.importedProjects?.[service.id])} + cutover + /> +
+

+ Keep source writers paused until cutover. Retire the old services at the provider + after verifying the destination. +

+ +
+ + )} {run.deploymentId && ( + ({ + ...service, + name: choices[service.id].name, + }))} + /> {(plan.requiresWritePause || plan.requiresWorkerPause) && (
@@ -154,7 +161,7 @@ export function PlatformImportReview({

Review complete

- {imports.length} new {imports.length === 1 ? "project" : "projects"} + One project · {imports.length} {imports.length === 1 ? "service" : "services"}

- {busy - ? "Starting import…" - : `Import ${imports.length} ${imports.length === 1 ? "project" : "projects"}`} + {busy ? "Starting import…" : "Import project"}

- Each selected service becomes a project. Configure test domains after import, then move - production traffic. + Services stay together in one project on its private network. Configure test domains after + import, then move production traffic.

@@ -252,7 +278,7 @@ function ImportServiceCard({