Skip to content

Consistent migration lifecycle to ensure beforeMigration and afterMigration are always called #16

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 55 additions & 56 deletions src/migration/MigrationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ export class MigrationExecutor {
public async executeMigration(migration: Migration): Promise<Migration> {
return this.withQueryRunner(async (queryRunner) => {
await this.createMigrationsTableIfNotExist(queryRunner)

// create typeorm_metadata table if it's not created yet
const schemaBuilder = this.connection.driver.createSchemaBuilder()
if (InstanceChecker.isRdbmsSchemaBuilder(schemaBuilder)) {
await schemaBuilder.createMetadataTableIfNecessary(queryRunner)
}
await this.createMetadataTableIfNotExist(queryRunner)

await queryRunner.beforeMigration()
await (migration.instance as any).up(queryRunner)
await queryRunner.afterMigration()
try {
await (migration.instance as any).up(queryRunner)
} finally {
await queryRunner.afterMigration()
}
await this.insertExecutedMigration(queryRunner, migration)

return migration
Expand Down Expand Up @@ -149,7 +147,6 @@ export class MigrationExecutor {
let hasUnappliedMigrations = false
const queryRunner =
this.queryRunner || this.connection.createQueryRunner()
// create migrations table if its not created yet
await this.createMigrationsTableIfNotExist(queryRunner)

// get all migrations that are executed and saved in the database
Expand Down Expand Up @@ -191,14 +188,8 @@ export class MigrationExecutor {
async executePendingMigrations(): Promise<Migration[]> {
const queryRunner =
this.queryRunner || this.connection.createQueryRunner()
// create migrations table if it's not created yet
await this.createMigrationsTableIfNotExist(queryRunner)

// create the typeorm_metadata table if it's not created yet
const schemaBuilder = this.connection.driver.createSchemaBuilder()
if (InstanceChecker.isRdbmsSchemaBuilder(schemaBuilder)) {
await schemaBuilder.createMetadataTableIfNecessary(queryRunner)
}
await this.createMetadataTableIfNotExist(queryRunner)

// get all migrations that are executed and saved in the database
const executedMigrations = await this.loadExecutedMigrations(
Expand Down Expand Up @@ -333,42 +324,40 @@ export class MigrationExecutor {
transactionStartedByUs = true
}

await migration
.instance!.up(queryRunner)
.catch((error) => {
// informative log about migration failure
this.connection.logger.logMigration(
`Migration "${migration.name}" failed, error: ${error?.message}`,
)
throw error
})
.then(async () => {
// now when migration is executed we need to insert record about it into the database
await this.insertExecutedMigration(
queryRunner,
migration,
)
// commit transaction if we started it
if (migration.transaction && transactionStartedByUs) {
await queryRunner.commitTransaction()
await queryRunner.afterMigration()
}
})
.then(() => {
// informative log about migration success
successMigrations.push(migration)
this.connection.logger.logSchemaBuild(
`Migration ${migration.name} has been ${
this.fake ? "(fake)" : ""
} executed successfully.`,
)
})
try {
await migration.instance!.up(queryRunner)

// Now when migration is executed we need to insert record about it into the database
await this.insertExecutedMigration(queryRunner, migration)

// Commit transaction if we started it
if (migration.transaction && transactionStartedByUs) {
await queryRunner.commitTransaction()
}

// Log success and track the successful migration
successMigrations.push(migration)
this.connection.logger.logSchemaBuild(
`Migration ${migration.name} has been ${
this.fake ? "(fake)" : ""
} executed successfully.`,
)
} catch (error) {
// Informative log about migration failure
this.connection.logger.logMigration(
`Migration "${migration.name}" failed, error: ${error?.message}`,
)
throw error
} finally {
if (migration.transaction && transactionStartedByUs) {
await queryRunner.afterMigration()
}
}
}

// commit transaction if we started it
if (this.transaction === "all" && transactionStartedByUs) {
await queryRunner.commitTransaction()
await queryRunner.afterMigration()
}
} catch (err) {
// rollback transaction if we started it
Expand All @@ -381,6 +370,9 @@ export class MigrationExecutor {

throw err
} finally {
if (this.transaction === "all" && transactionStartedByUs) {
await queryRunner.afterMigration()
}
// if query runner was created by us then release it
if (!this.queryRunner) await queryRunner.release()
}
Expand All @@ -394,14 +386,8 @@ export class MigrationExecutor {
const queryRunner =
this.queryRunner || this.connection.createQueryRunner()

// create migrations table if it's not created yet
await this.createMigrationsTableIfNotExist(queryRunner)

// create typeorm_metadata table if it's not created yet
const schemaBuilder = this.connection.driver.createSchemaBuilder()
if (InstanceChecker.isRdbmsSchemaBuilder(schemaBuilder)) {
await schemaBuilder.createMetadataTableIfNecessary(queryRunner)
}
await this.createMetadataTableIfNotExist(queryRunner)

// get all migrations that are executed and saved in the database
const executedMigrations = await this.loadExecutedMigrations(
Expand Down Expand Up @@ -457,8 +443,11 @@ export class MigrationExecutor {
try {
if (!this.fake) {
await queryRunner.beforeMigration()
await migrationToRevert.instance!.down(queryRunner)
await queryRunner.afterMigration()
try {
await migrationToRevert.instance!.down(queryRunner)
} finally {
await queryRunner.afterMigration()
}
}

await this.deleteExecutedMigration(queryRunner, migrationToRevert)
Expand Down Expand Up @@ -542,6 +531,16 @@ export class MigrationExecutor {
}
}

/**
* Create the "typeorm_metadata" table if it's not created yet
*/
private async createMetadataTableIfNotExist(queryRunner: QueryRunner) {
const schemaBuilder = this.connection.driver.createSchemaBuilder()
if (InstanceChecker.isRdbmsSchemaBuilder(schemaBuilder)) {
await schemaBuilder.createMetadataTableIfNecessary(queryRunner)
}
}

/**
* Loads all migrations that were executed and saved into the database (sorts by id).
*/
Expand Down