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

@@ -424,8 +424,10 @@ function delete_device($id)
dbDelete('sensors_to_state_indexes', "`sensor_id` = ?", array($sensor_id));
}
$fields = array('device_id','host');
$db_name = dbFetchCell('SELECT DATABASE()');
foreach ($fields as $field) {
foreach (dbFetch("SELECT table_name FROM information_schema.columns WHERE table_schema = ? AND column_name = ?", array($config['db_name'],$field)) as $table) {
foreach (dbFetch("SELECT table_name FROM information_schema.columns WHERE table_schema = ? AND column_name = ?", [$db_name, $field]) as $table) {
$table = $table['table_name'];
$entries = (int) dbDelete($table, "`$field` = ?", array($id));
if ($entries > 0 && $debug === true) {
@@ -2294,10 +2296,11 @@ function dump_db_schema()
global $config;
$output = array();
$db_name = dbFetchCell('SELECT DATABASE()');
foreach (dbFetchRows("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = '{$config['db_name']}' ORDER BY TABLE_NAME;") as $table) {
foreach (dbFetchRows("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = '$db_name' ORDER BY TABLE_NAME;") as $table) {
$table = $table['TABLE_NAME'];
foreach (dbFetchRows("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, EXTRA FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '{$config['db_name']}' AND TABLE_NAME='$table'") as $data) {
foreach (dbFetchRows("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, EXTRA FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '$db_name' AND TABLE_NAME='$table'") as $data) {
$def = array(
'Field' => $data['COLUMN_NAME'],
'Type' => $data['COLUMN_TYPE'],