Upgrade infer commands from APP_VERSION (#11881)
# Introduction This PR refactors the way we previously manually handled the upgrade command `versionTo` and `versionFrom` values to be replaced by a programmatic inferring using the `APP_VERSION` env variable. It raises new invariant edge cases that are covered by new tests and so on Please keep in mind that an upgrade will run agnostically of any `patch` semver value as it should be done only when releasing a `major/minor` version update [Related discord thread](https://discord.com/channels/1130383047699738754/1368953221921505280) ## Testing in local In order to test in local we have to define an `APP_VERSION` value in `packages/twenty-server/.env` following semver ( or not 🙃 ) ## Logs example ```ts Computing new Datasource for cacheKey: 20202020-1c25-4d02-bf25-6aeccf7ea419-8 out of 0 query: SELECT * FROM current_schema() query: SELECT version(); [Nest] 37872 - 05/06/2025, 4:07:21 PM LOG [UpgradeCommand] Initialized upgrade context with: - currentVersion (migrating to): 0.53.0 - fromWorkspaceVersion: 0.52.0 - 2 commands [Nest] 37872 - 05/06/2025, 4:07:21 PM LOG [UpgradeCommand] Upgrading workspace 20202020-1c25-4d02-bf25-6aeccf7ea419 from=0.52.0 to=0.53.0 1/2 [Nest] 37872 - 05/06/2025, 4:07:21 PM LOG [UpgradeCommand] Upgrade for workspace 20202020-1c25-4d02-bf25-6aeccf7ea419 ignored as is already at a higher version. [Nest] 37872 - 05/06/2025, 4:07:21 PM LOG [UpgradeCommand] Running command on workspace 3b8e6458-5fc1-4e63-8563-008ccddaa6db 2/2 Computing new Datasource for cacheKey: 3b8e6458-5fc1-4e63-8563-008ccddaa6db-8 out of 0 query: SELECT * FROM current_schema() query: SELECT version(); [Nest] 37872 - 05/06/2025, 4:07:21 PM LOG [UpgradeCommand] Upgrading workspace 3b8e6458-5fc1-4e63-8563-008ccddaa6db from=0.52.0 to=0.53.0 2/2 [Nest] 37872 - 05/06/2025, 4:07:21 PM LOG [UpgradeCommand] Upgrade for workspace 3b8e6458-5fc1-4e63-8563-008ccddaa6db ignored as is already at a higher version. [Nest] 37872 - 05/06/2025, 4:07:21 PM LOG [UpgradeCommand] Command completed! ``` ## Misc Related to https://github.com/twentyhq/twenty/issues/11780
This commit is contained in:
@ -2,9 +2,15 @@
|
||||
|
||||
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 all commands contains invalid semver keys 1`] = `[Error: No previous version found for version 2.0.0. Please review the "allCommands" record. Available versions are: invalid, 2.0.0]`;
|
||||
|
||||
exports[`UpgradeCommandRunner Workspace upgrade should fail when current version commands are not found 1`] = `[Error: No command found for version 42.0.0. Please check the commands record.]`;
|
||||
|
||||
exports[`UpgradeCommandRunner Workspace upgrade should fail when previous version is not found 1`] = `[Error: No previous version found for version 1.0.0. Please review the "allCommands" record. Available versions are: 1.0.0, 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]`;
|
||||
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=1.0.0]`;
|
||||
|
||||
exports[`UpgradeCommandRunner should run upgrade command with failing and successful workspaces 1`] = `[Error: WORKSPACE_VERSION_MISSMATCH Upgrade for workspace outated_version_workspace failed as its version is beneath fromWorkspaceVersion=1.0.0]`;
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
||||
|
||||
import { SemVer } from 'semver';
|
||||
import { EachTestingContext } from 'twenty-shared/testing';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
@ -12,46 +11,35 @@ import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
|
||||
import { SyncWorkspaceMetadataCommand } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/sync-workspace-metadata.command';
|
||||
|
||||
class TestUpgradeCommandRunnerV1 extends UpgradeCommandRunner {
|
||||
fromWorkspaceVersion = new SemVer('1.0.0');
|
||||
|
||||
public override async runBeforeSyncMetadata(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
public override async runAfterSyncMetadata(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
class BasicUpgradeCommandRunner extends UpgradeCommandRunner {
|
||||
allCommands = {
|
||||
'1.0.0': {
|
||||
beforeSyncMetadata: [],
|
||||
afterSyncMetadata: [],
|
||||
},
|
||||
'2.0.0': {
|
||||
beforeSyncMetadata: [],
|
||||
afterSyncMetadata: [],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
class InvalidVersionUpgradeCommandRunner extends UpgradeCommandRunner {
|
||||
fromWorkspaceVersion = new SemVer('invalid');
|
||||
|
||||
protected async runBeforeSyncMetadata(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
protected async runAfterSyncMetadata(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
class TestUpgradeCommandRunnerV2 extends UpgradeCommandRunner {
|
||||
fromWorkspaceVersion = new SemVer('2.0.0');
|
||||
|
||||
protected async runBeforeSyncMetadata(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
protected async runAfterSyncMetadata(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
class InvalidUpgradeCommandRunner extends UpgradeCommandRunner {
|
||||
allCommands = {
|
||||
invalid: {
|
||||
beforeSyncMetadata: [],
|
||||
afterSyncMetadata: [],
|
||||
},
|
||||
'2.0.0': {
|
||||
beforeSyncMetadata: [],
|
||||
afterSyncMetadata: [],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
type CommandRunnerValues =
|
||||
| typeof TestUpgradeCommandRunnerV1
|
||||
| typeof TestUpgradeCommandRunnerV2
|
||||
| typeof InvalidVersionUpgradeCommandRunner;
|
||||
| typeof BasicUpgradeCommandRunner
|
||||
| typeof InvalidUpgradeCommandRunner;
|
||||
|
||||
const generateMockWorkspace = (overrides?: Partial<Workspace>) =>
|
||||
({
|
||||
@ -132,7 +120,7 @@ const buildUpgradeCommandModule = async ({
|
||||
};
|
||||
|
||||
describe('UpgradeCommandRunner', () => {
|
||||
let upgradeCommandRunner: TestUpgradeCommandRunnerV1;
|
||||
let upgradeCommandRunner: BasicUpgradeCommandRunner;
|
||||
let workspaceRepository: Repository<Workspace>;
|
||||
let syncWorkspaceMetadataCommand: jest.Mocked<SyncWorkspaceMetadataCommand>;
|
||||
let runAfterSyncMetadataSpy: jest.SpyInstance;
|
||||
@ -150,7 +138,7 @@ describe('UpgradeCommandRunner', () => {
|
||||
numberOfWorkspace = 1,
|
||||
workspaceOverride,
|
||||
workspaces,
|
||||
commandRunner = TestUpgradeCommandRunnerV1,
|
||||
commandRunner = BasicUpgradeCommandRunner,
|
||||
appVersion = '2.0.0',
|
||||
}: BuildModuleAndSetupSpiesArgs) => {
|
||||
const generatedWorkspaces = Array.from(
|
||||
@ -347,6 +335,70 @@ describe('UpgradeCommandRunner', () => {
|
||||
expect(upgradeCommandRunner.migrationReport.fail.length).toBe(0);
|
||||
});
|
||||
|
||||
describe('Workspace upgrade should succeed ', () => {
|
||||
const successfulTestUseCases: EachTestingContext<{
|
||||
input: Omit<BuildModuleAndSetupSpiesArgs, 'numberOfWorkspace'>;
|
||||
}>[] = [
|
||||
{
|
||||
title: 'even if workspace version and app version differ in patch',
|
||||
context: {
|
||||
input: {
|
||||
appVersion: 'v2.0.0',
|
||||
workspaceOverride: {
|
||||
version: 'v1.0.12',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
title:
|
||||
'even if workspace version and app version differ in patch and semantic',
|
||||
context: {
|
||||
input: {
|
||||
appVersion: 'v2.0.0',
|
||||
workspaceOverride: {
|
||||
version: '1.0.12',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'even if app version contains a patch value',
|
||||
context: {
|
||||
input: {
|
||||
appVersion: '2.0.24',
|
||||
workspaceOverride: {
|
||||
version: '1.0.12',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
it.each(successfulTestUseCases)(
|
||||
'$title',
|
||||
async ({ context: { input } }) => {
|
||||
await buildModuleAndSetupSpies(input);
|
||||
|
||||
const passedParams = [];
|
||||
const options = {};
|
||||
|
||||
await upgradeCommandRunner.run(passedParams, options);
|
||||
|
||||
const { fail: failReport, success: successReport } =
|
||||
upgradeCommandRunner.migrationReport;
|
||||
|
||||
expect(failReport.length).toBe(0);
|
||||
expect(successReport.length).toBe(1);
|
||||
expect(runAfterSyncMetadataSpy).toBeCalledTimes(1);
|
||||
expect(runBeforeSyncMetadataSpy).toBeCalledTimes(1);
|
||||
const { workspaceId } = successReport[0];
|
||||
|
||||
expect(workspaceId).toBe('workspace_0');
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('Workspace upgrade should fail', () => {
|
||||
const failingTestUseCases: EachTestingContext<{
|
||||
input: Omit<BuildModuleAndSetupSpiesArgs, 'numberOfWorkspace'>;
|
||||
@ -355,8 +407,7 @@ describe('UpgradeCommandRunner', () => {
|
||||
title: 'when workspace version is not equal to fromVersion',
|
||||
context: {
|
||||
input: {
|
||||
appVersion: '3.0.0',
|
||||
commandRunner: TestUpgradeCommandRunnerV2,
|
||||
appVersion: '2.0.0',
|
||||
workspaceOverride: {
|
||||
version: '0.1.0',
|
||||
},
|
||||
@ -381,6 +432,30 @@ describe('UpgradeCommandRunner', () => {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'when current version commands are not found',
|
||||
context: {
|
||||
input: {
|
||||
appVersion: '42.0.0',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'when previous version is not found',
|
||||
context: {
|
||||
input: {
|
||||
appVersion: '1.0.0',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'when all commands contains invalid semver keys',
|
||||
context: {
|
||||
input: {
|
||||
commandRunner: InvalidUpgradeCommandRunner,
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
it.each(failingTestUseCases)('$title', async ({ context: { input } }) => {
|
||||
@ -402,12 +477,4 @@ describe('UpgradeCommandRunner', () => {
|
||||
expect(error).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw if upgrade command version is invalid', async () => {
|
||||
await expect(
|
||||
buildModuleAndSetupSpies({
|
||||
commandRunner: InvalidVersionUpgradeCommandRunner,
|
||||
}),
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(`"Invalid Version: invalid"`);
|
||||
});
|
||||
});
|
||||
|
||||
@ -17,10 +17,18 @@ import {
|
||||
CompareVersionMajorAndMinorReturnType,
|
||||
compareVersionMajorAndMinor,
|
||||
} from 'src/utils/version/compare-version-minor-and-major';
|
||||
import { extractVersionMajorMinorPatch } from 'src/utils/version/extract-version-major-minor-patch';
|
||||
import { getPreviousVersion } from 'src/utils/version/get-previous-version';
|
||||
|
||||
export type VersionCommands = {
|
||||
beforeSyncMetadata: ActiveOrSuspendedWorkspacesMigrationCommandRunner[];
|
||||
afterSyncMetadata: ActiveOrSuspendedWorkspacesMigrationCommandRunner[];
|
||||
};
|
||||
export type AllCommands = Record<string, VersionCommands>;
|
||||
export abstract class UpgradeCommandRunner extends ActiveOrSuspendedWorkspacesMigrationCommandRunner {
|
||||
abstract readonly fromWorkspaceVersion: SemVer;
|
||||
private fromWorkspaceVersion: SemVer;
|
||||
private currentAppVersion: SemVer;
|
||||
public abstract allCommands: AllCommands;
|
||||
public commands: VersionCommands;
|
||||
public readonly VALIDATE_WORKSPACE_VERSION_FEATURE_FLAG?: true;
|
||||
|
||||
constructor(
|
||||
@ -33,15 +41,62 @@ export abstract class UpgradeCommandRunner extends ActiveOrSuspendedWorkspacesMi
|
||||
super(workspaceRepository, twentyORMGlobalManager);
|
||||
}
|
||||
|
||||
private setUpgradeContextVersionsAndCommandsForCurrentAppVersion() {
|
||||
const ugpradeContextIsAlreadyDefined = [
|
||||
this.currentAppVersion,
|
||||
this.commands,
|
||||
this.fromWorkspaceVersion,
|
||||
].every(isDefined);
|
||||
|
||||
if (ugpradeContextIsAlreadyDefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentAppVersion = this.retrieveCurrentAppVersion();
|
||||
const currentVersionMajorMinor = `${currentAppVersion.major}.${currentAppVersion.minor}.0`;
|
||||
const currentCommands = this.allCommands[currentVersionMajorMinor];
|
||||
|
||||
if (!isDefined(currentCommands)) {
|
||||
throw new Error(
|
||||
`No command found for version ${currentAppVersion}. Please check the commands record.`,
|
||||
);
|
||||
}
|
||||
|
||||
const allCommandsVersions = Object.keys(this.allCommands);
|
||||
const previousVersion = getPreviousVersion({
|
||||
currentVersion: currentVersionMajorMinor,
|
||||
versions: allCommandsVersions,
|
||||
});
|
||||
|
||||
if (!isDefined(previousVersion)) {
|
||||
throw new Error(
|
||||
`No previous version found for version ${currentAppVersion}. Please review the "allCommands" record. Available versions are: ${allCommandsVersions.join(', ')}`,
|
||||
);
|
||||
}
|
||||
this.commands = currentCommands;
|
||||
this.fromWorkspaceVersion = previousVersion;
|
||||
this.currentAppVersion = currentAppVersion;
|
||||
|
||||
const message = [
|
||||
'Initialized upgrade context with:',
|
||||
`- currentVersion (migrating to): ${currentAppVersion}`,
|
||||
`- fromWorkspaceVersion: ${previousVersion}`,
|
||||
`- ${this.commands.beforeSyncMetadata.length + this.commands.afterSyncMetadata.length} commands`,
|
||||
];
|
||||
|
||||
this.logger.log(chalk.blue(message.join('\n ')));
|
||||
}
|
||||
|
||||
override async runOnWorkspace(args: RunOnWorkspaceArgs): Promise<void> {
|
||||
this.setUpgradeContextVersionsAndCommandsForCurrentAppVersion();
|
||||
|
||||
const { workspaceId, index, total, options } = args;
|
||||
|
||||
this.logger.log(
|
||||
chalk.blue(
|
||||
`${options.dryRun ? '(dry run)' : ''} Upgrading workspace ${workspaceId} ${index + 1}/${total}`,
|
||||
`${options.dryRun ? '(dry run) ' : ''}Upgrading workspace ${workspaceId} from=${this.fromWorkspaceVersion} to=${this.currentAppVersion} ${index + 1}/${total}`,
|
||||
),
|
||||
);
|
||||
const toVersion = this.retrieveToVersionFromAppVersion();
|
||||
|
||||
const workspaceVersionCompareResult =
|
||||
await this.retrieveWorkspaceVersionAndCompareToWorkspaceFromVersion(
|
||||
@ -61,7 +116,7 @@ export abstract class UpgradeCommandRunner extends ActiveOrSuspendedWorkspacesMi
|
||||
|
||||
await this.workspaceRepository.update(
|
||||
{ id: workspaceId },
|
||||
{ version: toVersion },
|
||||
{ version: this.currentAppVersion.version },
|
||||
);
|
||||
this.logger.log(
|
||||
chalk.blue(`Upgrade for workspace ${workspaceId} completed.`),
|
||||
@ -86,7 +141,19 @@ export abstract class UpgradeCommandRunner extends ActiveOrSuspendedWorkspacesMi
|
||||
}
|
||||
}
|
||||
|
||||
private retrieveToVersionFromAppVersion() {
|
||||
public readonly runBeforeSyncMetadata = async (args: RunOnWorkspaceArgs) => {
|
||||
for (const command of this.commands.beforeSyncMetadata) {
|
||||
await command.runOnWorkspace(args);
|
||||
}
|
||||
};
|
||||
|
||||
public readonly runAfterSyncMetadata = async (args: RunOnWorkspaceArgs) => {
|
||||
for (const command of this.commands.afterSyncMetadata) {
|
||||
await command.runOnWorkspace(args);
|
||||
}
|
||||
};
|
||||
|
||||
private retrieveCurrentAppVersion() {
|
||||
const appVersion = this.twentyConfigService.get('APP_VERSION');
|
||||
|
||||
if (!isDefined(appVersion)) {
|
||||
@ -95,15 +162,13 @@ export abstract class UpgradeCommandRunner extends ActiveOrSuspendedWorkspacesMi
|
||||
);
|
||||
}
|
||||
|
||||
const parsedVersion = extractVersionMajorMinorPatch(appVersion);
|
||||
|
||||
if (!isDefined(parsedVersion)) {
|
||||
try {
|
||||
return new SemVer(appVersion);
|
||||
} catch {
|
||||
throw new Error(
|
||||
`Should never occur, APP_VERSION is invalid ${parsedVersion}`,
|
||||
`Should never occur, APP_VERSION is invalid ${appVersion}`,
|
||||
);
|
||||
}
|
||||
|
||||
return parsedVersion;
|
||||
}
|
||||
|
||||
private async retrieveWorkspaceVersionAndCompareToWorkspaceFromVersion(
|
||||
@ -123,11 +188,4 @@ export abstract class UpgradeCommandRunner extends ActiveOrSuspendedWorkspacesMi
|
||||
this.fromWorkspaceVersion.version,
|
||||
);
|
||||
}
|
||||
|
||||
protected abstract runBeforeSyncMetadata(
|
||||
args: RunOnWorkspaceArgs,
|
||||
): Promise<void>;
|
||||
protected abstract runAfterSyncMetadata(
|
||||
args: RunOnWorkspaceArgs,
|
||||
): Promise<void>;
|
||||
}
|
||||
|
||||
@ -1,14 +1,13 @@
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Command } from 'nest-commander';
|
||||
import { SemVer } from 'semver';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Command } from 'nest-commander';
|
||||
|
||||
import {
|
||||
ActiveOrSuspendedWorkspacesMigrationCommandRunner,
|
||||
RunOnWorkspaceArgs,
|
||||
} from 'src/database/commands/command-runners/active-or-suspended-workspaces-migration.command-runner';
|
||||
import { UpgradeCommandRunner } from 'src/database/commands/command-runners/upgrade.command-runner';
|
||||
AllCommands,
|
||||
UpgradeCommandRunner,
|
||||
VersionCommands,
|
||||
} from 'src/database/commands/command-runners/upgrade.command-runner';
|
||||
import { AddTasksAssignedToMeViewCommand } from 'src/database/commands/upgrade-version-command/0-43/0-43-add-tasks-assigned-to-me-view.command';
|
||||
import { MigrateIsSearchableForCustomObjectMetadataCommand } from 'src/database/commands/upgrade-version-command/0-43/0-43-migrate-is-searchable-for-custom-object-metadata.command';
|
||||
import { MigrateRichTextContentPatchCommand } from 'src/database/commands/upgrade-version-command/0-43/0-43-migrate-rich-text-content-patch.command';
|
||||
@ -26,17 +25,12 @@ import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
|
||||
import { SyncWorkspaceMetadataCommand } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/sync-workspace-metadata.command';
|
||||
|
||||
type VersionCommands = {
|
||||
beforeSyncMetadata: ActiveOrSuspendedWorkspacesMigrationCommandRunner[];
|
||||
afterSyncMetadata: ActiveOrSuspendedWorkspacesMigrationCommandRunner[];
|
||||
};
|
||||
@Command({
|
||||
name: 'upgrade',
|
||||
description: 'Upgrade workspaces to the latest version',
|
||||
})
|
||||
export class UpgradeCommand extends UpgradeCommandRunner {
|
||||
fromWorkspaceVersion = new SemVer('0.50.0');
|
||||
private commands: VersionCommands;
|
||||
override allCommands: AllCommands;
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Workspace, 'core')
|
||||
@ -74,7 +68,7 @@ export class UpgradeCommand extends UpgradeCommandRunner {
|
||||
syncWorkspaceMetadataCommand,
|
||||
);
|
||||
|
||||
const _commands_043: VersionCommands = {
|
||||
const commands_043: VersionCommands = {
|
||||
beforeSyncMetadata: [
|
||||
this.migrateRichTextContentPatchCommand,
|
||||
this.migrateIsSearchableForCustomObjectMetadataCommand,
|
||||
@ -86,7 +80,7 @@ export class UpgradeCommand extends UpgradeCommandRunner {
|
||||
this.addTasksAssignedToMeViewCommand,
|
||||
],
|
||||
};
|
||||
const _commands_044: VersionCommands = {
|
||||
const commands_044: VersionCommands = {
|
||||
beforeSyncMetadata: [
|
||||
this.initializePermissionsCommand,
|
||||
this.updateViewAggregateOperationsCommand,
|
||||
@ -94,17 +88,17 @@ export class UpgradeCommand extends UpgradeCommandRunner {
|
||||
afterSyncMetadata: [],
|
||||
};
|
||||
|
||||
const _commands_050: VersionCommands = {
|
||||
const commands_050: VersionCommands = {
|
||||
beforeSyncMetadata: [],
|
||||
afterSyncMetadata: [],
|
||||
};
|
||||
|
||||
const _commands_051: VersionCommands = {
|
||||
const commands_051: VersionCommands = {
|
||||
beforeSyncMetadata: [this.upgradeCreatedByEnumCommand],
|
||||
afterSyncMetadata: [],
|
||||
};
|
||||
|
||||
const _commands_052: VersionCommands = {
|
||||
const commands_052: VersionCommands = {
|
||||
beforeSyncMetadata: [
|
||||
this.upgradeDateAndDateTimeFieldsSettingsJsonCommand,
|
||||
this.migrateRelationsToFieldMetadataCommand,
|
||||
@ -120,18 +114,13 @@ export class UpgradeCommand extends UpgradeCommandRunner {
|
||||
],
|
||||
};
|
||||
|
||||
this.commands = commands_053;
|
||||
}
|
||||
|
||||
override async runBeforeSyncMetadata(args: RunOnWorkspaceArgs) {
|
||||
for (const command of this.commands.beforeSyncMetadata) {
|
||||
await command.runOnWorkspace(args);
|
||||
}
|
||||
}
|
||||
|
||||
override async runAfterSyncMetadata(args: RunOnWorkspaceArgs) {
|
||||
for (const command of this.commands.afterSyncMetadata) {
|
||||
await command.runOnWorkspace(args);
|
||||
}
|
||||
this.allCommands = {
|
||||
'0.43.0': commands_043,
|
||||
'0.44.0': commands_044,
|
||||
'0.50.0': commands_050,
|
||||
'0.51.0': commands_051,
|
||||
'0.52.0': commands_052,
|
||||
'0.53.0': commands_053,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`is-same-major-and-minor-version incomplete version1 1`] = `"Received invalid version: 1.0 1.1.0"`;
|
||||
exports[`It should compare two versions with incomplete version1 1`] = `"Received invalid version: 1.0 1.1.0"`;
|
||||
|
||||
exports[`is-same-major-and-minor-version invalid version1 1`] = `"Received invalid version: invalid 1.1.0"`;
|
||||
exports[`It should compare two versions with invalid version1 1`] = `"Received invalid version: invalid 1.1.0"`;
|
||||
|
||||
exports[`is-same-major-and-minor-version invalid version2 1`] = `"Received invalid version: 1.0.0 invalid"`;
|
||||
exports[`It should compare two versions with invalid version2 1`] = `"Received invalid version: 1.0.0 invalid"`;
|
||||
|
||||
@ -11,7 +11,7 @@ type IsSameVersionTestCase = EachTestingContext<{
|
||||
expected?: CompareVersionMajorAndMinorReturnType;
|
||||
expectToThrow?: boolean;
|
||||
}>;
|
||||
describe('is-same-major-and-minor-version', () => {
|
||||
describe('It should compare two versions with', () => {
|
||||
const beneathVersionTestCases: IsSameVersionTestCase[] = [
|
||||
{
|
||||
context: {
|
||||
@ -181,7 +181,7 @@ describe('is-same-major-and-minor-version', () => {
|
||||
...beneathVersionTestCases,
|
||||
...aboveVersionTestCases,
|
||||
])(
|
||||
'$title',
|
||||
' $title',
|
||||
({ context: { version1, version2, expected, expectToThrow = false } }) => {
|
||||
if (expectToThrow) {
|
||||
expect(() =>
|
||||
|
||||
@ -6,7 +6,7 @@ type IsSameVersionTestCase = EachTestingContext<{
|
||||
version: string | undefined;
|
||||
expected: string | null;
|
||||
}>;
|
||||
describe('extract-version-major-minor-patch', () => {
|
||||
describe('It should extract major.minor.patch values from a', () => {
|
||||
const testCase: IsSameVersionTestCase[] = [
|
||||
{
|
||||
context: {
|
||||
@ -101,7 +101,7 @@ describe('extract-version-major-minor-patch', () => {
|
||||
},
|
||||
];
|
||||
|
||||
test.each(testCase)('$title', ({ context: { version, expected } }) => {
|
||||
test.each(testCase)(' $title', ({ context: { version, expected } }) => {
|
||||
expect(extractVersionMajorMinorPatch(version)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
@ -0,0 +1,103 @@
|
||||
import { EachTestingContext } from 'twenty-shared/testing';
|
||||
|
||||
import { getPreviousVersion } from 'src/utils/version/get-previous-version';
|
||||
|
||||
type GetPreviousVersionTestCase = EachTestingContext<{
|
||||
versions: string[];
|
||||
currentVersion: string;
|
||||
expected: string | undefined;
|
||||
}>;
|
||||
|
||||
describe('It should return the previous version from a list of', () => {
|
||||
const testCase: GetPreviousVersionTestCase[] = [
|
||||
{
|
||||
context: {
|
||||
versions: ['0.1.0', '0.2.0', '0.3.0'],
|
||||
currentVersion: '0.3.0',
|
||||
expected: '0.2.0',
|
||||
},
|
||||
title: 'Basic version sequence',
|
||||
},
|
||||
{
|
||||
context: {
|
||||
versions: ['1.0.0', '1.1.0', '2.0.0'],
|
||||
currentVersion: '2.0.0',
|
||||
expected: '1.1.0',
|
||||
},
|
||||
title: 'Major version jump',
|
||||
},
|
||||
{
|
||||
context: {
|
||||
versions: ['0.1.0', '0.1.1', '0.1.2'],
|
||||
currentVersion: '0.1.2',
|
||||
expected: '0.1.1',
|
||||
},
|
||||
title: 'Patch version sequence',
|
||||
},
|
||||
{
|
||||
context: {
|
||||
versions: ['1.0.0'],
|
||||
currentVersion: '1.0.0',
|
||||
expected: undefined,
|
||||
},
|
||||
title: 'Single version',
|
||||
},
|
||||
{
|
||||
context: {
|
||||
versions: [],
|
||||
currentVersion: '1.0.0',
|
||||
expected: undefined,
|
||||
},
|
||||
title: 'Empty version array',
|
||||
},
|
||||
{
|
||||
context: {
|
||||
versions: ['0.1.0', '0.2.0', '0.3.0'],
|
||||
currentVersion: '0.1.0',
|
||||
expected: undefined,
|
||||
},
|
||||
title: 'No previous version available',
|
||||
},
|
||||
{
|
||||
context: {
|
||||
versions: ['1.0.0', '2.0.0', '1.5.0'],
|
||||
currentVersion: '2.0.0',
|
||||
expected: '1.5.0',
|
||||
},
|
||||
title: 'Unordered versions',
|
||||
},
|
||||
{
|
||||
context: {
|
||||
versions: ['1.0.0', '1.0.0-alpha', '1.0.0-beta'],
|
||||
currentVersion: '1.0.0',
|
||||
expected: '1.0.0-beta',
|
||||
},
|
||||
title: 'Pre-release versions',
|
||||
},
|
||||
{
|
||||
context: {
|
||||
versions: ['invalid', '1.0.0', '2.0.0'],
|
||||
currentVersion: '2.0.0',
|
||||
expected: undefined,
|
||||
},
|
||||
title: 'Invalid version in array',
|
||||
},
|
||||
{
|
||||
context: {
|
||||
versions: ['1.0.0', '2.0.0'],
|
||||
currentVersion: 'invalid',
|
||||
expected: undefined,
|
||||
},
|
||||
title: 'Invalid current version',
|
||||
},
|
||||
];
|
||||
|
||||
test.each(testCase)(
|
||||
' $title',
|
||||
({ context: { versions, currentVersion, expected } }) => {
|
||||
const result = getPreviousVersion({ versions, currentVersion });
|
||||
|
||||
expect(result?.format()).toBe(expected);
|
||||
},
|
||||
);
|
||||
});
|
||||
@ -0,0 +1,26 @@
|
||||
import { SemVer } from 'semver';
|
||||
|
||||
type GetPreviousVersionFromArrayArgs = {
|
||||
versions: string[];
|
||||
currentVersion: string;
|
||||
};
|
||||
export const getPreviousVersion = ({
|
||||
versions,
|
||||
currentVersion,
|
||||
}: GetPreviousVersionFromArrayArgs): SemVer | undefined => {
|
||||
try {
|
||||
const semverVersions = versions
|
||||
.map((version) => new SemVer(version))
|
||||
.sort((a, b) => b.compare(a));
|
||||
|
||||
const currentSemver = new SemVer(currentVersion);
|
||||
|
||||
const previousVersion = semverVersions.find(
|
||||
(version) => version.compare(currentSemver) < 0,
|
||||
);
|
||||
|
||||
return previousVersion;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user