2021-10-13 15:21:00 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
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' }),
|
|
|
|
};
|
|
|
|
|
|
|
|
async run() {
|
|
|
|
const logger = getLogger();
|
|
|
|
LoggerProxy.init(logger);
|
|
|
|
|
2023-01-11 09:29:31 -08:00
|
|
|
this.parse(DbRevertMigrationCommand);
|
2021-10-13 15:21:00 -07:00
|
|
|
|
|
|
|
let connection: Connection | undefined;
|
|
|
|
try {
|
2023-01-11 09:29:31 -08:00
|
|
|
const dbType = config.getEnv('database.type');
|
|
|
|
const connectionOptions: ConnectionOptions = {
|
2023-01-30 05:42:30 -08:00
|
|
|
...getConnectionOptions(dbType),
|
2021-10-13 15:21:00 -07:00
|
|
|
subscribers: [],
|
|
|
|
synchronize: false,
|
|
|
|
migrationsRun: false,
|
|
|
|
dropSchema: false,
|
|
|
|
logging: ['query', 'error', 'schema'],
|
2023-01-11 09:29:31 -08:00
|
|
|
};
|
2023-01-13 09:12:22 -08:00
|
|
|
connection = new Connection(connectionOptions);
|
|
|
|
await connection.initialize();
|
2021-10-13 15:21:00 -07:00
|
|
|
await connection.undoLastMigration();
|
2023-01-13 09:12:22 -08:00
|
|
|
await connection.destroy();
|
2021-10-13 15:21:00 -07:00
|
|
|
} catch (error) {
|
2023-01-13 09:12:22 -08:00
|
|
|
if (connection?.isInitialized) await connection.destroy();
|
2021-10-13 15:21:00 -07:00
|
|
|
|
|
|
|
console.error('Error reverting last migration. See log messages for details.');
|
2022-07-24 08:25:01 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
2021-10-13 15:21:00 -07:00
|
|
|
logger.error(error.message);
|
|
|
|
this.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.exit();
|
|
|
|
}
|
|
|
|
}
|