Skip to content

The open alternative to Salesforce, designed for AI.

fix(upgrade): stop dry runs from executing instance commands (#26693)

Weikoacbef08e2100
diff --git a/packages/twenty-server/docs/UPGRADE_COMMANDS.md b/packages/twenty-server/docs/UPGRADE_COMMANDS.md
index ee80b1f7ced..9914396c910 100644
--- a/packages/twenty-server/docs/UPGRADE_COMMANDS.md
+++ b/packages/twenty-server/docs/UPGRADE_COMMANDS.md
@@ -146,6 +146,12 @@ Within a given version of Twenty, the upgrade pipeline runs commands in this ord
Workspace commands are executed sequentially across all active/suspended workspaces.
+## Dry run
+
+`upgrade --dry-run` stops as soon as it reaches an instance command, logging `Dry run stopped before instance step "<name>"`. It does not execute that command or any later step, and records nothing in `upgradeMigration`.
+
+If the run starts within a workspace segment, pending workspace commands run with `options.dryRun` set until the next instance command or the end of the sequence. Each workspace command is responsible for simulating its own changes. Dry runs cannot preview the full upgrade sequence because later workspace commands may depend on schema changes made by preceding instance commands.
+
## Interrupting a run (Ctrl+C, SIGTERM)
Ctrl+C during an `upgrade` stops it gracefully: the workspace being processed finishes its commands, then the run stops instead of starting the next one. Ctrl+C again forces an immediate exit, leaving the command in progress unfinished.
diff --git a/packages/twenty-server/src/engine/core-modules/upgrade/services/upgrade-sequence-runner.service.ts b/packages/twenty-server/src/engine/core-modules/upgrade/services/upgrade-sequence-runner.service.ts
index a7d938de903..4f3aec56cc2 100644
--- a/packages/twenty-server/src/engine/core-modules/upgrade/services/upgrade-sequence-runner.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/upgrade/services/upgrade-sequence-runner.service.ts
@@ -139,6 +139,23 @@ export class UpgradeSequenceRunnerService {
break;
}
+ if (options.dryRun) {
+ this.logger.log(
+ formatUpgradeLog({
+ humanMessage:
+ `Dry run stopped before instance step "${step.name}": ` +
+ 'instance commands cannot run in dry-run mode.',
+ event: 'sequence.stopped',
+ logFields: {
+ before: step.name,
+ reason: 'dry-run',
+ },
+ }),
+ );
+
+ break;
+ }
+
const previousStep = cursor > 0 ? sequence[cursor - 1] : undefined;
if (previousStep?.kind === 'workspace') {
diff --git a/packages/twenty-server/test/integration/upgrade/suites/sequence-runner/dry-run-sequence-runner.integration-spec.ts b/packages/twenty-server/test/integration/upgrade/suites/sequence-runner/dry-run-sequence-runner.integration-spec.ts
new file mode 100644
index 00000000000..39235fe30a1
--- /dev/null
+++ b/packages/twenty-server/test/integration/upgrade/suites/sequence-runner/dry-run-sequence-runner.integration-spec.ts
@@ -0,0 +1,246 @@
+import { InstanceCommandRunnerService } from 'src/engine/core-modules/upgrade/services/instance-command-runner.service';
+
+import {
+ type IntegrationTestContext,
+ createUpgradeSequenceRunnerIntegrationTestModule,
+ DEFAULT_OPTIONS,
+ makeFastInstance,
+ makeSlowInstance,
+ makeWorkspace,
+ migrationRecordToKey,
+ resetSeedSequenceCounter,
+ restoreUpgradeMigrations,
+ seedInstanceMigration,
+ seedWorkspaceMigration,
+ setMockActiveWorkspaceIds,
+ snapshotUpgradeMigrations,
+ testGetExecutedMigrationsInOrder,
+ WS_1,
+ WS_2,
+} from 'test/integration/upgrade/utils/upgrade-sequence-runner-integration-test.util';
+
+const DRY_RUN_OPTIONS = { ...DEFAULT_OPTIONS, dryRun: true };
+
+const getDryRunHumanMessages = (logSpy: jest.SpyInstance): string[] =>
+ logSpy.mock.calls
+ .map(([message]) => String(message).split('\n')[0])
+ .filter((humanMessage) => humanMessage.startsWith('Dry run'));
+
+describe('UpgradeSequenceRunnerService - dry run (integration)', () => {
+ let context: IntegrationTestContext;
+ let savedUpgradeMigrations: Awaited<
+ ReturnType<typeof snapshotUpgradeMigrations>
+ >;
+
+ beforeAll(async () => {
+ context = await createUpgradeSequenceRunnerIntegrationTestModule();
+ savedUpgradeMigrations = await snapshotUpgradeMigrations(
+ context.dataSource,
+ );
+ }, 30000);
+
+ afterAll(async () => {
+ await restoreUpgradeMigrations(context.dataSource, savedUpgradeMigrations);
+ await context.module?.close();
+ await context.dataSource?.destroy();
+ }, 15000);
+
+ beforeEach(async () => {
+ await context.dataSource.query('DELETE FROM core."upgradeMigration"');
+ resetSeedSequenceCounter();
+ setMockActiveWorkspaceIds([]);
+ jest.restoreAllMocks();
+ });
+
+ const getMigrationKeys = async () =>
+ (await testGetExecutedMigrationsInOrder(context.dataSource)).map(
+ migrationRecordToKey,
+ );
+
+ const spyOnInstanceCommandRunner = () => {
+ const instanceCommandRunnerService = context.module.get(
+ InstanceCommandRunnerService,
+ );
+
+ return {
+ runFastInstanceCommandSpy: jest.spyOn(
+ instanceCommandRunnerService,
+ 'runFastInstanceCommand',
+ ),
+ runSlowInstanceCommandSpy: jest.spyOn(
+ instanceCommandRunnerService,
+ 'runSlowInstanceCommand',
+ ),
+ };
+ };
+
+ it.each([
+ ['fast', makeFastInstance],
+ ['slow', makeSlowInstance],
+ ] as const)(
+ 'should stop before a pending %s instance command without running later workspace commands',
+ async (_, makeInstance) => {
+ const pendingWorkspaceCommand = makeWorkspace('Wc1');
+ const sequence = [
+ makeFastInstance('Ic1'),
+ makeInstance('Ic2'),
+ pendingWorkspaceCommand,
+ ];
+
+ setMockActiveWorkspaceIds([WS_1, WS_2]);
+
+ await seedInstanceMigration(context.dataSource, {
+ name: 'Ic1',
+ status: 'completed',
+ workspaceIds: [WS_1, WS_2],
+ });
+
+ const { runFastInstanceCommandSpy, runSlowInstanceCommandSpy } =
+ spyOnInstanceCommandRunner();
+ const pendingRunOnWorkspaceSpy = jest.spyOn(
+ pendingWorkspaceCommand.command,
+ 'runOnWorkspace',
+ );
+ const logSpy = jest
+ .spyOn(context.runner['logger'], 'log')
+ .mockImplementation();
+ const migrationKeysBeforeDryRun = await getMigrationKeys();
+
+ const report = await context.runner.run({
+ sequence,
+ options: DRY_RUN_OPTIONS,
+ });
+
+ expect(report).toEqual({ totalSuccesses: 0, totalFailures: 0 });
+ expect(runFastInstanceCommandSpy).not.toHaveBeenCalled();
+ expect(runSlowInstanceCommandSpy).not.toHaveBeenCalled();
+ expect(pendingRunOnWorkspaceSpy).not.toHaveBeenCalled();
+ expect(getDryRunHumanMessages(logSpy)).toStrictEqual([
+ 'Dry run stopped before instance step "Ic2": instance commands cannot run in dry-run mode.',
+ ]);
+ expect(await getMigrationKeys()).toStrictEqual(migrationKeysBeforeDryRun);
+ },
+ );
+
+ it.each(['completed', 'failed'] as const)(
+ 'should stop before the next instance command when the current workspace segment was %s',
+ async (status) => {
+ const currentWorkspaceCommand = makeWorkspace('Wc0');
+ const nextVersionWorkspaceCommand = makeWorkspace('Wc1');
+ const sequence = [
+ makeFastInstance('Ic0'),
+ currentWorkspaceCommand,
+ makeFastInstance('Ic1'),
+ nextVersionWorkspaceCommand,
+ ];
+
+ setMockActiveWorkspaceIds([WS_1, WS_2]);
+
+ await seedInstanceMigration(context.dataSource, {
+ name: 'Ic0',
+ status: 'completed',
+ workspaceIds: [WS_1, WS_2],
+ });
+ await seedWorkspaceMigration(context.dataSource, {
+ name: 'Wc0',
+ status,
+ workspaceId: WS_1,
+ });
+ await seedWorkspaceMigration(context.dataSource, {
+ name: 'Wc0',
+ status,
+ workspaceId: WS_2,
+ });
+
+ const { runFastInstanceCommandSpy, runSlowInstanceCommandSpy } =
+ spyOnInstanceCommandRunner();
+ const currentRunOnWorkspaceSpy = jest.spyOn(
+ currentWorkspaceCommand.command,
+ 'runOnWorkspace',
+ );
+ const nextVersionRunOnWorkspaceSpy = jest.spyOn(
+ nextVersionWorkspaceCommand.command,
+ 'runOnWorkspace',
+ );
+ const logSpy = jest
+ .spyOn(context.runner['logger'], 'log')
+ .mockImplementation();
+ const migrationKeysBeforeDryRun = await getMigrationKeys();
+
+ const report = await context.runner.run({
+ sequence,
+ options: DRY_RUN_OPTIONS,
+ });
+
+ expect(report).toEqual({ totalSuccesses: 2, totalFailures: 0 });
+ expect(runFastInstanceCommandSpy).not.toHaveBeenCalled();
+ expect(runSlowInstanceCommandSpy).not.toHaveBeenCalled();
+ if (status === 'completed') {
+ expect(currentRunOnWorkspaceSpy).not.toHaveBeenCalled();
+ } else {
+ expect(currentRunOnWorkspaceSpy).toHaveBeenCalledTimes(2);
+ expect(currentRunOnWorkspaceSpy).toHaveBeenCalledWith(
+ expect.objectContaining({
+ options: expect.objectContaining({ dryRun: true }),
+ }),
+ );
+ }
+ expect(nextVersionRunOnWorkspaceSpy).not.toHaveBeenCalled();
+ expect(getDryRunHumanMessages(logSpy)).toStrictEqual([
+ 'Dry run stopped before instance step "Ic1": instance commands cannot run in dry-run mode.',
+ ]);
+ expect(await getMigrationKeys()).toStrictEqual(migrationKeysBeforeDryRun);
+ },
+ );
+
+ it('should stop before checking the workspace barrier when retrying a failed instance command', async () => {
+ const pendingWorkspaceCommand = makeWorkspace('Wc1');
+ const sequence = [
+ makeWorkspace('Wc0a'),
+ makeWorkspace('Wc0b'),
+ makeFastInstance('Ic1'),
+ pendingWorkspaceCommand,
+ ];
+
+ setMockActiveWorkspaceIds([WS_1, WS_2]);
+
+ await seedWorkspaceMigration(context.dataSource, {
+ name: 'Wc0a',
+ status: 'completed',
+ workspaceId: WS_1,
+ });
+ await seedWorkspaceMigration(context.dataSource, {
+ name: 'Wc0a',
+ status: 'completed',
+ workspaceId: WS_2,
+ });
+ await seedWorkspaceMigration(context.dataSource, {
+ name: 'Wc0b',
+ status: 'completed',
+ workspaceId: WS_2,
+ });
+ await seedInstanceMigration(context.dataSource, {
+ name: 'Ic1',
+ status: 'failed',
+ workspaceIds: [WS_2],
+ });
+
+ const { runFastInstanceCommandSpy } = spyOnInstanceCommandRunner();
+ const pendingRunOnWorkspaceSpy = jest.spyOn(
+ pendingWorkspaceCommand.command,
+ 'runOnWorkspace',
+ );
+ const migrationKeysBeforeDryRun = await getMigrationKeys();
+
+ await expect(
+ context.runner.run({
+ sequence,
+ options: DRY_RUN_OPTIONS,
+ }),
+ ).resolves.toEqual({ totalSuccesses: 0, totalFailures: 0 });
+
+ expect(runFastInstanceCommandSpy).not.toHaveBeenCalled();
+ expect(pendingRunOnWorkspaceSpy).not.toHaveBeenCalled();
+ expect(await getMigrationKeys()).toStrictEqual(migrationKeysBeforeDryRun);
+ });
+});