1 | <?php |
||
2 | /* (c) Anton Medvedev <[email protected]> |
||
3 | * |
||
4 | * For the full copyright and license information, please view the LICENSE |
||
5 | * file that was distributed with this source code. |
||
6 | */ |
||
7 | |||
8 | namespace Deployer; |
||
9 | |||
10 | use Deployer\Exception\RunException; |
||
11 | |||
12 | /* |
||
13 | * Phinx recipe for Deployer |
||
14 | * |
||
15 | * @author Alexey Boyko <[email protected]> |
||
16 | * @contributor Security-Database <[email protected]> |
||
17 | * @copyright 2016 Alexey Boyko |
||
18 | * @license MIT https://github.com/deployphp/recipes/blob/master/LICENSE |
||
19 | * |
||
20 | * @link https://github.com/deployphp/recipes |
||
21 | * |
||
22 | * @see http://deployer.org |
||
23 | * @see https://phinx.org |
||
24 | */ |
||
25 | |||
26 | /** |
||
27 | * Path to Phinx |
||
28 | */ |
||
29 | set('bin/phinx', function () { |
||
30 | try { |
||
31 | $phinxPath = run('which phinx'); |
||
32 | } catch (RunException $e) { |
||
33 | $phinxPath = null; |
||
34 | } |
||
35 | |||
36 | if ($phinxPath !== null) { |
||
37 | return "phinx"; |
||
38 | } else if (test('[ -f {{release_path}}/vendor/bin/phinx ]')) { |
||
39 | return "{{release_path}}/vendor/bin/phinx"; |
||
40 | } else if (test('[ -f ~/.composer/vendor/bin/phinx ]')) { |
||
41 | return '~/.composer/vendor/bin/phinx'; |
||
42 | } else { |
||
43 | throw new \RuntimeException('Cannot find phinx. Please specify path to phinx manually'); |
||
44 | } |
||
45 | } |
||
46 | ); |
||
47 | |||
48 | /** |
||
49 | * Make Phinx command |
||
50 | * |
||
51 | * @param string $cmdName Name of command |
||
52 | * @param array $conf Command options(config) |
||
53 | * |
||
54 | * @return string Phinx command to execute |
||
55 | */ |
||
56 | function phinx_get_cmd($cmdName, $conf) { |
||
57 | $phinx = get('phinx_path') ?: get('bin/phinx'); |
||
58 | |||
59 | $phinxCmd = "$phinx $cmdName"; |
||
60 | |||
61 | $options = ''; |
||
62 | |||
63 | foreach ($conf as $name => $value) { |
||
64 | $options .= " --$name $value"; |
||
65 | } |
||
66 | |||
67 | $phinxCmd .= $options; |
||
68 | |||
69 | return $phinxCmd; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Returns options array that allowed for command |
||
74 | * |
||
75 | * @param array $allowedOptions List of allowed options |
||
76 | * |
||
77 | * @return array Array of options |
||
78 | */ |
||
79 | function phinx_get_allowed_config($allowedOptions) { |
||
80 | $opts = []; |
||
81 | |||
82 | try { |
||
83 | foreach (get('phinx') as $key => $val) { |
||
84 | if (in_array($key, $allowedOptions)) { |
||
85 | $opts[$key] = $val; |
||
86 | } |
||
87 | } |
||
88 | } catch (\RuntimeException $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
89 | } |
||
90 | |||
91 | return $opts; |
||
92 | } |
||
93 | |||
94 | |||
95 | desc('Migrating database with phinx'); |
||
96 | task('phinx:migrate', function () { |
||
97 | $ALLOWED_OPTIONS = [ |
||
98 | 'configuration', |
||
99 | 'date', |
||
100 | 'environment', |
||
101 | 'target', |
||
102 | 'parser' |
||
103 | ]; |
||
104 | |||
105 | $conf = phinx_get_allowed_config($ALLOWED_OPTIONS); |
||
106 | |||
107 | cd('{{release_path}}'); |
||
108 | |||
109 | $phinxCmd = phinx_get_cmd('migrate', $conf); |
||
110 | |||
111 | run($phinxCmd); |
||
112 | |||
113 | cd('{{deploy_path}}'); |
||
114 | } |
||
115 | ); |
||
116 | |||
117 | desc('Rollback database migrations with phinx'); |
||
118 | task('phinx:rollback', function () { |
||
119 | $ALLOWED_OPTIONS = [ |
||
120 | 'configuration', |
||
121 | 'date', |
||
122 | 'environment', |
||
123 | 'target', |
||
124 | 'parser' |
||
125 | ]; |
||
126 | |||
127 | $conf = phinx_get_allowed_config($ALLOWED_OPTIONS); |
||
128 | |||
129 | cd('{{release_path}}'); |
||
130 | |||
131 | $phinxCmd = phinx_get_cmd('rollback', $conf); |
||
132 | |||
133 | run($phinxCmd); |
||
134 | |||
135 | cd('{{deploy_path}}'); |
||
136 | } |
||
137 | ); |
||
138 | |||
139 | desc('Seed database with phinx'); |
||
140 | task('phinx:seed', function () { |
||
141 | $ALLOWED_OPTIONS = [ |
||
142 | 'configuration', |
||
143 | 'environment', |
||
144 | 'parser', |
||
145 | 'seed' |
||
146 | ]; |
||
147 | |||
148 | $conf = phinx_get_allowed_config($ALLOWED_OPTIONS); |
||
149 | |||
150 | cd('{{release_path}}'); |
||
151 | |||
152 | $phinxCmd = phinx_get_cmd('seed:run', $conf); |
||
153 | |||
154 | run($phinxCmd); |
||
155 | |||
156 | cd('{{deploy_path}}'); |
||
157 | } |
||
158 | ); |
||
159 | |||
160 | desc('Set a migrations breakpoint with phinx'); |
||
161 | task('phinx:breakpoint', function () { |
||
162 | $ALLOWED_OPTIONS = [ |
||
163 | 'configuration', |
||
164 | 'environment', |
||
165 | 'remove-all', |
||
166 | 'target' |
||
167 | ]; |
||
168 | |||
169 | $conf = phinx_get_allowed_config($ALLOWED_OPTIONS); |
||
170 | |||
171 | cd('{{release_path}}'); |
||
172 | |||
173 | $phinxCmd = phinx_get_cmd('breakpoint', $conf); |
||
174 | |||
175 | run($phinxCmd); |
||
176 | |||
177 | cd('{{deploy_path}}'); |
||
178 | } |
||
179 | ); |
||
180 |