mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: minor adjustment
This commit is contained in:
parent
20439fab92
commit
b0f997eccd
@ -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)) {
|
||||
throw new TypeError('Invalid version value.');
|
||||
}
|
||||
|
||||
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.');
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
if (!version) {
|
||||
await this.runAll(action);
|
||||
} else {
|
||||
await this.runOne({ action, version });
|
||||
}
|
||||
this.exit();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user