Skip to content

Commit

Permalink
feat(cli): add database:import command to run external sql dumps
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Nov 18, 2019
1 parent 6e58332 commit aea3614
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/cli/CLIHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { MigrationCommandFactory } from './MigrationCommandFactory';
import { DebugCommand } from './DebugCommand';
import { Dictionary } from '../types';
import { GenerateCacheCommand } from './GenerateCacheCommand';
import { ImportCommand } from './ImportCommand';

export class CLIHelper {

Expand Down Expand Up @@ -65,6 +66,7 @@ export class CLIHelper {
.command(new ClearCacheCommand())
.command(new GenerateCacheCommand())
.command(new GenerateEntitiesCommand())
.command(new ImportCommand())
.command(SchemaCommandFactory.create('create'))
.command(SchemaCommandFactory.create('drop'))
.command(SchemaCommandFactory.create('update'))
Expand Down
20 changes: 20 additions & 0 deletions lib/cli/ImportCommand.ts
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);
}

}
2 changes: 2 additions & 0 deletions tests/cli/CLIHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('CLIHelper', () => {
'cache:clear',
'cache:generate',
'generate-entities',
'database:import',
'schema:create',
'schema:drop',
'schema:update',
Expand All @@ -49,6 +50,7 @@ describe('CLIHelper', () => {
'cache:clear',
'cache:generate',
'generate-entities',
'database:import',
'schema:create',
'schema:drop',
'schema:update',
Expand Down
24 changes: 24 additions & 0 deletions tests/cli/ImportCommand.test.ts
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);
});

});

0 comments on commit aea3614

Please sign in to comment.