webparking /
laravel-db-rebuild
| 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 | 15 | public function __construct(Connection $connection, Kernel $artisan) |
|
| 48 | { |
||
| 49 | 15 | $this->connection = $connection; |
|
| 50 | 15 | $this->artisan = $artisan; |
|
| 51 | |||
| 52 | 15 | parent::__construct(); |
|
| 53 | 15 | } |
|
| 54 | |||
| 55 | 15 | public function handle(): void |
|
| 56 | { |
||
| 57 | 15 | if (app()->environment('production')) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 58 | 1 | $this->error('Rebuilding in production is not allowed!'); |
|
| 59 | |||
| 60 | 1 | return; |
|
| 61 | } |
||
| 62 | |||
| 63 | 14 | $this->config = new Config($this->option('preset')); |
|
| 64 | |||
| 65 | 12 | if (!$this->realConfirm("This will drop all tables in {$this->config->getDatabase()}. Are you sure you want to do this? [yes|no]", true)) { |
|
| 66 | 2 | $this->warn('Stopped rebuild process!'); |
|
| 67 | |||
| 68 | 2 | return; |
|
| 69 | } |
||
| 70 | |||
| 71 | 9 | $this->backupData(); |
|
| 72 | |||
| 73 | try { |
||
| 74 | 8 | $this->migrate(); |
|
| 75 | 8 | $this->callCommands(); |
|
| 76 | 7 | $this->seed(); |
|
| 77 | 2 | } catch (\Exception $e) { |
|
| 78 | 2 | $this->restoreData(); |
|
| 79 | |||
| 80 | 2 | throw $e; |
|
| 81 | } |
||
| 82 | |||
| 83 | 6 | $this->restoreData(); |
|
| 84 | 6 | } |
|
| 85 | |||
| 86 | 9 | private function backupData(): void |
|
| 87 | { |
||
| 88 | 9 | $this->info('Backing up data'); |
|
| 89 | |||
| 90 | 9 | $this->backup = collect(); |
|
| 91 | |||
| 92 | 9 | foreach ($this->config->getBackup() as $table) { |
|
| 93 | 2 | if ($this->connection->getSchemaBuilder()->hasTable($table)) { |
|
| 94 | 1 | $this->info('Backing up ' . $table); |
|
| 95 | 1 | $this->backup->put($table, $this->connection->table($table)->get()); |
|
| 96 | 1 | continue; |
|
| 97 | } |
||
| 98 | |||
| 99 | 1 | $this->warn('Table not found: ' . $table); |
|
| 100 | } |
||
| 101 | 8 | } |
|
| 102 | |||
| 103 | 8 | private function restoreData(): void |
|
| 104 | { |
||
| 105 | $this->backup->each(function (Collection $data, string $table) { |
||
| 106 | 1 | if ($this->connection->getSchemaBuilder()->hasTable($table)) { |
|
| 107 | 1 | $this->info('Restoring ' . $table); |
|
| 108 | |||
| 109 | $data->each(function ($record) use ($table) { |
||
| 110 | 1 | $this->connection->table($table)->insert(get_object_vars($record)); |
|
| 111 | 1 | }); |
|
| 112 | |||
| 113 | 1 | return; |
|
| 114 | } |
||
| 115 | 8 | }); |
|
| 116 | 8 | } |
|
| 117 | |||
| 118 | 8 | private function migrate(): void |
|
| 119 | { |
||
| 120 | 8 | $database = $this->config->getDatabase(); |
|
| 121 | |||
| 122 | 8 | $this->connection->getSchemaBuilder()->disableForeignKeyConstraints(); |
|
| 123 | 8 | $tables = $this->connection->select('SHOW TABLES'); |
|
| 124 | |||
| 125 | 8 | foreach ($tables as $table) { |
|
| 126 | 8 | $tableName = $table->{key($table)}; |
|
| 127 | 8 | $this->info('dropping table ' . $tableName); |
|
| 128 | 8 | $this->connection->getSchemaBuilder()->dropIfExists($tableName); |
|
| 129 | } |
||
| 130 | |||
| 131 | 8 | $this->connection->getSchemaBuilder()->enableForeignKeyConstraints(); |
|
| 132 | |||
| 133 | 8 | $this->info("\nAll tables in {$database} dropped!"); |
|
| 134 | |||
| 135 | 8 | $this->info("\nMigration started"); |
|
| 136 | 8 | $this->artisan->call('migrate'); |
|
| 137 | 8 | $this->info('Migration finished'); |
|
| 138 | 8 | } |
|
| 139 | |||
| 140 | 8 | private function callCommands(): void |
|
| 141 | { |
||
| 142 | 8 | foreach ($this->config->getCommands() as $command) { |
|
| 143 | 1 | $this->info('Calling command: ' . $command); |
|
| 144 | 1 | $this->artisan->call($command); |
|
| 145 | } |
||
| 146 | 7 | } |
|
| 147 | |||
| 148 | 7 | private function seed(): void |
|
| 149 | { |
||
| 150 | 7 | foreach ($this->config->getSeeds() as $seed) { |
|
| 151 | 1 | $this->info('Calling seeder: ' . $seed); |
|
| 152 | 1 | $this->artisan->call('db:seed', [ |
|
| 153 | 1 | '--class' => $seed, |
|
| 154 | ]); |
||
| 155 | } |
||
| 156 | 6 | } |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 160 | */ |
||
| 161 | 11 | private function realConfirm(string $msg, bool $default = false): bool |
|
| 162 | { |
||
| 163 | 11 | if ($this->option('f')) { |
|
| 164 | 1 | return true; |
|
| 165 | } |
||
| 166 | |||
| 167 | 10 | $answer = $this->choice($msg, ['No', 'Yes'], (true === $default) ? '1' : '0'); |
|
| 168 | switch ($answer) { |
||
| 169 | 10 | case 'No': |
|
| 170 | 2 | return false; |
|
| 171 | 8 | case 'Yes': |
|
| 172 | 8 | return true; |
|
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 |