Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add check for migrations command (#3923)
Allow checking for migrations. This would be useful for adding to git hooks to ensure that any changes to ORM entities are captured in migrations.
- Loading branch information
Showing
6 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
(global as any).process.env.FORCE_COLOR = 0; | ||
|
||
import { Migrator } from '@mikro-orm/migrations'; | ||
import { MikroORM } from '@mikro-orm/core'; | ||
import type { SqliteDriver } from '@mikro-orm/sqlite'; | ||
import { CLIHelper } from '@mikro-orm/cli'; | ||
import { MigrationCommandFactory } from '../../../packages/cli/src/commands/MigrationCommandFactory'; | ||
import { initORMSqlite } from '../../bootstrap'; | ||
|
||
const closeSpy = jest.spyOn(MikroORM.prototype, 'close'); | ||
jest.spyOn(CLIHelper, 'showHelp').mockImplementation(() => void 0); | ||
const checkMigrationMock = jest.spyOn(Migrator.prototype, 'checkMigrationNeeded'); | ||
checkMigrationMock.mockResolvedValue(true); | ||
const dumpMock = jest.spyOn(CLIHelper, 'dump'); | ||
dumpMock.mockImplementation(() => void 0); | ||
|
||
describe('CheckMigrationCommand', () => { | ||
|
||
let orm: MikroORM<SqliteDriver>; | ||
|
||
beforeAll(async () => { | ||
orm = await initORMSqlite(); | ||
const getORMMock = jest.spyOn(CLIHelper, 'getORM'); | ||
getORMMock.mockResolvedValue(orm); | ||
}); | ||
|
||
afterAll(async () => await orm.close(true)); | ||
|
||
test('builder', async () => { | ||
const cmd = MigrationCommandFactory.create('check'); | ||
const args = { option: jest.fn() }; | ||
cmd.builder(args as any); | ||
}); | ||
|
||
test('handler', async () => { | ||
const cmd = MigrationCommandFactory.create('check'); | ||
|
||
const mockExit = jest.spyOn(process, 'exit').mockImplementationOnce(() => { throw new Error('Mock'); }); | ||
|
||
await expect(cmd.handler({} as any)).rejects.toThrowError('Mock'); | ||
expect(checkMigrationMock.mock.calls.length).toBe(1); | ||
expect(closeSpy).toBeCalledTimes(1); | ||
expect(dumpMock).toHaveBeenLastCalledWith('Changes detected. Please create migration to update schema.'); | ||
expect(mockExit).toBeCalledTimes(1); | ||
|
||
checkMigrationMock.mockImplementationOnce(async () => false); | ||
await expect(cmd.handler({} as any)).resolves.toBeUndefined(); | ||
expect(checkMigrationMock.mock.calls.length).toBe(2); | ||
expect(closeSpy).toBeCalledTimes(2); | ||
expect(dumpMock).toHaveBeenLastCalledWith('No changes required, schema is up-to-date'); | ||
}); | ||
}); |