From 8adb99bfdf2d40b18c235775e08cd70f75a90449 Mon Sep 17 00:00:00 2001 From: Oleg Date: Sat, 16 May 2020 19:50:43 +0300 Subject: [PATCH] fix: redundant undefined parameters are not generated in migration files anymore (#5690) * fix: redundant undefined parameters are not generated anymore * fixed lint issues --- src/commands/MigrationGenerateCommand.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/commands/MigrationGenerateCommand.ts b/src/commands/MigrationGenerateCommand.ts index cb99555c4cc..faf6edd382c 100644 --- a/src/commands/MigrationGenerateCommand.ts +++ b/src/commands/MigrationGenerateCommand.ts @@ -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) + ");"); }); } @@ -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. */