|
|
@ -12,17 +12,37 @@ export default class TestDbMngr { |
|
|
|
public static sakilaKnex: knex; |
|
|
|
public static sakilaKnex: knex; |
|
|
|
|
|
|
|
|
|
|
|
public static defaultConnection = { |
|
|
|
public static defaultConnection = { |
|
|
|
user: process.env['DB_USER'] || 'root', |
|
|
|
user: 'root', |
|
|
|
password: process.env['DB_PASSWORD'] || 'password', |
|
|
|
password: 'password', |
|
|
|
host: process.env['DB_HOST'] || 'localhost', |
|
|
|
host: 'localhost', |
|
|
|
port: Number(process.env['DB_PORT']) || 3306, |
|
|
|
port: 3306, |
|
|
|
client: 'mysql2', |
|
|
|
client: 'mysql2', |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static connection: { |
|
|
|
|
|
|
|
user: string; |
|
|
|
|
|
|
|
password: string; |
|
|
|
|
|
|
|
host: string; |
|
|
|
|
|
|
|
port: number; |
|
|
|
|
|
|
|
client: string; |
|
|
|
|
|
|
|
} = TestDbMngr.defaultConnection; |
|
|
|
|
|
|
|
|
|
|
|
public static dbConfig: DbConfig; |
|
|
|
public static dbConfig: DbConfig; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static populateConnectionConfig() { |
|
|
|
|
|
|
|
const { user, password, host, port, client } = TestDbMngr.defaultConnection; |
|
|
|
|
|
|
|
TestDbMngr.connection = { |
|
|
|
|
|
|
|
user: process.env['DB_USER'] || user, |
|
|
|
|
|
|
|
password: process.env['DB_PASSWORD'] || password, |
|
|
|
|
|
|
|
host: process.env['DB_HOST'] || host, |
|
|
|
|
|
|
|
port: Number(process.env['DB_PORT']) || port, |
|
|
|
|
|
|
|
client |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static async testConnection(config: DbConfig) { |
|
|
|
static async testConnection(config: DbConfig) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
console.log('Testing connection', TestDbMngr.connection); |
|
|
|
return await SqlMgrv2.testConnection(config); |
|
|
|
return await SqlMgrv2.testConnection(config); |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
console.log(e); |
|
|
|
console.log(e); |
|
|
@ -31,15 +51,18 @@ export default class TestDbMngr { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static async init() { |
|
|
|
static async init() { |
|
|
|
|
|
|
|
TestDbMngr.populateConnectionConfig() |
|
|
|
|
|
|
|
|
|
|
|
if(await TestDbMngr.isMysqlConfigured()){ |
|
|
|
if(await TestDbMngr.isMysqlConfigured()){ |
|
|
|
await TestDbMngr.connectMysql(); |
|
|
|
await TestDbMngr.connectMysql(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
console.log('Mysql is not configured. Switching to sqlite'); |
|
|
|
await TestDbMngr.switchToSqlite(); |
|
|
|
await TestDbMngr.switchToSqlite(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static async isMysqlConfigured() { |
|
|
|
static async isMysqlConfigured() { |
|
|
|
const { user, password, host, port, client } = TestDbMngr.defaultConnection; |
|
|
|
const { user, password, host, port, client } = TestDbMngr.connection; |
|
|
|
const config = NcConfigFactory.urlToDbConfig(`${client}://${user}:${password}@${host}:${port}`); |
|
|
|
const config = NcConfigFactory.urlToDbConfig(`${client}://${user}:${password}@${host}:${port}`); |
|
|
|
config.connection = { |
|
|
|
config.connection = { |
|
|
|
user, |
|
|
|
user, |
|
|
@ -52,7 +75,7 @@ export default class TestDbMngr { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static async connectMysql() { |
|
|
|
static async connectMysql() { |
|
|
|
const { user, password, host, port, client } = TestDbMngr.defaultConnection; |
|
|
|
const { user, password, host, port, client } = TestDbMngr.connection; |
|
|
|
if(!process.env[`DATABASE_URL`]){ |
|
|
|
if(!process.env[`DATABASE_URL`]){ |
|
|
|
process.env[`DATABASE_URL`] = `${client}://${user}:${password}@${host}:${port}/${TestDbMngr.dbName}`; |
|
|
|
process.env[`DATABASE_URL`] = `${client}://${user}:${password}@${host}:${port}/${TestDbMngr.dbName}`; |
|
|
|
} |
|
|
|
} |
|
|
|