1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
|
4
|
|
|
## Configuration options |
5
|
|
|
|
6
|
|
|
All options are in the config parameter `phinx` specified as an array (instead of the `phinx_path` variable). |
7
|
|
|
All parameters are *optional*, but you can specify them with a dictionary (to change all parameters) |
8
|
|
|
or by deployer dot notation (to change one option). |
9
|
|
|
|
10
|
|
|
### Phinx params |
11
|
|
|
|
12
|
|
|
- `phinx.environment` |
13
|
|
|
- `phinx.date` |
14
|
|
|
- `phinx.configuration` N.B. current directory is the project directory |
15
|
|
|
- `phinx.target` |
16
|
|
|
- `phinx.seed` |
17
|
|
|
- `phinx.parser` |
18
|
|
|
- `phinx.remove-all` (pass empty string as value) |
19
|
|
|
|
20
|
|
|
### Phinx path params |
21
|
|
|
|
22
|
|
|
- `phinx_path` Specify phinx path (by default phinx is searched for in $PATH, ./vendor/bin and ~/.composer/vendor/bin) |
23
|
|
|
|
24
|
|
|
### Example of usage |
25
|
|
|
|
26
|
|
|
```php |
27
|
|
|
$phinx_env_vars = [ |
28
|
|
|
'environment' => 'development', |
29
|
|
|
'configuration' => './migration/.phinx.yml', |
30
|
|
|
'target' => '20120103083322', |
31
|
|
|
'remove-all' => '', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
set('phinx_path', '/usr/local/phinx/bin/phinx'); |
35
|
|
|
set('phinx', $phinx_env_vars); |
36
|
|
|
|
37
|
|
|
after('cleanup', 'phinx:migrate'); |
38
|
|
|
|
39
|
|
|
// or set it for a specific server |
40
|
|
|
host('dev') |
41
|
|
|
->user('user') |
42
|
|
|
->set('deploy_path', '/var/www') |
43
|
|
|
->set('phinx', $phinx_env_vars) |
44
|
|
|
->set('phinx_path', ''); |
45
|
|
|
``` |
46
|
|
|
|
47
|
|
|
## Suggested Usage |
48
|
|
|
|
49
|
|
|
You can run all tasks before or after any |
50
|
|
|
tasks (but you need to specify external configs for phinx). |
51
|
|
|
If you use internal configs (which are in your project) you need |
52
|
|
|
to run it after the `deploy:update_code` task is completed. |
53
|
|
|
|
54
|
|
|
## Read more |
55
|
|
|
|
56
|
|
|
For further reading see [phinx.org](https://phinx.org). Complete descriptions of all possible options can be found on the [commands page](http://docs.phinx.org/en/latest/commands.html). |
57
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
|
60
|
|
|
namespace Deployer; |
61
|
|
|
|
62
|
|
|
use Deployer\Exception\RunException; |
63
|
|
|
|
64
|
|
|
/* |
65
|
|
|
* Phinx recipe for Deployer |
66
|
|
|
* |
67
|
|
|
* @author Alexey Boyko <[email protected]> |
68
|
|
|
* @contributor Security-Database <[email protected]> |
69
|
|
|
* @copyright 2016 Alexey Boyko |
70
|
|
|
* @license MIT https://github.com/deployphp/recipes/blob/master/LICENSE |
71
|
|
|
* |
72
|
|
|
* @link https://github.com/deployphp/recipes |
73
|
|
|
* |
74
|
|
|
* @see http://deployer.org |
75
|
|
|
* @see https://phinx.org |
76
|
|
|
*/ |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Path to Phinx |
80
|
|
|
*/ |
81
|
|
|
set('bin/phinx', function () { |
82
|
|
|
try { |
83
|
|
|
$phinxPath = which('phinx'); |
84
|
|
|
} catch (RunException $e) { |
85
|
|
|
$phinxPath = null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($phinxPath !== null) { |
89
|
|
|
return "phinx"; |
90
|
|
|
} elseif (test('[ -f {{release_path}}/vendor/bin/phinx ]')) { |
91
|
|
|
return "{{release_path}}/vendor/bin/phinx"; |
92
|
|
|
} elseif (test('[ -f ~/.composer/vendor/bin/phinx ]')) { |
93
|
|
|
return '~/.composer/vendor/bin/phinx'; |
94
|
|
|
} else { |
95
|
|
|
throw new \RuntimeException('Cannot find phinx. Please specify path to phinx manually'); |
96
|
|
|
} |
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Make Phinx command |
101
|
|
|
* |
102
|
|
|
* @param string $cmdName Name of command |
103
|
|
|
* @param array $conf Command options(config) |
104
|
|
|
* |
105
|
|
|
* @return string Phinx command to execute |
106
|
|
|
*/ |
107
|
|
|
function phinx_get_cmd($cmdName, $conf) |
108
|
|
|
{ |
109
|
|
|
$phinx = get('phinx_path') ?: get('bin/phinx'); |
110
|
|
|
|
111
|
|
|
$phinxCmd = "$phinx $cmdName"; |
112
|
|
|
|
113
|
|
|
$options = ''; |
114
|
|
|
|
115
|
|
|
foreach ($conf as $name => $value) { |
116
|
|
|
$options .= " --$name $value"; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$phinxCmd .= $options; |
120
|
|
|
|
121
|
|
|
return $phinxCmd; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns options array that allowed for command |
126
|
|
|
* |
127
|
|
|
* @param array $allowedOptions List of allowed options |
128
|
|
|
* |
129
|
|
|
* @return array Array of options |
130
|
|
|
*/ |
131
|
|
|
function phinx_get_allowed_config($allowedOptions) |
132
|
|
|
{ |
133
|
|
|
$opts = []; |
134
|
|
|
|
135
|
|
|
try { |
136
|
|
|
foreach (get('phinx') as $key => $val) { |
137
|
|
|
if (in_array($key, $allowedOptions)) { |
138
|
|
|
$opts[$key] = $val; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} catch (\RuntimeException $e) { |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $opts; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
desc('Migrats database with phinx'); |
149
|
|
|
task('phinx:migrate', function () { |
150
|
|
|
$ALLOWED_OPTIONS = [ |
151
|
|
|
'configuration', |
152
|
|
|
'date', |
153
|
|
|
'environment', |
154
|
|
|
'target', |
155
|
|
|
'parser', |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
$conf = phinx_get_allowed_config($ALLOWED_OPTIONS); |
159
|
|
|
|
160
|
|
|
cd('{{release_path}}'); |
161
|
|
|
|
162
|
|
|
$phinxCmd = phinx_get_cmd('migrate', $conf); |
163
|
|
|
|
164
|
|
|
run($phinxCmd); |
165
|
|
|
|
166
|
|
|
cd('{{deploy_path}}'); |
167
|
|
|
}); |
168
|
|
|
|
169
|
|
|
desc('Rollbacks database migrations with phinx'); |
170
|
|
|
task('phinx:rollback', function () { |
171
|
|
|
$ALLOWED_OPTIONS = [ |
172
|
|
|
'configuration', |
173
|
|
|
'date', |
174
|
|
|
'environment', |
175
|
|
|
'target', |
176
|
|
|
'parser', |
177
|
|
|
]; |
178
|
|
|
|
179
|
|
|
$conf = phinx_get_allowed_config($ALLOWED_OPTIONS); |
180
|
|
|
|
181
|
|
|
cd('{{release_path}}'); |
182
|
|
|
|
183
|
|
|
$phinxCmd = phinx_get_cmd('rollback', $conf); |
184
|
|
|
|
185
|
|
|
run($phinxCmd); |
186
|
|
|
|
187
|
|
|
cd('{{deploy_path}}'); |
188
|
|
|
}); |
189
|
|
|
|
190
|
|
|
desc('Seeds database with phinx'); |
191
|
|
|
task('phinx:seed', function () { |
192
|
|
|
$ALLOWED_OPTIONS = [ |
193
|
|
|
'configuration', |
194
|
|
|
'environment', |
195
|
|
|
'parser', |
196
|
|
|
'seed', |
197
|
|
|
]; |
198
|
|
|
|
199
|
|
|
$conf = phinx_get_allowed_config($ALLOWED_OPTIONS); |
200
|
|
|
|
201
|
|
|
cd('{{release_path}}'); |
202
|
|
|
|
203
|
|
|
$phinxCmd = phinx_get_cmd('seed:run', $conf); |
204
|
|
|
|
205
|
|
|
run($phinxCmd); |
206
|
|
|
|
207
|
|
|
cd('{{deploy_path}}'); |
208
|
|
|
}); |
209
|
|
|
|
210
|
|
|
desc('Sets a migrations breakpoint with phinx'); |
211
|
|
|
task('phinx:breakpoint', function () { |
212
|
|
|
$ALLOWED_OPTIONS = [ |
213
|
|
|
'configuration', |
214
|
|
|
'environment', |
215
|
|
|
'remove-all', |
216
|
|
|
'target', |
217
|
|
|
]; |
218
|
|
|
|
219
|
|
|
$conf = phinx_get_allowed_config($ALLOWED_OPTIONS); |
220
|
|
|
|
221
|
|
|
cd('{{release_path}}'); |
222
|
|
|
|
223
|
|
|
$phinxCmd = phinx_get_cmd('breakpoint', $conf); |
224
|
|
|
|
225
|
|
|
run($phinxCmd); |
226
|
|
|
|
227
|
|
|
cd('{{deploy_path}}'); |
228
|
|
|
}); |
229
|
|
|
|