Skip to content
This repository was archived by the owner on Mar 18, 2022. It is now read-only.

Commit c52b3d2

Browse files
evgeniytarpleerock
authored andcommitted
fix: views generating broken Migrations (typeorm#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 typeorm#4123
1 parent 1d73a90 commit c52b3d2

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/commands/MigrationGenerateCommand.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,17 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
8181
// we are using simple quoted string instead of template string syntax
8282
if (connection.driver instanceof MysqlDriver) {
8383
sqlInMemory.upQueries.forEach(upQuery => {
84-
upSqls.push(" await queryRunner.query(\"" + upQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\");");
84+
upSqls.push(" await queryRunner.query(\"" + upQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\", " + JSON.stringify(upQuery.parameters) + ");");
8585
});
8686
sqlInMemory.downQueries.forEach(downQuery => {
87-
downSqls.push(" await queryRunner.query(\"" + downQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\");");
87+
downSqls.push(" await queryRunner.query(\"" + downQuery.query.replace(new RegExp(`"`, "g"), `\\"`) + "\", " + JSON.stringify(downQuery.parameters) + ");");
8888
});
8989
} else {
9090
sqlInMemory.upQueries.forEach(upQuery => {
91-
upSqls.push(" await queryRunner.query(`" + upQuery.query.replace(new RegExp("`", "g"), "\\`") + "`);");
91+
upSqls.push(" await queryRunner.query(`" + upQuery.query.replace(new RegExp("`", "g"), "\\`") + "`, " + JSON.stringify(upQuery.parameters) + ");");
9292
});
9393
sqlInMemory.downQueries.forEach(downQuery => {
94-
downSqls.push(" await queryRunner.query(`" + downQuery.query.replace(new RegExp("`", "g"), "\\`") + "`);");
94+
downSqls.push(" await queryRunner.query(`" + downQuery.query.replace(new RegExp("`", "g"), "\\`") + "`, " + JSON.stringify(downQuery.parameters) + ");");
9595
});
9696
}
9797

src/schema-builder/RdbmsSchemaBuilder.ts

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ export class RdbmsSchemaBuilder implements SchemaBuilder {
104104
this.queryRunner = this.connection.createQueryRunner("master");
105105
try {
106106
const tablePaths = this.entityToSyncMetadatas.map(metadata => metadata.tablePath);
107+
// TODO: typeorm_metadata table needs only for Views for now.
108+
// Remove condition or add new conditions if necessary (for CHECK constraints for example).
109+
if (this.viewEntityToSyncMetadatas.length > 0)
110+
await this.createTypeormMetadataTable();
107111
await this.queryRunner.getTables(tablePaths);
108112
await this.queryRunner.getViews([]);
109113
this.queryRunner.enableSqlMemory();

0 commit comments

Comments
 (0)