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