2024-01-22 09:25:36 -08:00
|
|
|
import { Command, Flags } from '@oclif/core';
|
2024-02-08 06:13:29 -08:00
|
|
|
import type { DataSourceOptions as ConnectionOptions } from '@n8n/typeorm';
|
|
|
|
import { DataSource as Connection } from '@n8n/typeorm';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { Container } from 'typedi';
|
|
|
|
import { Logger } from '@/Logger';
|
2024-02-20 05:28:53 -08:00
|
|
|
import { setSchema } from '@/Db';
|
|
|
|
import { getConnectionOptions } from '@db/config';
|
2023-06-22 00:06:31 -07:00
|
|
|
import type { Migration } from '@db/types';
|
|
|
|
import { wrapMigration } from '@db/utils/migrationHelpers';
|
2023-01-11 09:29:31 -08:00
|
|
|
import config from '@/config';
|
2021-10-13 15:21:00 -07:00
|
|
|
|
2024-04-11 00:20:48 -07:00
|
|
|
// This function is extracted to make it easier to unit test it.
|
|
|
|
// Mocking turned into a mess due to this command using typeorm and the db
|
|
|
|
// config directly and customizing and monkey patching parts.
|
|
|
|
export async function main(
|
|
|
|
connectionOptions: ConnectionOptions,
|
|
|
|
logger: Logger,
|
|
|
|
DataSource: typeof Connection,
|
|
|
|
) {
|
|
|
|
const dbType = config.getEnv('database.type');
|
|
|
|
|
|
|
|
(connectionOptions.migrations as Migration[]).forEach(wrapMigration);
|
|
|
|
|
|
|
|
const connection = new DataSource(connectionOptions);
|
|
|
|
await connection.initialize();
|
|
|
|
if (dbType === 'postgresdb') await setSchema(connection);
|
|
|
|
|
|
|
|
const lastMigration = connection.migrations.at(-1);
|
|
|
|
|
|
|
|
if (lastMigration === undefined) {
|
|
|
|
logger.error('There is no migration to reverse.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lastMigration.down) {
|
|
|
|
logger.error('The last migration was irreversible and cannot be reverted.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await connection.undoLastMigration();
|
|
|
|
await connection.destroy();
|
|
|
|
}
|
|
|
|
|
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 = {
|
2024-01-22 09:25:36 -08:00
|
|
|
help: Flags.help({ char: 'h' }),
|
2021-10-13 15:21:00 -07:00
|
|
|
};
|
|
|
|
|
2023-10-25 07:35:22 -07:00
|
|
|
protected logger = Container.get(Logger);
|
2023-02-10 05:59:20 -08:00
|
|
|
|
|
|
|
private connection: Connection;
|
2021-10-13 15:21:00 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
async init() {
|
2024-01-22 09:25:36 -08:00
|
|
|
await this.parse(DbRevertMigrationCommand);
|
2023-02-10 05:59:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async run() {
|
|
|
|
const connectionOptions: ConnectionOptions = {
|
2024-02-20 05:28:53 -08:00
|
|
|
...getConnectionOptions(),
|
2023-02-10 05:59:20 -08:00
|
|
|
subscribers: [],
|
|
|
|
synchronize: false,
|
|
|
|
migrationsRun: false,
|
|
|
|
dropSchema: false,
|
|
|
|
logging: ['query', 'error', 'schema'],
|
|
|
|
};
|
|
|
|
|
2024-04-11 00:20:48 -07:00
|
|
|
return await main(connectionOptions, this.logger, Connection);
|
2023-02-10 05:59:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|