fix: minor adjustment

This commit is contained in:
Mohamed Marrouchi 2025-01-06 10:29:26 +01:00
parent 20439fab92
commit b0f997eccd
2 changed files with 38 additions and 24 deletions

View File

@ -11,7 +11,7 @@ import { Command, CommandRunner } from 'nest-commander';
import { LoggerService } from '@/logger/logger.service';
import { MigrationService } from './migration.service';
import { MigrationAction, MigrationVersion } from './types';
import { MigrationAction } from './types';
@Command({
name: 'migration',
@ -31,7 +31,7 @@ export class MigrationCommand extends CommandRunner {
case 'create': {
const [, version] = passedParam;
if (!this.isValidVersion(version)) {
if (!this.migrationService.isValidVersion(version)) {
throw new TypeError('Invalid version value.');
}
@ -47,14 +47,17 @@ export class MigrationCommand extends CommandRunner {
this.exit();
}
if (!this.isValidVersion(version)) {
if (
typeof version === 'undefined' ||
this.migrationService.isValidVersion(version)
) {
return await this.migrationService.run({
action: action as MigrationAction,
version,
});
} else {
throw new TypeError('Invalid version value.');
}
return await this.migrationService.run({
action: action as MigrationAction,
version,
});
}
default:
this.logger.error('No valid command provided');
@ -67,14 +70,4 @@ export class MigrationCommand extends CommandRunner {
this.logger.log('Exiting migration process.');
process.exit(0);
}
/**
* Checks if the migration version is in valid format
* @param version migration version name
* @returns True, if the migration version name is valid
*/
public isValidVersion(version: string): version is MigrationVersion {
const regex = /^v(\d+)\.(\d+)\.(\d+)$/;
return regex.test(version);
}
}

View File

@ -55,8 +55,7 @@ export class MigrationService implements OnApplicationBootstrap {
}
this.logger.log('Mongoose connection established');
const isCLI = Boolean(process.env.HEXABOT_CLI);
if (!isCLI && config.mongo.autoMigrate) {
if (!this.isCLI && config.mongo.autoMigrate) {
this.logger.log('Executing migrations ...');
await this.run({
action: MigrationAction.UP,
@ -77,6 +76,24 @@ export class MigrationService implements OnApplicationBootstrap {
return this.moduleRef.get('MONGO_MIGRATION_DIR');
}
/**
* Checks if current running using CLI
* @returns True if using CLI
*/
public get isCLI() {
return Boolean(process.env.HEXABOT_CLI);
}
/**
* Checks if the migration version is in valid format
* @param version migration version name
* @returns True, if the migration version name is valid
*/
public isValidVersion(version: string): version is MigrationVersion {
const regex = /^v(\d+)\.(\d+)\.(\d+)$/;
return regex.test(version);
}
/**
* Checks if the migration path is well set and exists
*/
@ -187,7 +204,7 @@ module.exports = {
* @returns Resolves when the migration operation is successfully completed.
*/
public async run({ action, version, isAutoMigrate }: MigrationRunParams) {
if (!version) {
if (!this.isCLI) {
if (isAutoMigrate) {
const metadata = await this.metadataService.findOne({
name: 'db-version',
@ -195,11 +212,15 @@ module.exports = {
const version = metadata ? metadata.value : INITIAL_DB_VERSION;
await this.runUpgrades(action, version);
} else {
await this.runAll(action);
this.exit();
// Do nothing ...
return;
}
} else {
await this.runOne({ action, version });
if (!version) {
await this.runAll(action);
} else {
await this.runOne({ action, version });
}
this.exit();
}
}