forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: db caching won't work with replication enabled (typeorm#7694)
* FIX Insert cache only on master connection close typeorm#5919 * FIX release queryRunner created
- Loading branch information
1 parent
f0a85ce
commit 97d4aa9
Showing
6 changed files
with
143 additions
and
3 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,10 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../src"; | ||
|
||
@Entity() | ||
export class Comment { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
text: string; | ||
} |
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,107 @@ | ||
import { expect } from "chai"; | ||
import Sinon from "sinon"; | ||
import { Connection } from "../../../src"; | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils"; | ||
import { Comment } from "./entities"; | ||
|
||
describe("github issues > #5919 Caching won't work with replication enabled", () => { | ||
let connections: Connection[]; | ||
|
||
beforeEach(async () => { | ||
connections = await createTestingConnections({ | ||
entities: [Comment], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
cache: true, | ||
enabledDrivers: ["postgres"], | ||
}); | ||
await reloadTestingDatabases(connections); | ||
}); | ||
afterEach(() => closeTestingConnections(connections)); | ||
|
||
it("should not another queryRunner for cache with a given masterQueryRunner", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const comment1 = new Comment(); | ||
comment1.text = "tata"; | ||
await connection.manager.save(comment1); | ||
|
||
const masterQueryRunner = connection.createQueryRunner( | ||
"master" | ||
); | ||
const createQueryRunnerSpy = Sinon.spy( | ||
connection, | ||
"createQueryRunner" | ||
); | ||
|
||
const results1 = await connection | ||
.createQueryBuilder() | ||
.from(Comment, "c") | ||
.cache(true) | ||
.setQueryRunner(masterQueryRunner) | ||
.getRawMany(); | ||
|
||
expect(results1.length).eq(1); | ||
|
||
expect(createQueryRunnerSpy.notCalled); | ||
|
||
// add another one and ensure cache works | ||
const comment2 = new Comment(); | ||
comment2.text = "tata"; | ||
await connection.manager.save(comment2); | ||
|
||
const results2 = await connection | ||
.createQueryBuilder() | ||
.from(Comment, "c") | ||
.cache(true) | ||
.setQueryRunner(masterQueryRunner) | ||
.getRawMany(); | ||
|
||
expect(results2.length).eq(1); | ||
}) | ||
)); | ||
|
||
it("should create another queryRunner for cache with a given slaveQueryRunner", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const comment1 = new Comment(); | ||
comment1.text = "tata"; | ||
await connection.manager.save(comment1); | ||
|
||
const slaveQueryRunner = connection.createQueryRunner("slave"); | ||
const createQueryRunnerSpy = Sinon.spy( | ||
connection, | ||
"createQueryRunner" | ||
); | ||
|
||
const results1 = await connection | ||
.createQueryBuilder() | ||
.from(Comment, "c") | ||
.cache(true) | ||
.setQueryRunner(slaveQueryRunner) | ||
.getRawMany(); | ||
|
||
expect(results1.length).eq(1); | ||
|
||
expect(createQueryRunnerSpy.calledOnce); | ||
|
||
// add another one and ensure cache works | ||
const comment2 = new Comment(); | ||
comment2.text = "tata"; | ||
await connection.manager.save(comment2); | ||
|
||
const results2 = await connection | ||
.createQueryBuilder() | ||
.from(Comment, "c") | ||
.cache(true) | ||
.setQueryRunner(slaveQueryRunner) | ||
.getRawMany(); | ||
|
||
expect(results2.length).eq(1); | ||
}) | ||
)); | ||
}); |