Files
twenty_crm/packages/twenty-server/src/database/commands/command-runners/migration.command-runner.ts
Charles Bochet baa3043954 Refactor upgrade commands (#10592)
Simplifying a lot the upgrade system.

New way to upgrade:
`yarn command:prod upgrade`

New way to write upgrade commands (all wrapping is done for you)
```
  override async runOnWorkspace({
    index,
    total,
    workspaceId,
    options,
  }: RunOnWorkspaceArgs): Promise<void> {}
```

Also cleaning CommandModule imports to make it lighter
2025-02-28 19:51:32 +01:00

68 lines
1.5 KiB
TypeScript

import { Logger } from '@nestjs/common';
import chalk from 'chalk';
import { CommandRunner, Option } from 'nest-commander';
import { CommandLogger } from 'src/database/commands/logger';
export type MigrationCommandOptions = {
dryRun?: boolean;
verbose?: boolean;
};
export abstract class MigrationCommandRunner extends CommandRunner {
protected logger: CommandLogger | Logger;
constructor() {
super();
this.logger = new CommandLogger({
verbose: false,
constructorName: this.constructor.name,
});
}
@Option({
flags: '-d, --dry-run',
description: 'Simulate the command without making actual changes',
required: false,
})
parseDryRun(): boolean {
return true;
}
@Option({
flags: '-v, --verbose',
description: 'Verbose output',
required: false,
})
parseVerbose(): boolean {
return true;
}
override async run(
passedParams: string[],
options: MigrationCommandOptions,
): Promise<void> {
if (options.verbose) {
this.logger = new CommandLogger({
verbose: true,
constructorName: this.constructor.name,
});
}
try {
await this.runMigrationCommand(passedParams, options);
} catch (error) {
this.logger.error(chalk.red(`Command failed`));
throw error;
} finally {
this.logger.log(chalk.blue('Command completed!'));
}
}
protected abstract runMigrationCommand(
passedParams: string[],
options: MigrationCommandOptions,
): Promise<void>;
}