added creating test db if it doesn't already exist

This commit is contained in:
Kononnable 2017-08-13 17:55:53 +02:00
parent ed7d9c6f7e
commit a0b38623b3
4 changed files with 60 additions and 2 deletions

View File

@ -22,4 +22,9 @@ export abstract class AbstractDriver {
abstract async GetRelations(entities: EntityInfo[]):Promise<EntityInfo[]>;
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<boolean>;
}

View File

@ -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<boolean>{
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;
}
}

View File

@ -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<boolean>{
let resp =await this.Connection.query(`SELECT datname FROM pg_database WHERE datname ='${dbName}' `)
return resp.rowCount>0;
}
}

View File

@ -93,6 +93,15 @@ describe("integration tests", async function () {
})
async function createMSSQLModels(filesOrgPath: string, resultsPath: string): Promise<Engine> {
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<Engine> {
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;
}