Skip to content

Commit

Permalink
fix: broken database option when using replication, changes introduce…
Browse files Browse the repository at this point in the history
…d by #4753 (#4826)
  • Loading branch information
Matt Morgan authored and pleerock committed Oct 13, 2019
1 parent 154a441 commit df5479b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/connection/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,26 @@ export class Connection {
const migrations = connectionMetadataBuilder.buildMigrations(this.options.migrations || []);
ObjectUtils.assign(this, { migrations: migrations });

this.driver.database = <string> this.options.database;
this.driver.database = this.getDatabaseName();

// validate all created entity metadatas to make sure user created entities are valid and correct
entityMetadataValidator.validateMany(this.entityMetadatas.filter(metadata => metadata.tableType !== "view"), this.driver);
}

// This database name property is nested for replication configs.
protected getDatabaseName(): string {
const options = this.options;
switch (options.type) {
case "mysql" :
case "mariadb" :
case "postgres":
case "cockroachdb":
case "mssql":
case "oracle":
return (options.replication ? options.replication.master.database : options.database) as string;
default:
return options.database as string;
}
}

}
10 changes: 10 additions & 0 deletions test/github-issues/4753/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Entity, Column, PrimaryGeneratedColumn } from "../../../../src";

@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;
}
33 changes: 33 additions & 0 deletions test/github-issues/4753/issue-4753.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getConnectionManager } from "../../../src";
import { Connection } from "../../../src/connection/Connection";
import { closeTestingConnections } from "../../utils/test-utils";
import { User } from "./entity/User";

describe("github issues > #4753 MySQL Replication Config broken", () => {
let connections: Connection[] = [];
after(() => closeTestingConnections(connections));

it("should connect without error when using replication", async () => {
const connection = getConnectionManager().create({
type: "mysql",
replication: {
master: {
username: "test",
password: "test",
database: "test"
},
slaves: [
{
username: "test",
password: "test",
database: "test"
}
]
},
entities: [User]
});
connections.push(connection);
await connection.connect();
connection.isConnected.should.be.true;
});
});

0 comments on commit df5479b

Please sign in to comment.