1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
|
7
|
|
|
class NntmuxOptimizeTables extends Command |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name and signature of the console command. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $signature = 'nntmux:optimize-tables |
15
|
|
|
{--t|table=* : Table to optimize or all tables.}'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Checks and optimizes tables.'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Execute the console command. |
26
|
|
|
*/ |
27
|
|
|
public function handle(): void |
28
|
|
|
{ |
29
|
|
|
$this->info('Checking and optimizing tables.'); |
30
|
|
|
$table = $this->option('table'); |
31
|
|
|
if (empty($table) || $table[0] === 'all') { |
32
|
|
|
$this->optimizeAllTables(); |
33
|
|
|
} else { |
34
|
|
|
$this->optimizeTable($table); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Optimize all tables. |
40
|
|
|
*/ |
41
|
|
|
private function optimizeAllTables(): void |
42
|
|
|
{ |
43
|
|
|
$tables = \DB::select('SHOW TABLES'); |
44
|
|
|
foreach ($tables as $table) { |
45
|
|
|
$this->optimizeTable($table->Tables_in_nntmux); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Optimize a single table. |
51
|
|
|
*/ |
52
|
|
|
private function optimizeTable(array|string $tables): void |
53
|
|
|
{ |
54
|
|
|
if (is_array($tables)) { |
|
|
|
|
55
|
|
|
foreach ($tables as $table) { |
56
|
|
|
$this->tableCheck($table); |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
$this->tableCheck($tables); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function tableCheck($table): void |
64
|
|
|
{ |
65
|
|
|
$this->info('Checking table: '.$table); |
66
|
|
|
$tableCheck = \DB::select('CHECK TABLE '.$table); |
67
|
|
|
if ($tableCheck[0]->Msg_text !== 'OK') { |
68
|
|
|
$this->error('Table '.$table.' is corrupted. Please repair it.'); |
69
|
|
|
$this->info('Optimizing table: '.$table); |
70
|
|
|
\DB::select('OPTIMIZE TABLE '.$table); |
71
|
|
|
} else { |
72
|
|
|
$this->info('Table '.$table.' is ok. Optimization is not needed.'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|