mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
feat(core): Print the name of the migration that cannot be reverted when using n8n db:revert (#9473)
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { main } from '@/commands/db/revert';
|
||||
import { mockInstance } from '../../../shared/mocking';
|
||||
import { Logger } from '@/Logger';
|
||||
import * as DbConfig from '@db/config';
|
||||
import type { IrreversibleMigration, ReversibleMigration } from '@/databases/types';
|
||||
import type { DataSource } from '@n8n/typeorm';
|
||||
import type { Migration, MigrationExecutor } from '@n8n/typeorm';
|
||||
import { type DataSource } from '@n8n/typeorm';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
|
||||
const logger = mockInstance(Logger);
|
||||
@@ -16,23 +16,23 @@ test("don't revert migrations if there is no migration", async () => {
|
||||
//
|
||||
// ARRANGE
|
||||
//
|
||||
const connectionOptions = DbConfig.getConnectionOptions();
|
||||
// @ts-expect-error property is readonly
|
||||
connectionOptions.migrations = [];
|
||||
const dataSource = mock<DataSource>({ migrations: [] });
|
||||
const migrations: Migration[] = [];
|
||||
const dataSource = mock<DataSource>({ migrations });
|
||||
const migrationExecutor = mock<MigrationExecutor>();
|
||||
migrationExecutor.getExecutedMigrations.mockResolvedValue([]);
|
||||
|
||||
//
|
||||
// ACT
|
||||
//
|
||||
await main(connectionOptions, logger, function () {
|
||||
return dataSource;
|
||||
} as never);
|
||||
await main(logger, dataSource, migrationExecutor);
|
||||
|
||||
//
|
||||
// ASSERT
|
||||
//
|
||||
expect(logger.error).toHaveBeenCalledTimes(1);
|
||||
expect(logger.error).toHaveBeenCalledWith('There is no migration to reverse.');
|
||||
expect(logger.error).toHaveBeenCalledWith(
|
||||
"Cancelled command. The database was never migrated. Are you sure you're connected to the right database?.",
|
||||
);
|
||||
expect(dataSource.undoLastMigration).not.toHaveBeenCalled();
|
||||
expect(dataSource.destroy).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -42,33 +42,96 @@ test("don't revert the last migration if it had no down migration", async () =>
|
||||
// ARRANGE
|
||||
//
|
||||
class TestMigration implements IrreversibleMigration {
|
||||
name = undefined;
|
||||
|
||||
async up() {}
|
||||
|
||||
down = undefined;
|
||||
}
|
||||
|
||||
const connectionOptions = DbConfig.getConnectionOptions();
|
||||
const migrations = [TestMigration];
|
||||
// @ts-expect-error property is readonly
|
||||
connectionOptions.migrations = migrations;
|
||||
const dataSource = mock<DataSource>();
|
||||
// @ts-expect-error property is readonly, and I can't pass them the `mock`
|
||||
// because `mock` will mock the down method and thus defeat the purpose
|
||||
// of this test, because the tested code will assume that the migration has a
|
||||
// down method.
|
||||
dataSource.migrations = migrations.map((M) => new M());
|
||||
const migrationsInCode = [new TestMigration()];
|
||||
const migrationsInDb: Migration[] = [{ id: 1, timestamp: Date.now(), name: 'TestMigration' }];
|
||||
const dataSource = mock<DataSource>({ migrations: migrationsInCode });
|
||||
|
||||
const migrationExecutor = mock<MigrationExecutor>();
|
||||
migrationExecutor.getExecutedMigrations.mockResolvedValue(migrationsInDb);
|
||||
|
||||
//
|
||||
// ACT
|
||||
//
|
||||
await main(connectionOptions, logger, function () {
|
||||
return dataSource;
|
||||
} as never);
|
||||
await main(logger, dataSource, migrationExecutor);
|
||||
|
||||
//
|
||||
// ASSERT
|
||||
//
|
||||
expect(logger.error).toHaveBeenCalledTimes(1);
|
||||
expect(logger.error).toBeCalledWith('Cancelled command. The last migration was irreversible.');
|
||||
expect(dataSource.undoLastMigration).not.toHaveBeenCalled();
|
||||
expect(dataSource.destroy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('print migration name instead of class name in error message if the migration has a name', async () => {
|
||||
//
|
||||
// ARRANGE
|
||||
//
|
||||
class TestMigration implements IrreversibleMigration {
|
||||
name = 'Migration Name';
|
||||
|
||||
async up() {}
|
||||
|
||||
down = undefined;
|
||||
}
|
||||
|
||||
const migrationsInCode = [new TestMigration()];
|
||||
const migrationsInDb: Migration[] = [{ id: 1, timestamp: Date.now(), name: 'Migration Name' }];
|
||||
const dataSource = mock<DataSource>({ migrations: migrationsInCode });
|
||||
|
||||
const migrationExecutor = mock<MigrationExecutor>();
|
||||
migrationExecutor.getExecutedMigrations.mockResolvedValue(migrationsInDb);
|
||||
|
||||
//
|
||||
// ACT
|
||||
//
|
||||
await main(logger, dataSource, migrationExecutor);
|
||||
|
||||
//
|
||||
// ASSERT
|
||||
//
|
||||
expect(logger.error).toHaveBeenCalledTimes(1);
|
||||
expect(logger.error).toHaveBeenCalledWith(
|
||||
'The last migration was irreversible and cannot be reverted.',
|
||||
'Cancelled command. The last migration "Migration Name" was irreversible.',
|
||||
);
|
||||
expect(dataSource.undoLastMigration).not.toHaveBeenCalled();
|
||||
expect(dataSource.destroy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("don't revert the last migration if we cannot find the migration in the code", async () => {
|
||||
//
|
||||
// ARRANGE
|
||||
//
|
||||
|
||||
const migrationsInDb: Migration[] = [{ id: 1, timestamp: Date.now(), name: 'TestMigration' }];
|
||||
const dataSource = mock<DataSource>({ migrations: [] });
|
||||
|
||||
const migrationExecutor = mock<MigrationExecutor>();
|
||||
migrationExecutor.getExecutedMigrations.mockResolvedValue(migrationsInDb);
|
||||
|
||||
//
|
||||
// ACT
|
||||
//
|
||||
await main(logger, dataSource, migrationExecutor);
|
||||
|
||||
//
|
||||
// ASSERT
|
||||
//
|
||||
expect(logger.error).toHaveBeenCalledTimes(2);
|
||||
expect(logger.error).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'The last migration that was executed is "TestMigration", but I could not find that migration\'s code in the currently installed version of n8n.',
|
||||
);
|
||||
expect(logger.error).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'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.',
|
||||
);
|
||||
expect(dataSource.undoLastMigration).not.toHaveBeenCalled();
|
||||
expect(dataSource.destroy).not.toHaveBeenCalled();
|
||||
@@ -79,22 +142,25 @@ test('revert the last migration if it has a down migration', async () => {
|
||||
// ARRANGE
|
||||
//
|
||||
class TestMigration implements ReversibleMigration {
|
||||
name = 'ReversibleMigration';
|
||||
|
||||
async up() {}
|
||||
|
||||
async down() {}
|
||||
}
|
||||
|
||||
const connectionOptions = DbConfig.getConnectionOptions();
|
||||
// @ts-expect-error property is readonly
|
||||
connectionOptions.migrations = [TestMigration];
|
||||
const migrationsInDb: Migration[] = [
|
||||
{ id: 1, timestamp: Date.now(), name: 'ReversibleMigration' },
|
||||
];
|
||||
const dataSource = mock<DataSource>({ migrations: [new TestMigration()] });
|
||||
|
||||
const migrationExecutor = mock<MigrationExecutor>();
|
||||
migrationExecutor.getExecutedMigrations.mockResolvedValue(migrationsInDb);
|
||||
|
||||
//
|
||||
// ACT
|
||||
//
|
||||
await main(connectionOptions, logger, function () {
|
||||
return dataSource;
|
||||
} as never);
|
||||
await main(logger, dataSource, migrationExecutor);
|
||||
|
||||
//
|
||||
// ASSERT
|
||||
@@ -103,32 +169,3 @@ test('revert the last migration if it has a down migration', async () => {
|
||||
expect(dataSource.undoLastMigration).toHaveBeenCalled();
|
||||
expect(dataSource.destroy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('throw if a migration is invalid, e.g. has no `up` method', async () => {
|
||||
//
|
||||
// ARRANGE
|
||||
//
|
||||
class TestMigration {}
|
||||
|
||||
const connectionOptions = DbConfig.getConnectionOptions();
|
||||
// @ts-expect-error property is readonly
|
||||
connectionOptions.migrations = [TestMigration];
|
||||
const dataSource = mock<DataSource>({ migrations: [new TestMigration()] });
|
||||
|
||||
//
|
||||
// ACT
|
||||
//
|
||||
await expect(
|
||||
main(connectionOptions, logger, function () {
|
||||
return dataSource;
|
||||
} as never),
|
||||
).rejects.toThrowError(
|
||||
'At least on migration is missing the method `up`. Make sure all migrations are valid.',
|
||||
);
|
||||
|
||||
//
|
||||
// ASSERT
|
||||
//
|
||||
expect(dataSource.undoLastMigration).not.toHaveBeenCalled();
|
||||
expect(dataSource.destroy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user