From c52b3d225c1b4a621f3239f5e3babd4a70ccc0a5 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Fri, 13 Sep 2019 13:04:11 +0300 Subject: [PATCH] fix: views generating broken Migrations (#4726) MigrationGenerateCommand didn't apply parameters to query and information about ViewTables couldn't be inserted into typeorm_metadata table. Also added code that creates typeorm_metadata table if ViewTables exists Fixed issue #4123 --- src/commands/MigrationGenerateCommand.ts | 8 ++++---- src/schema-builder/RdbmsSchemaBuilder.ts | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/commands/MigrationGenerateCommand.ts b/src/commands/MigrationGenerateCommand.ts index 3816ccd78a..375f5b518b 100644 --- a/src/commands/MigrationGenerateCommand.ts +++ b/src/commands/MigrationGenerateCommand.ts @@ -81,17 +81,17 @@ export class MigrationGenerateCommand implements yargs.CommandModule { // we are using simple quoted string instead of template string syntax if (connection.driver instanceof MysqlDriver) { sqlInMemory.upQueries.forEach(upQuery => { - upSqls.push(" await queryRunner.query(\"" + upQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\");"); + upSqls.push(" await queryRunner.query(\"" + upQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\", " + JSON.stringify(upQuery.parameters) + ");"); }); sqlInMemory.downQueries.forEach(downQuery => { - downSqls.push(" await queryRunner.query(\"" + downQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\");"); + downSqls.push(" await queryRunner.query(\"" + downQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\", " + JSON.stringify(downQuery.parameters) + ");"); }); } else { sqlInMemory.upQueries.forEach(upQuery => { - upSqls.push(" await queryRunner.query(`" + upQuery.query.replace(new RegExp("`", "g"), "\\`") + "`);"); + upSqls.push(" await queryRunner.query(`" + upQuery.query.replace(new RegExp("`", "g"), "\\`") + "`, " + JSON.stringify(upQuery.parameters) + ");"); }); sqlInMemory.downQueries.forEach(downQuery => { - downSqls.push(" await queryRunner.query(`" + downQuery.query.replace(new RegExp("`", "g"), "\\`") + "`);"); + downSqls.push(" await queryRunner.query(`" + downQuery.query.replace(new RegExp("`", "g"), "\\`") + "`, " + JSON.stringify(downQuery.parameters) + ");"); }); } diff --git a/src/schema-builder/RdbmsSchemaBuilder.ts b/src/schema-builder/RdbmsSchemaBuilder.ts index d09909e0d0..ba2d2b28f6 100644 --- a/src/schema-builder/RdbmsSchemaBuilder.ts +++ b/src/schema-builder/RdbmsSchemaBuilder.ts @@ -104,6 +104,10 @@ export class RdbmsSchemaBuilder implements SchemaBuilder { this.queryRunner = this.connection.createQueryRunner("master"); try { const tablePaths = this.entityToSyncMetadatas.map(metadata => metadata.tablePath); + // TODO: typeorm_metadata table needs only for Views for now. + // Remove condition or add new conditions if necessary (for CHECK constraints for example). + if (this.viewEntityToSyncMetadatas.length > 0) + await this.createTypeormMetadataTable(); await this.queryRunner.getTables(tablePaths); await this.queryRunner.getViews([]); this.queryRunner.enableSqlMemory();