From a0b38623b38586b37fdd1f9a96ddf47fd006cb89 Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sun, 13 Aug 2017 17:55:53 +0200 Subject: [PATCH] added creating test db if it doesn't already exist --- src/drivers/AbstractDriver.ts | 5 +++++ src/drivers/MssqlDriver.ts | 17 +++++++++++++++++ src/drivers/PostgresDriver.ts | 15 +++++++++++++++ test/integration/integration.test.ts | 25 +++++++++++++++++++++++-- 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index 6a28051..672e4af 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -22,4 +22,9 @@ export abstract class AbstractDriver { abstract async GetRelations(entities: EntityInfo[]):Promise; abstract async FindPrimaryColumnsFromIndexes(dbModel:DatabaseModel); abstract async DisconnectFromServer(); + + abstract async CreateDB(dbName:string); + abstract async DropDB(dbName:string); + abstract async UseDB(dbName:string); + abstract async CheckIfDBExists(dbName:string):Promise; } \ No newline at end of file diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 1b7a0dd..2e262a4 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -376,4 +376,21 @@ order by await promise; } + async CreateDB(dbName:string){ + let request = new MSSQL.Request(this.Connection); + let resp =await request.query(`CREATE DATABASE ${dbName}; `) + } + async UseDB(dbName:string){ + let request = new MSSQL.Request(this.Connection); + let resp =await request.query(`USE ${dbName}; `) + } + async DropDB(dbName:string){ + let request = new MSSQL.Request(this.Connection); + let resp =await request.query(`DROP DATABASE ${dbName}; `) + } + async CheckIfDBExists(dbName:string):Promise{ + let request = new MSSQL.Request(this.Connection); + let resp =await request.query(`SELECT name FROM master.sys.databases WHERE name = N'${dbName}' `) + return resp.length>0; + } } \ No newline at end of file diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index d1b35f3..c1d7fd7 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -390,4 +390,19 @@ export class PostgresDriver extends AbstractDriver { await promise; } + + async CreateDB(dbName:string){ + + let resp = await this.Connection.query(`CREATE DATABASE ${dbName}; `) + } + async UseDB(dbName:string){ + let resp =await this.Connection.query(`USE ${dbName}; `) + } + async DropDB(dbName:string){ + let resp =await this.Connection.query(`DROP DATABASE ${dbName}; `) + } + async CheckIfDBExists(dbName:string):Promise{ + let resp =await this.Connection.query(`SELECT datname FROM pg_database WHERE datname ='${dbName}' `) + return resp.rowCount>0; + } } \ No newline at end of file diff --git a/test/integration/integration.test.ts b/test/integration/integration.test.ts index 7a466eb..372d1c1 100644 --- a/test/integration/integration.test.ts +++ b/test/integration/integration.test.ts @@ -93,6 +93,15 @@ describe("integration tests", async function () { }) async function createMSSQLModels(filesOrgPath: string, resultsPath: string): Promise { + + let driver: AbstractDriver; + driver = new MssqlDriver(); + await driver.ConnectToServer(`master`,process.env.MSSQL_Host,process.env.MSSQL_Port,process.env.MSSQL_Username, process.env.MSSQL_Password); + + if (! await driver.CheckIfDBExists(process.env.MSSQL_Database)) + await driver.CreateDB(process.env.MSSQL_Database); + await driver.DisconnectFromServer(); + let connOpt: ConnectionOptions = { driver: { database: process.env.MSSQL_Database, @@ -111,7 +120,7 @@ async function createMSSQLModels(filesOrgPath: string, resultsPath: string): Pro if (conn.isConnected) await conn.close() - let driver: AbstractDriver; + driver = new MssqlDriver(); let engine = new Engine( driver, { @@ -123,10 +132,20 @@ async function createMSSQLModels(filesOrgPath: string, resultsPath: string): Pro databaseType: 'mssql', resultsPath: resultsPath }); + + return engine; } async function createPostgresModels(filesOrgPath: string, resultsPath: string): Promise { + let driver: AbstractDriver; + driver = new PostgresDriver(); + await driver.ConnectToServer(`postgres`,process.env.POSTGRES_Host,process.env.POSTGRES_Port,process.env.POSTGRES_Username, process.env.POSTGRES_Password); + + if (! await driver.CheckIfDBExists(process.env.POSTGRES_Database)) + await driver.CreateDB(process.env.POSTGRES_Database); + await driver.DisconnectFromServer(); + let connOpt: ConnectionOptions = { driver: { database: process.env.POSTGRES_Database, @@ -145,7 +164,6 @@ async function createPostgresModels(filesOrgPath: string, resultsPath: string): if (conn.isConnected) await conn.close() - let driver: AbstractDriver; driver = new PostgresDriver(); let engine = new Engine( driver, { @@ -157,6 +175,9 @@ async function createPostgresModels(filesOrgPath: string, resultsPath: string): databaseType: 'postgres', resultsPath: resultsPath }); + + + return engine; }