Skip to content

Commit

Permalink
feat: add check and dryrun to migration generate (#7275)
Browse files Browse the repository at this point in the history
Adds support for “check” and “drynrun” modes to the migration generate command.

Fixes #3037
Refs #6978
  • Loading branch information
holm authored Feb 24, 2021
1 parent 7f06e44 commit d6df200
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions src/commands/MigrationGenerateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
type: "boolean",
default: false,
describe: "Generate a migration file on Javascript instead of Typescript",
})
.option("dr", {
alias: "dryrun",
type: "boolean",
default: false,
describe: "Prints out the contents of the migration instead of writing it to a file",
})
.option("ch", {
alias: "check",
type: "boolean",
default: false,
describe: "Verifies that the current database is up to date and that no migrations are needed. Otherwise exits with code 1.",
});
}

Expand Down Expand Up @@ -124,22 +136,36 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
await connection.close();
}

if (upSqls.length) {
if (args.name) {
const fileContent = args.outputJs ?
MigrationGenerateCommand.getJavascriptTemplate(args.name as any, timestamp, upSqls, downSqls.reverse()) :
MigrationGenerateCommand.getTemplate(args.name as any, timestamp, upSqls, downSqls.reverse());
const path = process.cwd() + "/" + (directory ? (directory + "/") : "") + filename;
await CommandUtils.createFile(path, fileContent);

console.log(chalk.green(`Migration ${chalk.blue(path)} has been generated successfully.`));
if (!upSqls.length) {
if (args.check) {
console.log(chalk.green(`No changes in database schema were found`));
process.exit(0);
} else {
console.log(chalk.yellow("Please specify a migration name using the `-n` argument"));
console.log(chalk.yellow(`No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command`));
process.exit(1);
}
} else {
console.log(chalk.yellow(`No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command`));
} else if (!args.name) {
console.log(chalk.yellow("Please specify a migration name using the `-n` argument"));
process.exit(1);
}

const fileContent = args.outputJs ?
MigrationGenerateCommand.getJavascriptTemplate(args.name as any, timestamp, upSqls, downSqls.reverse()) :
MigrationGenerateCommand.getTemplate(args.name as any, timestamp, upSqls, downSqls.reverse());
const path = process.cwd() + "/" + (directory ? (directory + "/") : "") + filename;

if (args.check) {
console.log(chalk.yellow(`Unexpected changes in database schema were found in check mode:\n\n${chalk.white(fileContent)}`));
process.exit(1);
}

if (args.dryrun) {
console.log(chalk.green(`Migration ${chalk.blue(path)} has content:\n\n${chalk.white(fileContent)}`));
} else {
await CommandUtils.createFile(path, fileContent);

console.log(chalk.green(`Migration ${chalk.blue(path)} has been generated successfully.`));
}
} catch (err) {
console.log(chalk.black.bgRed("Error during migration generation:"));
console.error(err);
Expand Down

0 comments on commit d6df200

Please sign in to comment.