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 { LoggerService } from '@/logger/logger.service';
import { MigrationService } from './migration.service'; import { MigrationService } from './migration.service';
import { MigrationAction, MigrationVersion } from './types'; import { MigrationAction } from './types';
@Command({ @Command({
name: 'migration', name: 'migration',
@ -31,7 +31,7 @@ export class MigrationCommand extends CommandRunner {
case 'create': { case 'create': {
const [, version] = passedParam; const [, version] = passedParam;
if (!this.isValidVersion(version)) { if (!this.migrationService.isValidVersion(version)) {
throw new TypeError('Invalid version value.'); throw new TypeError('Invalid version value.');
} }
@ -47,14 +47,17 @@ export class MigrationCommand extends CommandRunner {
this.exit(); 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.'); throw new TypeError('Invalid version value.');
} }
return await this.migrationService.run({
action: action as MigrationAction,
version,
});
} }
default: default:
this.logger.error('No valid command provided'); this.logger.error('No valid command provided');
@ -67,14 +70,4 @@ export class MigrationCommand extends CommandRunner {
this.logger.log('Exiting migration process.'); this.logger.log('Exiting migration process.');
process.exit(0); 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'); this.logger.log('Mongoose connection established');
const isCLI = Boolean(process.env.HEXABOT_CLI); if (!this.isCLI && config.mongo.autoMigrate) {
if (!isCLI && config.mongo.autoMigrate) {
this.logger.log('Executing migrations ...'); this.logger.log('Executing migrations ...');
await this.run({ await this.run({
action: MigrationAction.UP, action: MigrationAction.UP,
@ -77,6 +76,24 @@ export class MigrationService implements OnApplicationBootstrap {
return this.moduleRef.get('MONGO_MIGRATION_DIR'); 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 * 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. * @returns Resolves when the migration operation is successfully completed.
*/ */
public async run({ action, version, isAutoMigrate }: MigrationRunParams) { public async run({ action, version, isAutoMigrate }: MigrationRunParams) {
if (!version) { if (!this.isCLI) {
if (isAutoMigrate) { if (isAutoMigrate) {
const metadata = await this.metadataService.findOne({ const metadata = await this.metadataService.findOne({
name: 'db-version', name: 'db-version',
@ -195,11 +212,15 @@ module.exports = {
const version = metadata ? metadata.value : INITIAL_DB_VERSION; const version = metadata ? metadata.value : INITIAL_DB_VERSION;
await this.runUpgrades(action, version); await this.runUpgrades(action, version);
} else { } else {
await this.runAll(action); // Do nothing ...
this.exit(); return;
} }
} else { } else {
await this.runOne({ action, version }); if (!version) {
await this.runAll(action);
} else {
await this.runOne({ action, version });
}
this.exit(); this.exit();
} }
} }