-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow per-migration control over transaction behavior (#9459)
Closes #7087
- Loading branch information
Showing
11 changed files
with
260 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Migration } from "../migration/Migration" | ||
import { TypeORMError } from "./TypeORMError" | ||
|
||
/** | ||
* Thrown when the per-migration transaction mode is overriden but the global transaction mode is set to "all". | ||
*/ | ||
export class ForbiddenTransactionModeOverrideError extends TypeORMError { | ||
constructor(migrationsOverridingTransactionMode: Migration[]) { | ||
const migrationNames = migrationsOverridingTransactionMode.map( | ||
(migration) => `"${migration.name}"`, | ||
) | ||
|
||
super( | ||
`Migrations ${migrationNames.join( | ||
", ", | ||
)} override the transaction mode, but the global transaction mode is "all"`, | ||
) | ||
} | ||
} |
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,70 @@ | ||
import "reflect-metadata" | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { DataSource } from "../../../src/data-source/DataSource" | ||
import { Migration } from "../../../src/migration/Migration" | ||
import { ForbiddenTransactionModeOverrideError } from "../../../src/error/ForbiddenTransactionModeOverrideError" | ||
|
||
describe("github issues > #7087 Allow to specify transaction property for individual migrations", () => { | ||
let connections: DataSource[] | ||
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 and there are transaction overrides", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
return connection | ||
.runMigrations({ transaction: "all" }) | ||
.should.be.rejectedWith( | ||
ForbiddenTransactionModeOverrideError, | ||
'Migrations "InsertUser0000000000002", "CreateIndex0000000000003" override the transaction mode, but the global transaction mode is "all"', | ||
) | ||
}), | ||
)) | ||
|
||
it("should set correct transaction mode when transaction is each", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const migrations: Migration[] = await connection.runMigrations({ | ||
transaction: "each", | ||
}) | ||
|
||
migrations.length.should.be.equal(3) | ||
migrations[0].name.should.be.equal("CreateUsers0000000000001") | ||
migrations[0].transaction!.should.be.true | ||
migrations[1].name.should.be.equal("InsertUser0000000000002") | ||
migrations[1].transaction!.should.be.true | ||
migrations[2].name.should.be.equal("CreateIndex0000000000003") | ||
migrations[2].transaction!.should.be.false | ||
}), | ||
)) | ||
|
||
it("should set correct transaction mode when transaction is none", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const migrations: Migration[] = await connection.runMigrations({ | ||
transaction: "none", | ||
}) | ||
|
||
migrations.length.should.be.equal(3) | ||
migrations[0].name.should.be.equal("CreateUsers0000000000001") | ||
migrations[0].transaction!.should.be.false | ||
migrations[1].name.should.be.equal("InsertUser0000000000002") | ||
migrations[1].transaction!.should.be.true | ||
migrations[2].name.should.be.equal("CreateIndex0000000000003") | ||
migrations[2].transaction!.should.be.false | ||
}), | ||
)) | ||
}) |
25 changes: 25 additions & 0 deletions
25
test/github-issues/7087/migration/0000000000001-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 CreateUsers0000000000001 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") | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/github-issues/7087/migration/0000000000002-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,16 @@ | ||
import { MigrationInterface } from "../../../../src/migration/MigrationInterface" | ||
import { QueryRunner } from "../../../../src/query-runner/QueryRunner" | ||
import { User } from "../entity/user" | ||
|
||
export class InsertUser0000000000002 implements MigrationInterface { | ||
public transaction = true | ||
|
||
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() | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/github-issues/7087/migration/0000000000003-CreateIndex.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,16 @@ | ||
import { MigrationInterface } from "../../../../src/migration/MigrationInterface" | ||
import { QueryRunner } from "../../../../src/query-runner/QueryRunner" | ||
|
||
export class CreateIndex0000000000003 implements MigrationInterface { | ||
public transaction = false | ||
|
||
public up(queryRunner: QueryRunner): Promise<any> { | ||
return queryRunner.query( | ||
"CREATE INDEX CONCURRENTLY user_ids_idx ON users(id)", | ||
) | ||
} | ||
|
||
public down(queryRunner: QueryRunner): Promise<any> { | ||
return Promise.resolve() | ||
} | ||
} |