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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user