Skip to content

Commit

Permalink
fix: respect database from connection urls (typeorm#5640)
Browse files Browse the repository at this point in the history
* fix: respect database from connection urls

database names can be defined in the options object. Now also connection urls that contain a database can be used to have the database set in the drivers object.

Closes: typeorm#2096

* only disconnect connection if connected.

* revert changes.

* fix credentials for testing

* create connection by lib function

* check typeorm config during testing to check whether a mysql database is available

Co-authored-by: julius <julius.friedrich@shift-f3.com>
  • Loading branch information
2 people authored and Svetlozar committed Jan 12, 2021
1 parent c768eab commit 587977f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
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();
}
});
});

0 comments on commit 587977f

Please sign in to comment.