-
-
Notifications
You must be signed in to change notification settings - Fork 544
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
database:import
command to run external sql dumps
- Loading branch information
Showing
4 changed files
with
48 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Arguments, CommandModule } from 'yargs'; | ||
import chalk from 'chalk'; | ||
|
||
import { CLIHelper } from './CLIHelper'; | ||
import { MikroORM } from '../MikroORM'; | ||
import { AbstractSqlDriver } from '../drivers'; | ||
|
||
export class ImportCommand implements CommandModule { | ||
|
||
command = 'database:import <file>'; | ||
describe = 'Imports the SQL file to the database'; | ||
|
||
async handler(args: Arguments) { | ||
const orm = await CLIHelper.getORM() as MikroORM<AbstractSqlDriver>; | ||
await orm.em.getConnection().loadFile(args.file as string); | ||
CLIHelper.dump(chalk.green(`File ${args.file} successfully imported`)); | ||
await orm.close(true); | ||
} | ||
|
||
} |
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,24 @@ | ||
import { Configuration } from '../../lib/utils'; | ||
|
||
const close = jest.fn(); | ||
const connection = { loadFile: jest.fn() }; | ||
const em = { getConnection: () => connection }; | ||
const config = new Configuration({} as any, false); | ||
const getORM = async () => ({ em, config, close }); | ||
jest.mock('../../lib/cli/CLIHelper', () => ({ CLIHelper: { getORM, dump: jest.fn() } })); | ||
|
||
(global as any).console.log = jest.fn(); | ||
|
||
import { ImportCommand } from '../../lib/cli/ImportCommand'; | ||
|
||
describe('ImportDatabaseCommand', () => { | ||
|
||
test('handler', async () => { | ||
const cmd = new ImportCommand(); | ||
|
||
await expect(cmd.handler({} as any)).resolves.toBeUndefined(); | ||
expect(close.mock.calls.length).toBe(1); | ||
expect(connection.loadFile.mock.calls.length).toBe(1); | ||
}); | ||
|
||
}); |