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: create typeorm_metatable when running migrations #4956

Merged
merged 14 commits into from Nov 14, 2021
Merged
7 changes: 7 additions & 0 deletions src/commands/MigrationRunCommand.ts
Expand Up @@ -3,6 +3,7 @@ import {ConnectionOptionsReader} from "../connection/ConnectionOptionsReader";
import {Connection} from "../connection/Connection";
import * as process from "process";
import * as yargs from "yargs";
import {RdbmsSchemaBuilder} from "../schema-builder/RdbmsSchemaBuilder";
const chalk = require("chalk");

/**
Expand Down Expand Up @@ -73,6 +74,12 @@ export class MigrationRunCommand implements yargs.CommandModule {
// noop
}

const schemaBuilder = connection.driver.createSchemaBuilder();

if (schemaBuilder instanceof RdbmsSchemaBuilder) {
await schemaBuilder.createMetadataTableIfNecessary();
}
BitPatty marked this conversation as resolved.
Show resolved Hide resolved

await connection.runMigrations(options);
await connection.close();
// exit process if no errors
Expand Down
20 changes: 12 additions & 8 deletions src/schema-builder/RdbmsSchemaBuilder.ts
Expand Up @@ -70,10 +70,7 @@ export class RdbmsSchemaBuilder implements SchemaBuilder {
await this.queryRunner.startTransaction();
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.createMetadataTableIfNecessary();
await this.queryRunner.getTables(tablePaths);
await this.queryRunner.getViews([]);
await this.executeSchemaSyncOperationsInProperOrder();
Expand All @@ -98,17 +95,24 @@ export class RdbmsSchemaBuilder implements SchemaBuilder {
}
}

/**
* If the schema contains views, create the typeorm_metadata table if it doesn't exist yet
*/
async createMetadataTableIfNecessary(): Promise<void> {
if (!this.queryRunner) this.queryRunner = this.connection.createQueryRunner("master");
BitPatty marked this conversation as resolved.
Show resolved Hide resolved
if (this.viewEntityToSyncMetadatas.length > 0) {
await this.createTypeormMetadataTable();
}
}

/**
* Returns sql queries to be executed by schema builder.
*/
async log(): Promise<SqlInMemory> {
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.createMetadataTableIfNecessary();
await this.queryRunner.getTables(tablePaths);
await this.queryRunner.getViews([]);
this.queryRunner.enableSqlMemory();
Expand Down