2021-10-13 15:21:00 -07:00
|
|
|
import { Command, flags } from '@oclif/command';
|
2023-01-27 05:56:56 -08:00
|
|
|
import type { DataSourceOptions as ConnectionOptions } from 'typeorm';
|
|
|
|
import { DataSource as Connection } from 'typeorm';
|
2021-10-13 15:21:00 -07:00
|
|
|
import { LoggerProxy } from 'n8n-workflow';
|
2022-11-09 06:25:00 -08:00
|
|
|
import { getLogger } from '@/Logger';
|
2023-01-11 09:29:31 -08:00
|
|
|
import { getConnectionOptions } from '@/Db';
|
|
|
|
import config from '@/config';
|
2021-10-13 15:21:00 -07:00
|
|
|
|
|
|
|
export class DbRevertMigrationCommand extends Command {
|
|
|
|
static description = 'Revert last database migration';
|
|
|
|
|
|
|
|
static examples = ['$ n8n db:revert'];
|
|
|
|
|
|
|
|
static flags = {
|
|
|
|
help: flags.help({ char: 'h' }),
|
|
|
|
};
|
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
protected logger = LoggerProxy.init(getLogger());
|
|
|
|
|
|
|
|
private connection: Connection;
|
2021-10-13 15:21:00 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
async init() {
|
2023-01-11 09:29:31 -08:00
|
|
|
this.parse(DbRevertMigrationCommand);
|
2023-02-10 05:59:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async run() {
|
|
|
|
const dbType = config.getEnv('database.type');
|
|
|
|
const connectionOptions: ConnectionOptions = {
|
|
|
|
...getConnectionOptions(dbType),
|
|
|
|
subscribers: [],
|
|
|
|
synchronize: false,
|
|
|
|
migrationsRun: false,
|
|
|
|
dropSchema: false,
|
|
|
|
logging: ['query', 'error', 'schema'],
|
|
|
|
};
|
|
|
|
|
|
|
|
this.connection = new Connection(connectionOptions);
|
|
|
|
await this.connection.initialize();
|
|
|
|
await this.connection.undoLastMigration();
|
|
|
|
await this.connection.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
async catch(error: Error) {
|
|
|
|
this.logger.error('Error reverting last migration. See log messages for details.');
|
|
|
|
this.logger.error(error.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async finally(error: Error | undefined) {
|
|
|
|
if (this.connection?.isInitialized) await this.connection.destroy();
|
2021-10-13 15:21:00 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
this.exit(error ? 1 : 0);
|
2021-10-13 15:21:00 -07:00
|
|
|
}
|
|
|
|
}
|