mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fix: prevent error when replication is undefined (#11423)
* fix: prevent error when replication is undefined * fix format * add test * update test name * fix test * fix test * skip test * add unit test * fix unit test * fix unit test
This commit is contained in:
parent
b9ddd14298
commit
61a6f971af
@ -750,7 +750,10 @@ export class DataSource {
|
||||
* Get the replication mode SELECT queries should use for this datasource by default
|
||||
*/
|
||||
defaultReplicationModeForReads(): ReplicationMode {
|
||||
if ("replication" in this.driver.options) {
|
||||
if (
|
||||
"replication" in this.driver.options &&
|
||||
this.driver.options.replication
|
||||
) {
|
||||
const value = (
|
||||
this.driver.options.replication as {
|
||||
defaultMode?: ReplicationMode
|
||||
|
||||
@ -195,4 +195,54 @@ describe("Connection replication", () => {
|
||||
expect(result[0].current_setting).to.equal("master")
|
||||
})
|
||||
})
|
||||
|
||||
describe("with undefined replication", function () {
|
||||
let connection: DataSource
|
||||
|
||||
beforeEach(async () => {
|
||||
connection = (
|
||||
await createTestingConnections({
|
||||
entities: [Post, Category],
|
||||
enabledDrivers: ["postgres"],
|
||||
schemaCreate: true,
|
||||
dropSchema: true,
|
||||
driverSpecific: {
|
||||
replication: undefined,
|
||||
},
|
||||
})
|
||||
)[0]
|
||||
|
||||
const post = new Post()
|
||||
post.title = "TypeORM Intro"
|
||||
|
||||
await connection
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(Post)
|
||||
.values(post)
|
||||
.execute()
|
||||
})
|
||||
|
||||
afterEach(() => closeTestingConnections([connection]))
|
||||
|
||||
it("query runners should go to the available instance", async () => {
|
||||
const queryRunner = connection.createQueryRunner()
|
||||
expect(queryRunner.getReplicationMode()).to.equal("master")
|
||||
|
||||
await expectCurrentApplicationName(queryRunner, "")
|
||||
await queryRunner.release()
|
||||
})
|
||||
|
||||
it("read queries should go to the available instance", async () => {
|
||||
const result = await connection.manager
|
||||
.createQueryBuilder(Post, "post")
|
||||
.select("id")
|
||||
.addSelect(
|
||||
"current_setting('application_name')",
|
||||
"current_setting",
|
||||
)
|
||||
.execute()
|
||||
expect(result[0].current_setting).to.equal("")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
12
test/github-issues/11423/entity/Post.ts
Normal file
12
test/github-issues/11423/entity/Post.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"
|
||||
|
||||
@Entity({
|
||||
name: "post",
|
||||
})
|
||||
export class Post {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number
|
||||
|
||||
@Column()
|
||||
title: string
|
||||
}
|
||||
44
test/github-issues/11423/issue-11423.ts
Normal file
44
test/github-issues/11423/issue-11423.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { expect } from "chai"
|
||||
import { DataSource, Repository } from "../../../src"
|
||||
import { PostgresConnectionOptions } from "../../../src/driver/postgres/PostgresConnectionOptions"
|
||||
import {
|
||||
closeTestingConnections,
|
||||
reloadTestingDatabases,
|
||||
setupSingleTestingConnection,
|
||||
} from "../../utils/test-utils"
|
||||
import { Post } from "./entity/Post"
|
||||
|
||||
describe("github issues > #11423", () => {
|
||||
let dataSource: DataSource
|
||||
let repository: Repository<Post>
|
||||
|
||||
before(async () => {
|
||||
const options = setupSingleTestingConnection("postgres", {
|
||||
entities: [Post],
|
||||
}) as PostgresConnectionOptions
|
||||
if (!options) return
|
||||
|
||||
dataSource = new DataSource({
|
||||
...options,
|
||||
replication: undefined,
|
||||
})
|
||||
await dataSource.initialize()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
if (!dataSource) return
|
||||
await reloadTestingDatabases([dataSource])
|
||||
})
|
||||
after(() => closeTestingConnections([dataSource]))
|
||||
|
||||
it("allow replication to be undefined", async () => {
|
||||
if (!dataSource) return
|
||||
repository = dataSource.getRepository(Post)
|
||||
const posts = await repository.find({
|
||||
order: {
|
||||
title: "DESC",
|
||||
},
|
||||
})
|
||||
expect(posts).to.be.an("array")
|
||||
})
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user