forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new transaction mode to wrap each migration in transaction (t…
…ypeorm#4629) * Add new mode to wrap each migration in transaction * Add tests * feat: fix command line options, refactor MigrationExecutor.executePendingMigrations and fix tests
- Loading branch information
Showing
9 changed files
with
149 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
|
||
@Entity({name: "users", synchronize: false}) | ||
export class User { | ||
@PrimaryGeneratedColumn("uuid") | ||
id: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import "reflect-metadata"; | ||
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; | ||
import {Connection} from "../../../src/connection/Connection"; | ||
import {Migration} from "../../../src/migration/Migration"; | ||
import {QueryFailedError} from "../../../src/error/QueryFailedError"; | ||
|
||
describe("github issues > #2875 Option to run migrations in 1-transaction-per-migration mode", () => { | ||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
__dirname, | ||
schemaCreate: false, | ||
dropSchema: true, | ||
enabledDrivers: ["postgres"] | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should fail to run all necessary migrations when transaction is all", () => Promise.all(connections.map(async connection => { | ||
return connection.runMigrations({ transaction: "all" }).should.be.rejectedWith(QueryFailedError, "relation \"users\" does not exist"); | ||
}))); | ||
|
||
it("should be able to run all necessary migrations when transaction is each", () => Promise.all(connections.map(async connection => { | ||
const mymigr: Migration[] = await connection.runMigrations({ transaction: "each" }); | ||
|
||
mymigr.length.should.be.equal(3); | ||
mymigr[0].name.should.be.equal("CreateUuidExtension0000000000001"); | ||
mymigr[1].name.should.be.equal("CreateUsers0000000000002"); | ||
mymigr[2].name.should.be.equal("InsertUser0000000000003"); | ||
}))); | ||
}); |
12 changes: 12 additions & 0 deletions
12
test/github-issues/2693/migration/0000000000001-CreateUuidExtension.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { MigrationInterface } from "../../../../src/migration/MigrationInterface"; | ||
import { QueryRunner } from "../../../../src/query-runner/QueryRunner"; | ||
|
||
export class CreateUuidExtension0000000000001 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\""); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
test/github-issues/2693/migration/0000000000002-CreateUsers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { MigrationInterface } from "../../../../src/migration/MigrationInterface"; | ||
import { QueryRunner } from "../../../../src/query-runner/QueryRunner"; | ||
import { Table } from "../../../../src/schema-builder/table/Table"; | ||
|
||
export class CreateUsers0000000000002 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"); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
test/github-issues/2693/migration/0000000000003-InsertUser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { MigrationInterface } from "../../../../src/migration/MigrationInterface"; | ||
import { QueryRunner } from "../../../../src/query-runner/QueryRunner"; | ||
import { User } from "../entity/user"; | ||
|
||
export class InsertUser0000000000003 implements MigrationInterface { | ||
public up(queryRunner: QueryRunner): Promise<any> { | ||
const userRepo = queryRunner.connection.getRepository<User>(User); | ||
return userRepo.save(new User()); | ||
} | ||
|
||
public down(queryRunner: QueryRunner): Promise<any> { | ||
return Promise.resolve(); | ||
} | ||
} |