Prevent cross version upgrade mismatch in 1.22 (#19627)

## Introduction
As the new upgrade sequence engine is released in `1.22` it requires all
workspaces to be in `1.21.0` which mean they will have a cursor on the
sequence

As if if someone upgrades from `1.20` to `1.22` no `upgradeMigration`
will exist and throw a pretty basic `Could not find any cursor, database
might not been initialized correctly`

Here we allow a meaningful error
This commit is contained in:
Paul Rastoin
2026-04-13 16:53:12 +02:00
committed by GitHub
parent 7dfc556250
commit 87f5c0083f
8 changed files with 153 additions and 79 deletions
@@ -2,7 +2,8 @@ import { InjectDataSource } from '@nestjs/typeorm';
import chalk from 'chalk';
import { Command, CommandRunner, Option } from 'nest-commander';
import { DataSource } from 'typeorm';
import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
import { DataSource, In } from 'typeorm';
import { CommandLogger } from 'src/database/commands/logger';
import { UpgradeCommandRegistryService } from 'src/engine/core-modules/upgrade/services/upgrade-command-registry.service';
@@ -10,7 +11,9 @@ import { UpgradeMigrationService } from 'src/engine/core-modules/upgrade/service
import { UpgradeSequenceReaderService } from 'src/engine/core-modules/upgrade/services/upgrade-sequence-reader.service';
import { UpgradeSequenceRunnerService } from 'src/engine/core-modules/upgrade/services/upgrade-sequence-runner.service';
import { RemovedSinceVersion } from 'src/engine/core-modules/upgrade/types/removed-since-version.type';
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
import { WorkspaceVersionService } from 'src/engine/workspace-manager/workspace-version/services/workspace-version.service';
import { compareVersionMajorAndMinor } from 'src/utils/version/compare-version-minor-and-major';
import { isDefined } from 'twenty-shared/utils';
type RawUpgradeCommandOptions = {
@@ -127,6 +130,7 @@ export class UpgradeCommand extends CommandRunner {
try {
await this.runBootstrapMigrations();
await this.guardAllActiveOrSuspendedWorkspacesAreIn1_21_0();
await this.backfillWorkspaceCreatedIn1_21_0Cursors();
const sequence = this.upgradeSequenceReaderService.getUpgradeSequence();
@@ -172,6 +176,50 @@ export class UpgradeCommand extends CommandRunner {
}
}
private async guardAllActiveOrSuspendedWorkspacesAreIn1_21_0(): RemovedSinceVersion<
'1.23.0',
Promise<void>
> {
const MINIMUM_VERSION = '1.21.0';
const activeOrSuspendedWorkspaces = await this.dataSource
.getRepository(WorkspaceEntity)
.find({
select: {
version: true,
id: true,
},
where: {
activationStatus: In([
WorkspaceActivationStatus.ACTIVE,
WorkspaceActivationStatus.SUSPENDED,
]),
},
});
const workspacesBelowMinimum = activeOrSuspendedWorkspaces.filter(
(workspace) =>
!isDefined(workspace.version) ||
compareVersionMajorAndMinor(workspace.version, MINIMUM_VERSION) ===
'lower',
);
if (workspacesBelowMinimum.length > 0) {
const listing = workspacesBelowMinimum
.map(
(workspace) =>
` - ${workspace.id} (version: ${workspace.version ?? 'null'})`,
)
.join('\n');
throw new Error(
`Cannot upgrade: ${workspacesBelowMinimum.length} workspace(s) have a version below ${MINIMUM_VERSION}.\n` +
`All workspaces must be upgraded to at least ${MINIMUM_VERSION} before running the 1.22.0 upgrade.\n` +
`Affected workspaces:\n${listing}`,
);
}
}
// Workspaces created during 1.21 were activated before the cursor-based
// upgrade system existed. They have no upgradeMigration record yet.
// Stamp them with the last 1.21 workspace command as their initial cursor.