refactor: Refactor database and config init (#8527)

* Refactor database and config init
Connect to the database without loading full config
Load config completely so post-processing is always done consistently.
Erase existing $config when loading, fixes issues in case we load the config twice.
If the database is not connected, don't try to load database settings. (Fixes some db errors on install)
Attempt to remove $config access/modification before init.php
Remove usage of db_name, that might not match the connected database.
Centralize db config loading, so we consistently apply db_test database settings.
Many of these changes are influenced by Laravel port.

* Some safety so we don't assign strings to numeric port field
Smooth out phpunit bootstrap

* Fix a couple of scrutinizer warnings.
This commit is contained in:
Tony Murray
2018-04-11 10:15:13 -05:00
committed by Neil Lathwood
parent c09e44aa94
commit d841625f12
15 changed files with 190 additions and 161 deletions

View File

@@ -23,13 +23,15 @@
* @author Tony Murray <murraytony@gmail.com>
*/
use LibreNMS\Config;
use LibreNMS\Exceptions\DatabaseConnectException;
use LibreNMS\Util\Snmpsim;
global $config;
$install_dir = realpath(__DIR__ . '/..');
$init_modules = array('web', 'discovery', 'polling');
$init_modules = array('web', 'discovery', 'polling', 'nodb');
if (!getenv('SNMPSIM')) {
$init_modules[] = 'mocksnmp';
@@ -39,8 +41,6 @@ if (getenv('DBTEST')) {
if (!is_file($install_dir . '/config.php')) {
exec("cp $install_dir/tests/config/config.test.php $install_dir/config.php");
}
} else {
$init_modules[] = 'nodb';
}
require $install_dir . '/includes/init.php';
@@ -64,13 +64,22 @@ if (getenv('SNMPSIM')) {
if (getenv('DBTEST')) {
global $schema, $sql_mode;
try {
dbConnect();
} catch (DatabaseConnectException $e) {
echo $e->getMessage() . PHP_EOL;
}
$sql_mode = dbFetchCell("SELECT @@global.sql_mode");
$empty_db = (dbFetchCell("SELECT count(*) FROM `information_schema`.`tables` WHERE `table_type` = 'BASE TABLE' AND `table_schema` = ?", array($config['db_name'])) == 0);
$db_name = dbFetchCell("SELECT DATABASE()");
$empty_db = (dbFetchCell("SELECT count(*) FROM `information_schema`.`tables` WHERE `table_type` = 'BASE TABLE' AND `table_schema` = ?", [$db_name]) == 0);
dbQuery("SET GLOBAL sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'");
$cmd = $config['install_dir'] . '/build-base.php';
exec($cmd, $schema);
Config::load(); // reload the config including database config
register_shutdown_function(function () use ($empty_db, $sql_mode) {
global $config;
dbConnect();
@@ -80,9 +89,10 @@ if (getenv('DBTEST')) {
// restore sql_mode
dbQuery("SET GLOBAL sql_mode='$sql_mode'");
$db_name = dbFetchCell('SELECT DATABASE()');
if ($empty_db) {
dbQuery("DROP DATABASE " . $config['db_name']);
} elseif (isset($config['test_db_name']) && $config['db_name'] == $config['test_db_name']) {
dbQuery("DROP DATABASE $db_name");
} elseif (isset($config['test_db_name']) && $config['test_db_name'] == $db_name) {
// truncate tables
$tables = dbFetchColumn('SHOW TABLES');