refactor(server): upgrade command to more human friendly (#10858)

# Introduction
Refactored the upgrade command to be more intuitive to anyone wanting to
add a command to the next relase upgrade instance

Also updated the upgrade command for the next 0.44 release
This commit is contained in:
Paul Rastoin
2025-03-13 16:48:58 +01:00
committed by GitHub
parent 93f70f8457
commit 37afb38479
6 changed files with 80 additions and 30 deletions

View File

@ -177,5 +177,5 @@ export abstract class ActiveOrSuspendedWorkspacesMigrationCommandRunner<
);
}
protected abstract runOnWorkspace(args: RunOnWorkspaceArgs): Promise<void>;
public abstract runOnWorkspace(args: RunOnWorkspaceArgs): Promise<void>;
}

View File

@ -2,7 +2,6 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { InitializePermissionsCommand } from 'src/database/commands/upgrade-version-command/0-44/0-44-initialize-permissions.command';
import { MigrateRelationsToFieldMetadataCommand } from 'src/database/commands/upgrade-version-command/0-44/0-44-migrate-relations-to-field-metadata.command';
import { UserWorkspace } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
@ -22,13 +21,7 @@ import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/works
RoleModule,
UserRoleModule,
],
providers: [
InitializePermissionsCommand,
MigrateRelationsToFieldMetadataCommand,
],
exports: [
InitializePermissionsCommand,
MigrateRelationsToFieldMetadataCommand,
],
providers: [InitializePermissionsCommand],
exports: [InitializePermissionsCommand],
})
export class V0_44_UpgradeVersionCommandModule {}

View File

@ -21,7 +21,7 @@ import {
import { isFieldMetadataOfType } from 'src/engine/utils/is-field-metadata-of-type.util';
@Command({
name: 'upgrade:0-44:migrate-relations-to-field-metadata',
name: 'upgrade:0-50:migrate-relations-to-field-metadata',
description: 'Migrate relations to field metadata',
})
export class MigrateRelationsToFieldMetadataCommand extends ActiveOrSuspendedWorkspacesMigrationCommandRunner {

View File

@ -0,0 +1,27 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MigrateRelationsToFieldMetadataCommand } from 'src/database/commands/upgrade-version-command/0-50/0-50-migrate-relations-to-field-metadata.command';
import { UserWorkspace } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { RoleModule } from 'src/engine/metadata-modules/role/role.module';
import { UserRoleModule } from 'src/engine/metadata-modules/user-role/user-role.module';
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
@Module({
imports: [
TypeOrmModule.forFeature([Workspace, UserWorkspace], 'core'),
TypeOrmModule.forFeature(
[FieldMetadataEntity, ObjectMetadataEntity],
'metadata',
),
WorkspaceDataSourceModule,
RoleModule,
UserRoleModule,
],
providers: [MigrateRelationsToFieldMetadataCommand],
exports: [MigrateRelationsToFieldMetadataCommand],
})
export class V0_50_UpgradeVersionCommandModule {}

View File

@ -3,6 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { V0_43_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/0-43/0-43-upgrade-version-command.module';
import { V0_44_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/0-44/0-44-upgrade-version-command.module';
import { V0_50_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/0-50/0-50-upgrade-version-command.module';
import { UpgradeCommand } from 'src/database/commands/upgrade-version-command/upgrade.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { WorkspaceSyncMetadataModule } from 'src/engine/workspace-manager/workspace-sync-metadata/workspace-sync-metadata.module';
@ -12,6 +13,7 @@ import { WorkspaceSyncMetadataModule } from 'src/engine/workspace-manager/worksp
TypeOrmModule.forFeature([Workspace], 'core'),
V0_43_UpgradeVersionCommandModule,
V0_44_UpgradeVersionCommandModule,
V0_50_UpgradeVersionCommandModule,
WorkspaceSyncMetadataModule,
],
providers: [UpgradeCommand],

View File

@ -4,24 +4,34 @@ import { Command } from 'nest-commander';
import { SemVer } from 'semver';
import { Repository } from 'typeorm';
import { RunOnWorkspaceArgs } from 'src/database/commands/command-runners/active-or-suspended-workspaces-migration.command-runner';
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';
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';
import { MigrateSearchVectorOnNoteAndTaskEntitiesCommand } from 'src/database/commands/upgrade-version-command/0-43/0-43-migrate-search-vector-on-note-and-task-entities.command';
import { UpdateDefaultViewRecordOpeningOnWorkflowObjectsCommand } from 'src/database/commands/upgrade-version-command/0-43/0-43-update-default-view-record-opening-on-workflow-objects.command';
import { InitializePermissionsCommand } from 'src/database/commands/upgrade-version-command/0-44/0-44-initialize-permissions.command';
import { MigrateRelationsToFieldMetadataCommand } from 'src/database/commands/upgrade-version-command/0-50/0-50-migrate-relations-to-field-metadata.command';
import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service';
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.43.0');
private commands: VersionCommands;
constructor(
@InjectRepository(Workspace, 'core')
@ -30,11 +40,18 @@ export class UpgradeCommand extends UpgradeCommandRunner {
protected readonly twentyORMGlobalManager: TwentyORMGlobalManager,
protected readonly syncWorkspaceMetadataCommand: SyncWorkspaceMetadataCommand,
// 0.43 Commands
protected readonly migrateRichTextContentPatchCommand: MigrateRichTextContentPatchCommand,
protected readonly addTasksAssignedToMeViewCommand: AddTasksAssignedToMeViewCommand,
protected readonly migrateIsSearchableForCustomObjectMetadataCommand: MigrateIsSearchableForCustomObjectMetadataCommand,
protected readonly updateDefaultViewRecordOpeningOnWorkflowObjectsCommand: UpdateDefaultViewRecordOpeningOnWorkflowObjectsCommand,
protected readonly migrateSearchVectorOnNoteAndTaskEntitiesCommand: MigrateSearchVectorOnNoteAndTaskEntitiesCommand,
// 0.44 Commands
protected readonly initializePermissionsCommand: InitializePermissionsCommand,
// 0.50 Commands
protected readonly migrateRelationsToFieldMetadataCommand: MigrateRelationsToFieldMetadataCommand,
) {
super(
workspaceRepository,
@ -42,29 +59,40 @@ export class UpgradeCommand extends UpgradeCommandRunner {
twentyORMGlobalManager,
syncWorkspaceMetadataCommand,
);
const _commands_043: VersionCommands = {
beforeSyncMetadata: [
this.migrateRichTextContentPatchCommand,
this.migrateIsSearchableForCustomObjectMetadataCommand,
this.migrateSearchVectorOnNoteAndTaskEntitiesCommand,
this.migrateIsSearchableForCustomObjectMetadataCommand,
],
afterSyncMetadata: [
this.updateDefaultViewRecordOpeningOnWorkflowObjectsCommand,
this.addTasksAssignedToMeViewCommand,
],
};
const commands_044: VersionCommands = {
beforeSyncMetadata: [this.initializePermissionsCommand],
afterSyncMetadata: [],
};
const _commands_050: VersionCommands = {
beforeSyncMetadata: [this.migrateRelationsToFieldMetadataCommand],
afterSyncMetadata: [],
};
this.commands = commands_044;
}
override async runBeforeSyncMetadata(args: RunOnWorkspaceArgs) {
await this.migrateRichTextContentPatchCommand.runOnWorkspace(args);
await this.migrateIsSearchableForCustomObjectMetadataCommand.runOnWorkspace(
args,
);
await this.migrateSearchVectorOnNoteAndTaskEntitiesCommand.runOnWorkspace(
args,
);
await this.migrateIsSearchableForCustomObjectMetadataCommand.runOnWorkspace(
args,
);
for (const command of this.commands.beforeSyncMetadata) {
await command.runOnWorkspace(args);
}
}
override async runAfterSyncMetadata(args: RunOnWorkspaceArgs) {
await this.updateDefaultViewRecordOpeningOnWorkflowObjectsCommand.runOnWorkspace(
args,
);
await this.addTasksAssignedToMeViewCommand.runOnWorkspace(args);
for (const command of this.commands.afterSyncMetadata) {
await command.runOnWorkspace(args);
}
}
}