From f80be4177cf613c6ae37396d0174e7d2cb593d49 Mon Sep 17 00:00:00 2001 From: julius Date: Sun, 8 Mar 2020 00:03:21 +0100 Subject: [PATCH 1/6] 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 --- src/connection/Connection.ts | 23 ++++++++++---------- test/github-issues/2096/entity/TestEntity.ts | 9 ++++++++ test/github-issues/2096/issue-2096.ts | 23 ++++++++++++++++++++ 3 files changed, 44 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 5209687957..1ddb8ff7b5 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 0000000000..355ba71f11 --- /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 0000000000..7444db7d60 --- /dev/null +++ b/test/github-issues/2096/issue-2096.ts @@ -0,0 +1,23 @@ +import "reflect-metadata"; +import {expect} from "chai"; +import {createConnection, getConnectionManager} from "../../../src"; + +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 connection = getConnectionManager().connections.find( + c => c.name.indexOf("mysql") > -1 + ); + if (connection) { + await connection.close(); + } + // it is important to synchronize here, to trigger EntityMetadataValidator.validate + // that previously threw the error where the database on the driver object was undefined + const newConnection = await createConnection({ + url: "mysql://test:test@localhost:3306/test", + entities: [__dirname + "/entity/*{.js,.ts}"], + synchronize: true, + type: "mysql" + }); + expect(newConnection.isConnected).to.eq(true); + }); +}); From 34e7f0a49d87a4fc10b4800819ec0d132f0c59ef Mon Sep 17 00:00:00 2001 From: julius Date: Sun, 8 Mar 2020 00:23:09 +0100 Subject: [PATCH 2/6] only disconnect connection if connected. --- package-lock.json | 2 +- src/connection/Connection.ts | 1 + test/github-issues/2096/issue-2096.ts | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4a15a9cb27..2cd0856f29 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "typeorm", - "version": "0.2.22", + "version": "0.2.24", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/connection/Connection.ts b/src/connection/Connection.ts index 1ddb8ff7b5..1cd010d4b1 100644 --- a/src/connection/Connection.ts +++ b/src/connection/Connection.ts @@ -518,6 +518,7 @@ export class Connection { ObjectUtils.assign(this, { migrations: migrations }); this.driver.database = this.getDatabaseName(); + console.log(this.driver.database); // 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); diff --git a/test/github-issues/2096/issue-2096.ts b/test/github-issues/2096/issue-2096.ts index 7444db7d60..777bd290dd 100644 --- a/test/github-issues/2096/issue-2096.ts +++ b/test/github-issues/2096/issue-2096.ts @@ -1,13 +1,13 @@ import "reflect-metadata"; -import {expect} from "chai"; -import {createConnection, getConnectionManager} from "../../../src"; +import { expect } from "chai"; +import { createConnection, getConnectionManager } from "../../../src"; 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 connection = getConnectionManager().connections.find( c => c.name.indexOf("mysql") > -1 ); - if (connection) { + if (connection && connection.isConnected) { await connection.close(); } // it is important to synchronize here, to trigger EntityMetadataValidator.validate From 8494cb4d74d1b1cf24ebfd940a06ff51124b854e Mon Sep 17 00:00:00 2001 From: julius Date: Sun, 8 Mar 2020 00:24:15 +0100 Subject: [PATCH 3/6] revert changes. --- package-lock.json | 2 +- src/connection/Connection.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2cd0856f29..4a15a9cb27 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "typeorm", - "version": "0.2.24", + "version": "0.2.22", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/connection/Connection.ts b/src/connection/Connection.ts index 1cd010d4b1..1ddb8ff7b5 100644 --- a/src/connection/Connection.ts +++ b/src/connection/Connection.ts @@ -518,7 +518,6 @@ export class Connection { ObjectUtils.assign(this, { migrations: migrations }); this.driver.database = this.getDatabaseName(); - console.log(this.driver.database); // 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); From 0fa78a264f819bd7b7251af50b40e0b44b3ded1c Mon Sep 17 00:00:00 2001 From: julius Date: Sun, 8 Mar 2020 01:34:36 +0100 Subject: [PATCH 4/6] fix credentials for testing --- test/github-issues/2096/issue-2096.ts | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/test/github-issues/2096/issue-2096.ts b/test/github-issues/2096/issue-2096.ts index 777bd290dd..eefcf6b4bb 100644 --- a/test/github-issues/2096/issue-2096.ts +++ b/test/github-issues/2096/issue-2096.ts @@ -1,23 +1,19 @@ import "reflect-metadata"; -import { expect } from "chai"; -import { createConnection, getConnectionManager } from "../../../src"; +import {expect} from "chai"; +import {Connection} from "../../../src"; 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 connection = getConnectionManager().connections.find( - c => c.name.indexOf("mysql") > -1 - ); - if (connection && connection.isConnected) { - await connection.close(); - } // it is important to synchronize here, to trigger EntityMetadataValidator.validate // that previously threw the error where the database on the driver object was undefined - const newConnection = await createConnection({ - url: "mysql://test:test@localhost:3306/test", + const connection = new Connection({ + name: "#2096", + url: "mysql://root:admin@localhost:3306/test", entities: [__dirname + "/entity/*{.js,.ts}"], synchronize: true, type: "mysql" }); - expect(newConnection.isConnected).to.eq(true); + expect(connection.isConnected).to.eq(true); + await connection.close(); }); }); From d2b687021710c7256af53b55cfecdef0d502acd8 Mon Sep 17 00:00:00 2001 From: julius Date: Sun, 8 Mar 2020 01:42:10 +0100 Subject: [PATCH 5/6] create connection by lib function --- test/github-issues/2096/issue-2096.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/github-issues/2096/issue-2096.ts b/test/github-issues/2096/issue-2096.ts index eefcf6b4bb..ad272a3425 100644 --- a/test/github-issues/2096/issue-2096.ts +++ b/test/github-issues/2096/issue-2096.ts @@ -1,12 +1,12 @@ import "reflect-metadata"; import {expect} from "chai"; -import {Connection} from "../../../src"; +import {createConnection} from "../../../src"; 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 () => { // it is important to synchronize here, to trigger EntityMetadataValidator.validate // that previously threw the error where the database on the driver object was undefined - const connection = new Connection({ + const connection = await createConnection({ name: "#2096", url: "mysql://root:admin@localhost:3306/test", entities: [__dirname + "/entity/*{.js,.ts}"], From bc2bd9db952f0a91b12de9b0db1d7cdd21c6b547 Mon Sep 17 00:00:00 2001 From: julius Date: Sat, 14 Mar 2020 09:29:49 +0100 Subject: [PATCH 6/6] check typeorm config during testing to check whether a mysql database is available --- test/github-issues/2096/issue-2096.ts | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/test/github-issues/2096/issue-2096.ts b/test/github-issues/2096/issue-2096.ts index ad272a3425..05cbfc11bb 100644 --- a/test/github-issues/2096/issue-2096.ts +++ b/test/github-issues/2096/issue-2096.ts @@ -1,19 +1,24 @@ import "reflect-metadata"; -import {expect} from "chai"; -import {createConnection} from "../../../src"; +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 - 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(); + 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(); + } }); });