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

@@ -13,6 +13,7 @@
*/
use LibreNMS\Config;
use LibreNMS\Exceptions\DatabaseConnectException;
use LibreNMS\Exceptions\LockException;
use LibreNMS\Util\FileLock;
use LibreNMS\Util\MemcacheLock;
@@ -21,8 +22,14 @@ global $database_link;
if (!isset($init_modules) && php_sapi_name() == 'cli') {
// Not called from within discovery, let's load up the necessary stuff.
$init_modules = array();
$init_modules = array('nodb');
require realpath(__DIR__ . '/../..') . '/includes/init.php';
try {
dbConnect();
} catch (DatabaseConnectException $e) {
echo $e->getMessage() . PHP_EOL;
exit;
}
}
$return = 0;
@@ -37,7 +44,8 @@ try {
}
// only import build.sql to an empty database
$tables = dbFetchRows("SHOW TABLES FROM " . Config::get('db_name'));
$tables = dbFetchRows("SHOW TABLES");
if (empty($tables)) {
echo "-- Creating base database structure\n";
$step = 0;
@@ -70,7 +78,7 @@ try {
d_echo("DB Schema already up to date.\n");
} else {
// Set Database Character set and Collation
dbQuery('ALTER DATABASE ? CHARACTER SET utf8 COLLATE utf8_unicode_ci;', array(array(Config::get('db_name'))));
dbQuery('ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_unicode_ci;');
$db_rev = get_db_schema();
$insert = ($db_rev == 0); // if $db_rev == 0, insert the first update