fix: redundant undefined parameters are not generated in migration fi… · typeorm/typeorm@d5cde49
@@ -82,17 +82,17 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
8282// we are using simple quoted string instead of template string syntax
8383if (connection.driver instanceof MysqlDriver || connection.driver instanceof AuroraDataApiDriver) {
8484sqlInMemory.upQueries.forEach(upQuery => {
85-upSqls.push(" await queryRunner.query(\"" + upQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\", " + JSON.stringify(upQuery.parameters) + ");");
85+upSqls.push(" await queryRunner.query(\"" + upQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\"" + MigrationGenerateCommand.queryParams(upQuery.parameters) + ");");
8686});
8787sqlInMemory.downQueries.forEach(downQuery => {
88-downSqls.push(" await queryRunner.query(\"" + downQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\", " + JSON.stringify(downQuery.parameters) + ");");
88+downSqls.push(" await queryRunner.query(\"" + downQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\"" + MigrationGenerateCommand.queryParams(downQuery.parameters) + ");");
8989});
9090} else {
9191sqlInMemory.upQueries.forEach(upQuery => {
92-upSqls.push(" await queryRunner.query(`" + upQuery.query.replace(new RegExp("`", "g"), "\\`") + "`, " + JSON.stringify(upQuery.parameters) + ");");
92+upSqls.push(" await queryRunner.query(`" + upQuery.query.replace(new RegExp("`", "g"), "\\`") + "`" + MigrationGenerateCommand.queryParams(upQuery.parameters) + ");");
9393});
9494sqlInMemory.downQueries.forEach(downQuery => {
95-downSqls.push(" await queryRunner.query(`" + downQuery.query.replace(new RegExp("`", "g"), "\\`") + "`, " + JSON.stringify(downQuery.parameters) + ");");
95+downSqls.push(" await queryRunner.query(`" + downQuery.query.replace(new RegExp("`", "g"), "\\`") + "`" + MigrationGenerateCommand.queryParams(downQuery.parameters) + ");");
9696});
9797}
9898@@ -124,6 +124,17 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
124124// Protected Static Methods
125125// -------------------------------------------------------------------------
126126127+/**
128+ * Formats query parameters for migration queries if parameters actually exist
129+ */
130+protected static queryParams(parameters: any[] | undefined): string {
131+if (!parameters || !parameters.length) {
132+return "";
133+}
134+135+return `, ${JSON.stringify(parameters)}`;
136+}
137+127138/**
128139 * Gets contents of the migration file.
129140 */