Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redundant undefined parameters are not generated in migration files anymore #5690

Merged
merged 2 commits into from May 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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