Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect database from connection urls #5640

Merged
merged 7 commits into from May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/connection/Connection.ts
Expand Up @@ -38,6 +38,7 @@ import {ObjectUtils} from "../util/ObjectUtils";
import {PromiseUtils} from "../";
import {IsolationLevel} from "../driver/types/IsolationLevel";
import {AuroraDataApiDriver} from "../driver/aurora-data-api/AuroraDataApiDriver";
import {DriverUtils} from "../driver/DriverUtils";

/**
* Connection is a single database ORM connection to a specific database.
Expand Down Expand Up @@ -524,17 +525,17 @@ export class Connection {

// 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;
const options = this.options;
switch (options.type) {
case "mysql" :
case "mariadb" :
case "postgres":
case "cockroachdb":
case "mssql":
case "oracle":
return DriverUtils.buildDriverOptions(options.replication ? options.replication.master : options).database;
default:
return DriverUtils.buildDriverOptions(options).database;
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/github-issues/2096/entity/TestEntity.ts
@@ -0,0 +1,9 @@
import {Entity, PrimaryGeneratedColumn} from "../../../../src";

@Entity()
export class TestEntity {

@PrimaryGeneratedColumn()
id: number;

}
24 changes: 24 additions & 0 deletions test/github-issues/2096/issue-2096.ts
@@ -0,0 +1,24 @@
import "reflect-metadata";
import { expect } from "chai";
import { createConnection } from "../../../src";
import { getTypeOrmConfig } from "../../utils/test-utils";

describe("github issues > #2096 [mysql] Database name isn't read from url", () => {
it("should be possible to define a database by connection url for mysql", async () => {
const config = getTypeOrmConfig();

// it is important to synchronize here, to trigger EntityMetadataValidator.validate
// that previously threw the error where the database on the driver object was undefined
if (config.find(c => c.name === "mysql" && !c.skip)) {
const connection = await createConnection({
name: "#2096",
url: "mysql://root:admin@localhost:3306/test",
entities: [__dirname + "/entity/*{.js,.ts}"],
synchronize: true,
type: "mysql"
});
expect(connection.isConnected).to.eq(true);
await connection.close();
}
});
});