Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
garbles committed Dec 7, 2018
1 parent 543031e commit 72c549d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/connection/Connection.ts
Expand Up @@ -272,12 +272,12 @@ export class Connection {
* Runs all pending migrations.
* Can be used only after connection to the database is established.
*/
async runMigrations(options?: { transactionMode?: 'all' | 'none' | 'each' }): Promise<Migration[]> {
async runMigrations(options?: { transactionMode?: "all" | "none" | "each" }): Promise<Migration[]> {
if (!this.isConnected)
throw new CannotExecuteNotConnectedError(this.name);

const migrationExecutor = new MigrationExecutor(this);
migrationExecutor.transactionMode = (options && options.transactionMode) || 'all';
migrationExecutor.transactionMode = (options && options.transactionMode) || "all";

const successMigrations = await migrationExecutor.executePendingMigrations();
return successMigrations;
Expand All @@ -287,13 +287,13 @@ export class Connection {
* Reverts last executed migration.
* Can be used only after connection to the database is established.
*/
async undoLastMigration(options?: { transactionMode?: 'all' | 'none' | 'each' }): Promise<void> {
async undoLastMigration(options?: { transactionMode?: "all" | "none" | "each" }): Promise<void> {

if (!this.isConnected)
throw new CannotExecuteNotConnectedError(this.name);

const migrationExecutor = new MigrationExecutor(this);
migrationExecutor.transactionMode = (options && options.transactionMode) || 'all';
migrationExecutor.transactionMode = (options && options.transactionMode) || "all";

await migrationExecutor.undoLastMigration();
}
Expand Down
12 changes: 6 additions & 6 deletions src/migration/MigrationExecutor.ts
Expand Up @@ -24,7 +24,7 @@ export class MigrationExecutor {
* none: all migrations are run without a transaction
* each: each migration is run in a separate transaction
*/
transactionMode: 'all' | 'none' | 'each' = 'all';
transactionMode: "all" | "none" | "each" = "all";

// -------------------------------------------------------------------------
// Private Properties
Expand Down Expand Up @@ -101,11 +101,11 @@ export class MigrationExecutor {
this.connection.logger.logSchemaBuild(`${pendingMigrations.length} migrations are new migrations that needs to be executed.`);

switch (this.transactionMode) {
case 'each':
case "each":
return this.runEachMigrationInSeparateTransaction(queryRunner, pendingMigrations);
case 'all':
case "all":
return this.runAllMigrationsInSingleTransaction(queryRunner, pendingMigrations);
case 'none':
case "none":
return this.runAllMigrationsWithoutTransaction(queryRunner, pendingMigrations);
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ export class MigrationExecutor {

// start transaction if its not started yet
let transactionStartedByUs = false;
if ((this.transactionMode !== 'none') && !queryRunner.isTransactionActive) {
if ((this.transactionMode !== "none") && !queryRunner.isTransactionActive) {
await queryRunner.startTransaction();
transactionStartedByUs = true;
}
Expand Down Expand Up @@ -315,7 +315,7 @@ export class MigrationExecutor {
const successMigrations: Migration[] = [];

if (queryRunner.isTransactionActive) {
throw new Error('Trying to run each migration in separate transaction, but already in one. Try changing this option and run migrations again.');
throw new Error("Trying to run each migration in separate transaction, but already in one. Try changing this option and run migrations again.");
}

// run all pending migrations in a sequence
Expand Down
31 changes: 31 additions & 0 deletions test/github-issues/2693/issue-2693.ts
@@ -0,0 +1,31 @@
import "reflect-metadata";
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import {Migration} from "../../../src/migration/Migration";

describe("github issues > #2875 Option to run migrations in 1-transaction-per-migration mode", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
migrations: [__dirname + "/migration/*.js"],
enabledDrivers: ["postgres"],
schemaCreate: true,
dropSchema: true,
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should fail to run all necessary migrations when transactionMode is all", () => Promise.all(connections.map(async connection => {
const mymigr: Migration[] = await connection.runMigrations({ transactionMode: "all" });

mymigr.length.should.be.equal(0);
})));

it("should be able to run all necessary migrations when transactionMode is each", () => Promise.all(connections.map(async connection => {
const mymigr: Migration[] = await connection.runMigrations({ transactionMode: "each" });

mymigr.length.should.be.equal(2);
mymigr[0].name.should.be.equal("CreateUuidExtension1544044606093");
mymigr[1].name.should.be.equal("CreateUsers1543965157399");
})));
});
@@ -0,0 +1,12 @@
import { MigrationInterface } from "../../../../src/migration/MigrationInterface";
import { QueryRunner } from "../../../../src/query-runner/QueryRunner";

export class CreateUuidExtension1544044606093 implements MigrationInterface {
public up(queryRunner: QueryRunner): Promise<any> {
return queryRunner.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`);
}

public down(queryRunner: QueryRunner): Promise<any> {
return queryRunner.query("DROP EXTENSION \"uuid-ossp\"");
}
}
20 changes: 20 additions & 0 deletions test/github-issues/2693/migration/1543965157399-CreateUsers.ts
@@ -0,0 +1,20 @@
import { MigrationInterface } from "../../../../src/migration/MigrationInterface";
import { QueryRunner } from "../../../../src/query-runner/QueryRunner";
import { Table } from "../../../../src/schema-builder/table/Table";

export class CreateUsers1543965157399 implements MigrationInterface {
public up(queryRunner: QueryRunner): Promise<any> {
return queryRunner.createTable(
new Table({
name: "users",
columns: [
{ name: "id", type: "uuid", isPrimary: true, default: "uuid_generate_v4()" },
]
})
);
}

public down(queryRunner: QueryRunner): Promise<any> {
return queryRunner.dropTable("users");
}
}

0 comments on commit 72c549d

Please sign in to comment.