Skip to content

Commit

Permalink
feat: export Migration Execution API from main package (fixes typeorm…
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardofalk committed Oct 10, 2019
1 parent 488d210 commit 632a132
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ export {DeleteResult} from "./query-builder/result/DeleteResult";
export {QueryRunner} from "./query-runner/QueryRunner";
export {EntityManager} from "./entity-manager/EntityManager";
export {MongoEntityManager} from "./entity-manager/MongoEntityManager";
export {MigrationInterface} from "./migration/MigrationInterface";
export {Migration} from "./migration/Migration";
export {MigrationExecutor} from "./migration/MigrationExecutor";
export {MigrationInterface } from "./migration/MigrationInterface";
export {DefaultNamingStrategy} from "./naming-strategy/DefaultNamingStrategy";
export {NamingStrategyInterface} from "./naming-strategy/NamingStrategyInterface";
export {Repository} from "./repository/Repository";
Expand Down
69 changes: 64 additions & 5 deletions src/migration/MigrationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,40 @@ export class MigrationExecutor {
// Public Methods
// -------------------------------------------------------------------------

public async executeMigration(migration: Migration): Promise<Migration> {
return this.withQueryRunner(async (queryRunner) => {
await this.createMigrationsTableIfNotExist(queryRunner);
await (migration.instance as any).up(queryRunner);
await this.insertExecutedMigration(queryRunner, migration);

return migration;
})
}

public async getAllMigrations(): Promise<Migration[]> {
return this.getMigrations()
}

public async getExecutedMigrations(): Promise<Migration[]> {
return this.withQueryRunner(async queryRunner => {
await this.createMigrationsTableIfNotExist(queryRunner);

return await this.loadExecutedMigrations(queryRunner);
});
}

public async getPendingMigrations(): Promise<Migration[]> {
const allMigrations = await this.getAllMigrations();
const executedMigrations = await this.getExecutedMigrations();

return allMigrations.filter(migration =>
executedMigrations.find(
executedMigration =>
executedMigration.name === migration.name
)
);
}

/**
* Lists all migrations and whether they have been executed or not
* returns true if there are unapplied migrations
Expand Down Expand Up @@ -315,9 +349,11 @@ export class MigrationExecutor {
protected getMigrations(): Migration[] {
const migrations = this.connection.migrations.map(migration => {
const migrationClassName = (migration.constructor as any).name;
const migrationTimestamp = parseInt(migrationClassName.substr(-13));
if (!migrationTimestamp)
const migrationTimestamp = parseInt(migrationClassName.substr(-13), 10);

if (!migrationTimestamp || isNaN(migrationTimestamp)) {
throw new Error(`${migrationClassName} migration name is wrong. Migration class name should have a JavaScript timestamp appended.`);
}

return new Migration(undefined, migrationTimestamp, migrationClassName, migration);
});
Expand All @@ -342,6 +378,12 @@ export class MigrationExecutor {
return sortedMigrations.length > 0 ? sortedMigrations[0] : undefined;
}

public insertMigration(migration: Migration) {
return this.withQueryRunner(async queryRunner => {
return await this.insertExecutedMigration(queryRunner, migration)
})
}

/**
* Inserts new executed migration's data into migrations table.
*/
Expand All @@ -354,9 +396,9 @@ export class MigrationExecutor {
values["timestamp"] = migration.timestamp;
values["name"] = migration.name;
}
if (this.connection.driver instanceof MongoDriver) {
if (this.connection.driver instanceof MongoDriver) {
const mongoRunner = queryRunner as MongoQueryRunner;
mongoRunner.databaseConnection.db(this.connection.driver.database!).collection(this.migrationsTableName).insert(values);
mongoRunner.databaseConnection.db(this.connection.driver.database!).collection(this.migrationsTableName).insert(values);
} else {
const qb = queryRunner.manager.createQueryBuilder();
await qb.insert()
Expand All @@ -366,6 +408,12 @@ export class MigrationExecutor {
}
}

public deleteMigration(migration: Migration) {
return this.withQueryRunner(async queryRunner => {
return await this.deleteExecutedMigration(queryRunner, migration);
})
}

/**
* Delete previously executed migration's data from the migrations table.
*/
Expand All @@ -382,7 +430,7 @@ export class MigrationExecutor {

if (this.connection.driver instanceof MongoDriver) {
const mongoRunner = queryRunner as MongoQueryRunner;
mongoRunner.databaseConnection.db(this.connection.driver.database!).collection(this.migrationsTableName).deleteOne(conditions);
mongoRunner.databaseConnection.db(this.connection.driver.database!).collection(this.migrationsTableName).deleteOne(conditions);
} else {
const qb = queryRunner.manager.createQueryBuilder();
await qb.delete()
Expand All @@ -395,4 +443,15 @@ export class MigrationExecutor {

}

private async withQueryRunner<T extends any>(callback: (queryRunner: QueryRunner) => T) {
const queryRunner = this.queryRunner || this.connection.createQueryRunner("master");

try {
return callback(queryRunner)
} finally {
if (!this.queryRunner) {
queryRunner.release()
}
}
}
}

0 comments on commit 632a132

Please sign in to comment.