Skip to content

Commit

Permalink
feat(cli): add --drop-db flag to migration:fresh and schema:fresh
Browse files Browse the repository at this point in the history
Closes #4569
  • Loading branch information
B4nan committed Aug 9, 2023
1 parent a600f55 commit cf1db80
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/cli/src/commands/MigrationCommandFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export class MigrationCommandFactory {
type: 'string',
desc: 'Allows to seed the database after dropping it and rerunning all migrations',
});
args.option('drop-db', {
type: 'boolean',
desc: 'Drop the whole database',
});
}

private static async handleUpDownCommand(args: ArgumentsCamelCase<Options>, migrator: IMigrator, method: MigratorMethod) {
Expand Down Expand Up @@ -184,7 +188,7 @@ export class MigrationCommandFactory {

private static async handleFreshCommand(args: ArgumentsCamelCase<Options>, migrator: IMigrator, orm: MikroORM) {
const generator = orm.getSchemaGenerator();
await generator.dropSchema({ dropMigrationsTable: true });
await generator.dropSchema({ dropMigrationsTable: true, dropDb: args.dropDb });
CLIHelper.dump(colors.green('Dropped schema successfully'));
const opts = MigrationCommandFactory.getUpDownOptions(args);
await migrator.up(opts);
Expand Down Expand Up @@ -243,4 +247,4 @@ export class MigrationCommandFactory {
type MigratorMethod = 'create' | 'check' | 'up' | 'down' | 'list' | 'pending' | 'fresh';
type CliUpDownOptions = { to?: string | number; from?: string | number; only?: string };
type GenerateOptions = { dump?: boolean; blank?: boolean; initial?: boolean; path?: string; disableFkChecks?: boolean; seed: string; name?: string };
type Options = GenerateOptions & CliUpDownOptions;
type Options = GenerateOptions & CliUpDownOptions & { dropDb?: boolean };
5 changes: 4 additions & 1 deletion packages/cli/src/commands/SchemaCommandFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class SchemaCommandFactory {
desc: 'Set the current schema for wildcard schema entities',
});

if (command === 'create' || command === 'fresh') {
if (['create', 'fresh'].includes(command)) {
args.option('seed', {
type: 'string',
desc: 'Allows to seed the database on create or drop and recreate',
Expand All @@ -78,6 +78,9 @@ export class SchemaCommandFactory {
type: 'boolean',
desc: 'Drop also migrations table',
});
}

if (['drop', 'fresh'].includes(command)) {
args.option('drop-db', {
type: 'boolean',
desc: 'Drop the whole database',
Expand Down
4 changes: 3 additions & 1 deletion tests/features/cli/CLIHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,15 @@ Maybe you want to check, or regenerate your yarn.lock or package-lock.json file?
test('builder (schema fresh)', async () => {
const args = { option: jest.fn() };
SchemaCommandFactory.configureSchemaCommand(args as any, 'fresh');
expect(args.option.mock.calls).toHaveLength(3);
expect(args.option.mock.calls).toHaveLength(4);
expect(args.option.mock.calls[0][0]).toBe('r');
expect(args.option.mock.calls[0][1]).toMatchObject({ alias: 'run', type: 'boolean' });
expect(args.option.mock.calls[1][0]).toBe('schema');
expect(args.option.mock.calls[1][1]).toMatchObject({ type: 'string' });
expect(args.option.mock.calls[2][0]).toBe('seed');
expect(args.option.mock.calls[2][1]).toMatchObject({ type: 'string' });
expect(args.option.mock.calls[3][0]).toBe('drop-db');
expect(args.option.mock.calls[3][1]).toMatchObject({ type: 'boolean' });
});

test('dump', async () => {
Expand Down

0 comments on commit cf1db80

Please sign in to comment.