1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webparking\DbRebuild\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Contracts\Console\Kernel; |
7
|
|
|
use Illuminate\Database\Connection; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Webparking\DbRebuild\Config; |
10
|
|
|
|
11
|
|
|
class DbRebuild extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The name and signature of the console command. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $signature = 'db:rebuild {--preset=default} {--f}'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Saves sessions & users table, drops everything, migrates, seeds and optionally devSeeds'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Connection |
29
|
|
|
*/ |
30
|
|
|
private $connection; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Kernel |
34
|
|
|
*/ |
35
|
|
|
private $artisan; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \Illuminate\Support\Collection |
39
|
|
|
*/ |
40
|
|
|
protected $backup; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Config |
44
|
|
|
*/ |
45
|
|
|
protected $config; |
46
|
|
|
|
47
|
|
|
public function __construct(Connection $connection, Kernel $artisan) |
48
|
|
|
{ |
49
|
|
|
$this->connection = $connection; |
50
|
|
|
$this->artisan = $artisan; |
51
|
|
|
|
52
|
|
|
parent::__construct(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function handle(): void |
56
|
|
|
{ |
57
|
|
|
if (app()->environment('production')) { |
|
|
|
|
58
|
|
|
$this->error('Rebuilding in production is not allowed!'); |
59
|
|
|
|
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->config = new Config($this->option('preset')); |
64
|
|
|
|
65
|
|
|
$this->backupData(); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$this->migrate(); |
69
|
|
|
$this->callCommands(); |
70
|
|
|
$this->seed(); |
71
|
|
|
} catch (\Exception $e) { |
72
|
|
|
$this->restoreData(); |
73
|
|
|
|
74
|
|
|
throw $e; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->restoreData(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function backupData(): void |
81
|
|
|
{ |
82
|
|
|
$this->info('Backing up data'); |
83
|
|
|
|
84
|
|
|
$this->backup = collect(); |
85
|
|
|
|
86
|
|
|
foreach ($this->config->getBackup() as $table) { |
87
|
|
|
if ($this->connection->getSchemaBuilder()->hasTable($table)) { |
88
|
|
|
$this->info('Backing up ' . $table); |
89
|
|
|
$this->backup->put($table, $this->connection->table($table)->get()); |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->warn('Table not found: ' . $table); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function restoreData(): void |
98
|
|
|
{ |
99
|
|
|
$this->backup->each(function (Collection $data, string $table) { |
100
|
|
|
if ($this->connection->getSchemaBuilder()->hasTable($table)) { |
101
|
|
|
$this->info('Restoring ' . $table); |
102
|
|
|
|
103
|
|
|
$data->each(function ($record) use ($table) { |
104
|
|
|
$this->connection->table($table)->insert(get_object_vars($record)); |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function migrate(): void |
113
|
|
|
{ |
114
|
|
|
$database = $this->config->getDatabase(); |
115
|
|
|
|
116
|
|
|
if ($this->realConfirm( |
117
|
|
|
"This will drop all tables in {$database}. Are you sure you want to do this? [yes|no]", |
118
|
|
|
true |
119
|
|
|
)) { |
120
|
|
|
$this->connection->statement('SET FOREIGN_KEY_CHECKS=0;'); |
121
|
|
|
$tables = $this->connection->select('SHOW TABLES'); |
122
|
|
|
|
123
|
|
|
foreach ($tables as $table) { |
124
|
|
|
$tableName = $table->{key($table)}; |
125
|
|
|
$this->info('dropping table ' . $tableName); |
126
|
|
|
$this->connection->getSchemaBuilder()->dropIfExists($tableName); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->connection->statement('SET FOREIGN_KEY_CHECKS=1;'); |
130
|
|
|
|
131
|
|
|
$this->info("\nAll tables in {$database} dropped!"); |
132
|
|
|
|
133
|
|
|
$this->info("\nMigration started"); |
134
|
|
|
$this->artisan->call('migrate'); |
135
|
|
|
$this->info('Migration finished'); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function callCommands(): void |
140
|
|
|
{ |
141
|
|
|
foreach ($this->config->getCommands() as $command) { |
142
|
|
|
$this->info('Calling command: ' . $command); |
143
|
|
|
$this->artisan->call($command); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private function seed(): void |
148
|
|
|
{ |
149
|
|
|
foreach ($this->config->getSeeds() as $seed) { |
150
|
|
|
$this->info('Calling seeder: ' . $seed); |
151
|
|
|
$this->artisan->call('db:seed', [ |
152
|
|
|
'--class' => $seed, |
153
|
|
|
]); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
159
|
|
|
*/ |
160
|
|
|
private function realConfirm(string $msg, bool $default = false): bool |
161
|
|
|
{ |
162
|
|
|
if ($this->option('f')) { |
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$answer = $this->choice($msg, ['No', 'Yes'], (true === $default) ? '1' : '0'); |
167
|
|
|
switch ($answer) { |
168
|
|
|
case 'No': |
169
|
|
|
return false; |
170
|
|
|
case 'Yes': |
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|