From 587977f5df1f686c9f52133a2ec6d749b2ef1da0 Mon Sep 17 00:00:00 2001 From: ant45de Date: Sat, 16 May 2020 18:52:36 +0200 Subject: [PATCH] fix: respect database from connection urls (#5640) * 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: #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 --- src/connection/Connection.ts | 23 ++++++++++--------- test/github-issues/2096/entity/TestEntity.ts | 9 ++++++++ test/github-issues/2096/issue-2096.ts | 24 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 test/github-issues/2096/entity/TestEntity.ts create mode 100644 test/github-issues/2096/issue-2096.ts diff --git a/src/connection/Connection.ts b/src/connection/Connection.ts index 52096879570..1ddb8ff7b58 100644 --- a/src/connection/Connection.ts +++ b/src/connection/Connection.ts @@ -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. @@ -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; } } diff --git a/test/github-issues/2096/entity/TestEntity.ts b/test/github-issues/2096/entity/TestEntity.ts new file mode 100644 index 00000000000..355ba71f112 --- /dev/null +++ b/test/github-issues/2096/entity/TestEntity.ts @@ -0,0 +1,9 @@ +import {Entity, PrimaryGeneratedColumn} from "../../../../src"; + +@Entity() +export class TestEntity { + + @PrimaryGeneratedColumn() + id: number; + +} diff --git a/test/github-issues/2096/issue-2096.ts b/test/github-issues/2096/issue-2096.ts new file mode 100644 index 00000000000..05cbfc11bb0 --- /dev/null +++ b/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(); + } + }); +});