1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mtolhuijs\LDS\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use mtolhuijs\LDS\DatabaseSynchronizer; |
7
|
|
|
use mtolhuijs\LDS\Exceptions\DatabaseConnectionException; |
8
|
|
|
|
9
|
|
|
class Synchronise extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = ' |
17
|
|
|
db:sync |
18
|
|
|
{ --from= : Synchronize data from this database instead of the one specified in config } |
19
|
|
|
{ --to= : Synchronize data to this database instead of the one specified in config } |
20
|
|
|
{ --t|tables=* : Only run for given table(s) } |
21
|
|
|
{ --st|skip-tables=* : Skip given table(s) } |
22
|
|
|
{ --l|limit= : Limit query rows (defaults to 5000) } |
23
|
|
|
{ --m|migrate : Run migrations before synchronization } |
24
|
|
|
{ --truncate : Truncate before inserting data } |
25
|
|
|
'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Synchronizes your \'from\' database with your \'to\' database'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute the console command. |
36
|
|
|
* |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function handle() |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
|
|
(new DatabaseSynchronizer( |
43
|
|
|
$this->option('from') ?? config('database-synchronizer.from'), |
44
|
|
|
$this->option('to') ?? config('database-synchronizer.to'), |
45
|
|
|
$this |
46
|
|
|
)) |
47
|
|
|
->setTables($this->getTables()) |
48
|
|
|
->setSkipTables($this->getSkipTables()) |
49
|
|
|
->setLimit((int) $this->getLimit()) |
50
|
|
|
->setOptions($this->options()) |
51
|
|
|
->run(); |
52
|
|
|
} catch (DatabaseConnectionException $e) { |
53
|
|
|
$this->error($e->getMessage()); |
54
|
|
|
|
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->info(PHP_EOL.'Synchronization done!'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function getTables() |
62
|
|
|
{ |
63
|
|
|
return empty($this->option('tables')) ? |
64
|
|
|
config('database-synchronizer.tables') : $this->option('tables'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function getSkipTables() |
68
|
|
|
{ |
69
|
|
|
return empty($this->option('skip-tables')) ? |
70
|
|
|
config('database-synchronizer.skip_tables') : $this->option('skip-tables'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function getLimit() |
74
|
|
|
{ |
75
|
|
|
return $this->option('limit') ?? config('database-synchronizer.limit', DatabaseSynchronizer::DEFAULT_LIMIT); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|