|
1
|
|
|
<?php namespace Codengine\CustomMigrations; |
|
2
|
|
|
|
|
3
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class BatchMigrationTrait |
|
7
|
|
|
* Provides batch migration functions for multiple database connections |
|
8
|
|
|
* |
|
9
|
|
|
* @package Codengine\CustomMigrations |
|
10
|
|
|
*/ |
|
11
|
|
|
trait BatchMigrationTrait { |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $migrationType = 'default'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Extends the default options by type-option |
|
19
|
|
|
* |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function getOptions() |
|
23
|
|
|
{ |
|
24
|
|
|
$optExtend = array( |
|
25
|
|
|
array('type', null, InputOption::VALUE_OPTIONAL, 'The migration type to be executed.', 'default'), |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
return array_merge(parent::getOptions(), $optExtend); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Filters the connections and only returns the ones that match the migration type |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $connection The database connections |
|
35
|
|
|
* @return bool Returns TRUE on a match, else FALSE |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function filterConnections($connection) |
|
38
|
|
|
{ |
|
39
|
|
|
switch($this->migrationType) |
|
40
|
|
|
{ |
|
41
|
|
|
case 'default': |
|
42
|
|
|
return (empty($connection['migration_type']) || $connection['migration_type'] == 'default'); break; |
|
|
|
|
|
|
43
|
|
|
default: |
|
44
|
|
|
return (!empty($connection['migration_type']) && $connection['migration_type'] == $this->migrationType ? true : false); break; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns the default DB connection |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function getDefaultConnection() |
|
54
|
|
|
{ |
|
55
|
|
|
$defaultConnection = \DB::getDefaultConnection(); |
|
56
|
|
|
$connection = \Config::get('database.connections.' . $defaultConnection); |
|
57
|
|
|
return (empty($connection) ? array() : array($defaultConnection => $connection)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Retrieves database connections by type |
|
62
|
|
|
* |
|
63
|
|
|
* @param null|string $filter When specified (--database option), only this connection will be checked |
|
64
|
|
|
* @return array An array containing the matching connections |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getConnectionsByType($filter = null) |
|
67
|
|
|
{ |
|
68
|
|
|
$connections = array(); |
|
|
|
|
|
|
69
|
|
|
if($this->migrationType == "default" && empty($filter)) { |
|
70
|
|
|
return $this->getDefaultConnection(); |
|
71
|
|
|
} elseif (!empty($filter)) { |
|
72
|
|
|
$connections = \Config::get('database.connections.' . $filter); |
|
73
|
|
|
if(!empty($connections)) |
|
74
|
|
|
{ |
|
75
|
|
|
$connections = array($filter => $connections); |
|
76
|
|
|
} |
|
77
|
|
|
} else { |
|
78
|
|
|
$connections = \Config::get('database.connections'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if(!empty($connections)) |
|
82
|
|
|
{ |
|
83
|
|
|
$connections = array_filter($connections, array($this, 'filterConnections')); |
|
84
|
|
|
} |
|
85
|
|
|
return (array)$connections; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Retrieves and sets the migration type |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function setMigrationType() |
|
92
|
|
|
{ |
|
93
|
|
|
$this->migrationType = $this->input->getOption('type'); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Run a batch migration on the specified connections |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $connections |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function runMigrationsOnConnections($connections) |
|
102
|
|
|
{ |
|
103
|
|
|
foreach($connections as $name => $connection) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->input->setOption('database', $name); |
|
106
|
|
|
if(isset($this->migrator)) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->migrator->setMigrationType(array_get($connection, 'migration_type', 'default')); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
parent::fire(); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Default command override |
|
116
|
|
|
*/ |
|
117
|
|
|
public function fire() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->setMigrationType(); |
|
120
|
|
|
$connections = $this->getConnectionsByType($this->input->getOption('database')); |
|
121
|
|
|
if(empty($connections)) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->info("No connections found for the specified migration type"); |
|
|
|
|
|
|
124
|
|
|
} else { |
|
125
|
|
|
$this->runMigrationsOnConnections($connections); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.