Skip to content

Commit

Permalink
fix: redundant undefined parameters are not generated in migration fi…
Browse files Browse the repository at this point in the history
…les anymore (#5690)

* fix: redundant undefined parameters are not generated anymore

* fixed lint issues
  • Loading branch information
olegmdev committed May 16, 2020
1 parent 265d1ae commit d5cde49
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/commands/MigrationGenerateCommand.ts
Expand Up @@ -82,17 +82,17 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
// we are using simple quoted string instead of template string syntax
if (connection.driver instanceof MysqlDriver || connection.driver instanceof AuroraDataApiDriver) {
sqlInMemory.upQueries.forEach(upQuery => {
upSqls.push(" await queryRunner.query(\"" + upQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\", " + JSON.stringify(upQuery.parameters) + ");");
upSqls.push(" await queryRunner.query(\"" + upQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\"" + MigrationGenerateCommand.queryParams(upQuery.parameters) + ");");
});
sqlInMemory.downQueries.forEach(downQuery => {
downSqls.push(" await queryRunner.query(\"" + downQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\", " + JSON.stringify(downQuery.parameters) + ");");
downSqls.push(" await queryRunner.query(\"" + downQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\"" + MigrationGenerateCommand.queryParams(downQuery.parameters) + ");");
});
} else {
sqlInMemory.upQueries.forEach(upQuery => {
upSqls.push(" await queryRunner.query(`" + upQuery.query.replace(new RegExp("`", "g"), "\\`") + "`, " + JSON.stringify(upQuery.parameters) + ");");
upSqls.push(" await queryRunner.query(`" + upQuery.query.replace(new RegExp("`", "g"), "\\`") + "`" + MigrationGenerateCommand.queryParams(upQuery.parameters) + ");");
});
sqlInMemory.downQueries.forEach(downQuery => {
downSqls.push(" await queryRunner.query(`" + downQuery.query.replace(new RegExp("`", "g"), "\\`") + "`, " + JSON.stringify(downQuery.parameters) + ");");
downSqls.push(" await queryRunner.query(`" + downQuery.query.replace(new RegExp("`", "g"), "\\`") + "`" + MigrationGenerateCommand.queryParams(downQuery.parameters) + ");");
});
}

Expand Down Expand Up @@ -124,6 +124,17 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
// Protected Static Methods
// -------------------------------------------------------------------------

/**
* Formats query parameters for migration queries if parameters actually exist
*/
protected static queryParams(parameters: any[] | undefined): string {
if (!parameters || !parameters.length) {
return "";
}

return `, ${JSON.stringify(parameters)}`;
}

/**
* Gets contents of the migration file.
*/
Expand Down

0 comments on commit d5cde49

Please sign in to comment.