diff --git a/api/src/migration/migration.command.ts b/api/src/migration/migration.command.ts index 4e27cd52..346175dd 100644 --- a/api/src/migration/migration.command.ts +++ b/api/src/migration/migration.command.ts @@ -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); - } } diff --git a/api/src/migration/migration.service.ts b/api/src/migration/migration.service.ts index 6090fda3..88db7aca 100644 --- a/api/src/migration/migration.service.ts +++ b/api/src/migration/migration.service.ts @@ -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(); } }