Merged in afiqhamzah/add-debug-cawangan-connection-command (pull request #43)
Add command to debug cawangan connections
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Cawangan;
|
||||
use App\Helper;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class CawanganDebugConnections extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'cawangan:debug-connections';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Debugs cawangan connections';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
||||
$all_cawangan = Cawangan::withoutGlobalScopes()->where('db_name', 'like', 'arrahn_%')->get(['mysql', 'db_name'])->toArray();
|
||||
|
||||
$all_cawangan_in_config = config('database.connections');
|
||||
$all_cawangan_in_config_keys = array_keys($all_cawangan_in_config);
|
||||
|
||||
$all_cawangan_in_config = array_reduce($all_cawangan_in_config_keys, function ($accumulator, $current_key_value) use ($all_cawangan_in_config) {
|
||||
|
||||
$accumulator[] = [
|
||||
'mysql' => $current_key_value,
|
||||
'database' => $all_cawangan_in_config[$current_key_value]['database'],
|
||||
];
|
||||
|
||||
return $accumulator;
|
||||
|
||||
}, []);
|
||||
|
||||
$all_cawangan_in_config = array_filter($all_cawangan_in_config, function ($connection) {
|
||||
|
||||
$is_cawangan_databases = strpos($connection['database'], 'arrahn_') === 0;
|
||||
|
||||
if ($is_cawangan_databases) {
|
||||
return true;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
function filterArraysByDatabaseNames($array1, $array2)
|
||||
{
|
||||
$idToNameMap = [];
|
||||
foreach ($array1 as $item) {
|
||||
$idToNameMap[$item['mysql']] = $item['db_name'];
|
||||
}
|
||||
|
||||
$filteredArray2 = array_filter($array2, function ($item) use ($idToNameMap) {
|
||||
return $idToNameMap[$item['mysql']] !== $item['database'];
|
||||
});
|
||||
|
||||
return $filteredArray2;
|
||||
}
|
||||
|
||||
$filteredConnections = filterArraysByDatabaseNames($all_cawangan, $all_cawangan_in_config);
|
||||
|
||||
// checks if there is connection errors
|
||||
if (Helper::getInactiveCawanganDbConnection() > 0) {
|
||||
dump('Inactive cawangan connection detected : ');
|
||||
dump(Helper::getInactiveCawanganDbConnection());
|
||||
}
|
||||
|
||||
// checks if config connection do not exist referring to arrahn_master_db
|
||||
if (count($all_cawangan) !== count($all_cawangan_in_config)) {
|
||||
$db_not_config = array_diff(array_column($all_cawangan, 'db_name'), array_column($all_cawangan_in_config, 'database'));
|
||||
|
||||
dump(implode(', ', $db_not_config) . ' not configured in database config');
|
||||
}
|
||||
|
||||
// checks if config connection databases are not tally fwith arrahn master db
|
||||
if (count($filteredConnections) !== 0) {
|
||||
dump('Database config names are not tally arrahn_master_db : ');
|
||||
dump($filteredConnections);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ class Kernel extends ConsoleKernel
|
||||
protected $commands = [
|
||||
Commands\DailyGadai::class,
|
||||
Commands\MigrationOnePercent::class,
|
||||
Commands\CawanganDebugConnections::class,
|
||||
|
||||
];
|
||||
|
||||
@@ -31,7 +32,7 @@ class Kernel extends ConsoleKernel
|
||||
protected function schedule(Schedule $schedule)
|
||||
{
|
||||
//
|
||||
|
||||
|
||||
$schedule->command('gadai:daily')->daily()->timezone('Asia/Kuala_Lumpur')->at('00:00');
|
||||
}
|
||||
}
|
||||
|
||||
+69
-16
@@ -2,6 +2,7 @@
|
||||
namespace App;
|
||||
|
||||
use DateTime;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class Helper
|
||||
@@ -32,29 +33,81 @@ class Helper
|
||||
|
||||
public static function getActiveCawanganDbConnection()
|
||||
{
|
||||
$queryActiveCawanganDbConnection = function () {
|
||||
$all_active_db_keys = array_filter(array_keys(config('database.connections')), function ($connection) {
|
||||
try {
|
||||
DB::connection($connection)->getPdo();
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
$non_cawangan_database = ['lelong', 'online_arrahn', 'kaunterpkb'];
|
||||
|
||||
$all_cawangan_db = array_filter($all_active_db_keys, function ($db_key) use ($non_cawangan_database) {
|
||||
$isNotCawangan = in_array(config('database.connections.' . $db_key . '.database'), $non_cawangan_database);
|
||||
|
||||
if ($isNotCawangan) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$all_active_db_keys = array_filter(array_keys(config('database.connections')), function ($connection) {
|
||||
try {
|
||||
DB::connection($connection)->getPdo();
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$non_cawangan_database = ['lelong', 'online_arrahn', 'kaunterpkb'];
|
||||
return $all_cawangan_db;
|
||||
|
||||
$all_cawangan_db = array_filter($all_active_db_keys, function ($db_key) use ($non_cawangan_database) {
|
||||
$isNotCawangan = in_array(config('database.connections.' . $db_key . '.database'), $non_cawangan_database);
|
||||
};
|
||||
|
||||
if ($isNotCawangan) {
|
||||
return false;
|
||||
}
|
||||
$all_cawangan_db_connection = Cache::store('file')->get('active_cawangan_db_connection');
|
||||
|
||||
return true;
|
||||
});
|
||||
if ($all_cawangan_db_connection === null or count($all_cawangan_db_connection) === 0) {
|
||||
$all_cawangan_db_connection = $queryActiveCawanganDbConnection();
|
||||
Cache::store('file')->put('active_cawangan_db_connection', $all_cawangan_db_connection, 3600);
|
||||
}
|
||||
|
||||
return $all_cawangan_db;
|
||||
return $all_cawangan_db_connection;
|
||||
|
||||
}
|
||||
|
||||
public static function getInactiveCawanganDbConnection()
|
||||
{
|
||||
$queryInactiveCawanganDbConnection = function () {
|
||||
$all_inactive_db_keys = array_filter(array_keys(config('database.connections')), function ($connection) {
|
||||
try {
|
||||
DB::connection($connection)->getPdo();
|
||||
return false;
|
||||
} catch (\Throwable $th) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
$non_cawangan_database = ['lelong', 'online_arrahn', 'kaunterpkb'];
|
||||
|
||||
$all_cawangan_db = array_filter($all_inactive_db_keys, function ($db_key) use ($non_cawangan_database) {
|
||||
$isNotCawangan = in_array(config('database.connections.' . $db_key . '.database'), $non_cawangan_database);
|
||||
$isNotCawanganKey = in_array($db_key, $non_cawangan_database);
|
||||
|
||||
if ($isNotCawangan OR $isNotCawanganKey) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return $all_cawangan_db;
|
||||
|
||||
};
|
||||
|
||||
$all_cawangan_db_connection = array_map(function ($db_connection) {
|
||||
return [
|
||||
'connection' => $db_connection,
|
||||
'database' => config('database.connections.' . $db_connection . '.database'),
|
||||
];
|
||||
}, $queryInactiveCawanganDbConnection());
|
||||
|
||||
return $all_cawangan_db_connection;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user