1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Lodash package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Longman\LaravelLodash\Commands; |
13
|
|
|
|
14
|
|
|
use DB; |
15
|
|
|
use Illuminate\Console\Command; |
16
|
|
|
use Illuminate\Console\ConfirmableTrait; |
17
|
|
|
|
18
|
|
|
class DbRestore extends Command |
19
|
|
|
{ |
20
|
|
|
use ConfirmableTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The name and signature of the console command. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $signature = 'db:restore {file : Dump file path to restore.} |
28
|
|
|
{--database= : The database connection to use.} |
29
|
|
|
{--force : Force the operation to run when in production.}'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The console command description. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $description = 'Restore mysql dump.'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Execute the console command. |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function handle() |
44
|
|
|
{ |
45
|
|
|
if (! $this->confirmToProceed('Application In Production! Will be imported sql file!')) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$db_conn = $this->getDatabase(); |
50
|
|
|
$connection = DB::connection($db_conn); |
51
|
|
|
|
52
|
|
|
$path = $this->getFilePath(); |
53
|
|
|
if (! file_exists($path)) { |
54
|
|
|
$this->error('File ' . $path . ' not found!'); |
55
|
|
|
|
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$connection->unprepared(file_get_contents($path)); |
60
|
|
|
|
61
|
|
|
$this->info('Database backup restored successfully'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function getDatabase(): string |
65
|
|
|
{ |
66
|
|
|
$database = $this->input->getOption('database'); |
67
|
|
|
|
68
|
|
|
return $database ?: $this->laravel['config']['database.default']; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getFilePath(): string |
72
|
|
|
{ |
73
|
|
|
$file = $this->input->getArgument('file'); |
74
|
|
|
|
75
|
|
|
return base_path($file); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|