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