[REFACTOR] Workspace version only x.y.z (#10910)

# Introduction
We want the APP_VERSION to be able to contains pre-release options, in a
nutshell to be semVer compatible.
But we want to have workspace, at least for the moment, that only store
`x.y.z` and not `vx.y.z` or `x.y.z-alpha` version in database

Explaining this refactor

Related https://github.com/twentyhq/twenty/pull/10907
This commit is contained in:
Paul Rastoin
2025-03-14 19:21:44 +01:00
committed by GitHub
parent 1aeef2b68e
commit 23b4605987
6 changed files with 149 additions and 21 deletions

View File

@ -2,7 +2,7 @@
exports[`UpgradeCommandRunner Workspace upgrade should fail when APP_VERSION is not defined 1`] = `[Error: Cannot run upgrade command when APP_VERSION is not defined, please double check your env variables]`;
exports[`UpgradeCommandRunner Workspace upgrade should fail when workspace version is not defined 1`] = `[Error: WORKSPACE_VERSION_NOT_DEFINED to=2.0.0]`;
exports[`UpgradeCommandRunner Workspace upgrade should fail when workspace version is not defined 1`] = `[Error: WORKSPACE_VERSION_NOT_DEFINED workspace=workspace_0]`;
exports[`UpgradeCommandRunner Workspace upgrade should fail when workspace version is not equal to fromVersion 1`] = `[Error: WORKSPACE_VERSION_MISSMATCH Upgrade for workspace workspace_0 failed as its version is beneath fromWorkspaceVersion=2.0.0]`;
@ -10,4 +10,4 @@ exports[`UpgradeCommandRunner should run upgrade command with failing and succes
exports[`UpgradeCommandRunner should run upgrade command with failing and successful workspaces 2`] = `[Error: Received invalid version: invalid 1.0.0]`;
exports[`UpgradeCommandRunner should run upgrade command with failing and successful workspaces 3`] = `[Error: WORKSPACE_VERSION_NOT_DEFINED to=2.0.0]`;
exports[`UpgradeCommandRunner should run upgrade command with failing and successful workspaces 3`] = `[Error: WORKSPACE_VERSION_NOT_DEFINED workspace=null_version_workspace]`;

View File

@ -248,7 +248,8 @@ describe('UpgradeCommandRunner', () => {
nullVersionWorkspace,
];
const totalWorkspace = numberOfValidWorkspace + failingWorkspaces.length;
const appVersion = '2.0.0';
const appVersion = 'v2.0.0';
const expectedToVersion = '2.0.0';
await buildModuleAndSetupSpies({
numberOfWorkspace: numberOfValidWorkspace,
@ -280,7 +281,7 @@ describe('UpgradeCommandRunner', () => {
expect(workspaceRepository.update).toHaveBeenNthCalledWith(
numberOfValidWorkspace,
{ id: expect.any(String) },
{ version: appVersion },
{ version: expectedToVersion },
);
// Failing assertions

View File

@ -17,11 +17,7 @@ import {
CompareVersionMajorAndMinorReturnType,
compareVersionMajorAndMinor,
} from 'src/utils/version/compare-version-minor-and-major';
type ValidateWorkspaceVersionEqualsWorkspaceFromVersionOrThrowArgs = {
workspaceId: string;
appVersion: string | undefined;
};
import { extractVersionMajorMinorPatch } from 'src/utils/version/extract-version-major-minor-patch';
export abstract class UpgradeCommandRunner extends ActiveOrSuspendedWorkspacesMigrationCommandRunner {
abstract readonly fromWorkspaceVersion: SemVer;
@ -39,19 +35,18 @@ export abstract class UpgradeCommandRunner extends ActiveOrSuspendedWorkspacesMi
override async runOnWorkspace(args: RunOnWorkspaceArgs): Promise<void> {
const { workspaceId, index, total, options } = args;
const appVersion = this.environmentService.get('APP_VERSION');
this.logger.log(
chalk.blue(
`${options.dryRun ? '(dry run)' : ''} Upgrading workspace ${workspaceId} ${index + 1}/${total}`,
),
);
const toVersion = this.retrieveToVersionFromAppVersion();
const workspaceVersionCompareResult =
await this.retrieveWorkspaceVersionAndCompareToWorkspaceFromVersion({
appVersion,
await this.retrieveWorkspaceVersionAndCompareToWorkspaceFromVersion(
workspaceId,
});
);
switch (workspaceVersionCompareResult) {
case 'lower': {
@ -66,7 +61,7 @@ export abstract class UpgradeCommandRunner extends ActiveOrSuspendedWorkspacesMi
await this.workspaceRepository.update(
{ id: workspaceId },
{ version: appVersion },
{ version: toVersion },
);
this.logger.log(
chalk.blue(`Upgrade for workspace ${workspaceId} completed.`),
@ -91,16 +86,29 @@ export abstract class UpgradeCommandRunner extends ActiveOrSuspendedWorkspacesMi
}
}
private async retrieveWorkspaceVersionAndCompareToWorkspaceFromVersion({
appVersion,
workspaceId,
}: ValidateWorkspaceVersionEqualsWorkspaceFromVersionOrThrowArgs): Promise<CompareVersionMajorAndMinorReturnType> {
private retrieveToVersionFromAppVersion() {
const appVersion = this.environmentService.get('APP_VERSION');
if (!isDefined(appVersion)) {
throw new Error(
'Cannot run upgrade command when APP_VERSION is not defined, please double check your env variables',
);
}
const parsedVersion = extractVersionMajorMinorPatch(appVersion);
if (!isDefined(parsedVersion)) {
throw new Error(
`Should never occur, APP_VERSION is invalid ${parsedVersion}`,
);
}
return parsedVersion;
}
private async retrieveWorkspaceVersionAndCompareToWorkspaceFromVersion(
workspaceId: string,
): Promise<CompareVersionMajorAndMinorReturnType> {
// TODO remove after first release has been done using workspace_version
if (!isDefined(this.VALIDATE_WORKSPACE_VERSION_FEATURE_FLAG)) {
this.logger.warn(
@ -116,7 +124,7 @@ export abstract class UpgradeCommandRunner extends ActiveOrSuspendedWorkspacesMi
const currentWorkspaceVersion = workspace.version;
if (!isDefined(currentWorkspaceVersion)) {
throw new Error(`WORKSPACE_VERSION_NOT_DEFINED to=${appVersion}`);
throw new Error(`WORKSPACE_VERSION_NOT_DEFINED workspace=${workspaceId}`);
}
return compareVersionMajorAndMinor(