feat(core): Print the name of the migration that cannot be reverted when using n8n db:revert (#9473)

This commit is contained in:
Danny Martini
2024-05-23 18:16:26 +02:00
committed by GitHub
parent 93679076b4
commit 3b93aae6dc
4 changed files with 207 additions and 79 deletions

View File

@@ -1,6 +1,6 @@
import { Command, Flags } from '@oclif/core';
import type { DataSourceOptions as ConnectionOptions } from '@n8n/typeorm';
import { DataSource as Connection } from '@n8n/typeorm';
import { MigrationExecutor, DataSource as Connection } from '@n8n/typeorm';
import { Container } from 'typedi';
import { Logger } from '@/Logger';
import { setSchema } from '@/Db';
@@ -13,27 +13,44 @@ import config from '@/config';
// 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,
connection: Connection,
migrationExecutor: MigrationExecutor,
) {
const dbType = config.getEnv('database.type');
const executedMigrations = await migrationExecutor.getExecutedMigrations();
const lastExecutedMigration = executedMigrations.at(0);
(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.');
if (lastExecutedMigration === undefined) {
logger.error(
"Cancelled command. The database was never migrated. Are you sure you're connected to the right database?.",
);
return;
}
if (!lastMigration.down) {
logger.error('The last migration was irreversible and cannot be reverted.');
const lastMigrationInstance = connection.migrations.find((m) => {
// Migration names are optional. If a migration has no name property
// TypeORM will default to the class name.
const name1 = m.name ?? m.constructor.name;
const name2 = lastExecutedMigration.name;
return name1 === name2;
});
if (lastMigrationInstance === undefined) {
logger.error(
`The last migration that was executed is "${lastExecutedMigration.name}", but I could not find that migration's code in the currently installed version of n8n.`,
);
logger.error(
'This usually means that you downgraded n8n before running `n8n db:revert`. Please upgrade n8n again and run `n8n db:revert` and then downgrade again.',
);
return;
}
if (!lastMigrationInstance.down) {
const message = lastMigrationInstance.name
? `Cancelled command. The last migration "${lastMigrationInstance.name}" was irreversible.`
: 'Cancelled command. The last migration was irreversible.';
logger.error(message);
return;
}
@@ -68,7 +85,17 @@ export class DbRevertMigrationCommand extends Command {
logging: ['query', 'error', 'schema'],
};
return await main(connectionOptions, this.logger, Connection);
const connection = new Connection(connectionOptions);
await connection.initialize();
const dbType = config.getEnv('database.type');
if (dbType === 'postgresdb') await setSchema(connection);
const migrationExecutor = new MigrationExecutor(connection);
(connectionOptions.migrations as Migration[]).forEach(wrapMigration);
return await main(this.logger, connection, migrationExecutor);
}
async catch(error: Error) {