-
Notifications
You must be signed in to change notification settings - Fork 2
/
AbstractLiquibaseTask.php
328 lines (288 loc) · 8.55 KB
/
AbstractLiquibaseTask.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
namespace Phing\Task\Ext\Liquibase;
use Phing\Exception\BuildException;
use Phing\Task;
use Phing\Util\StringHelper;
/**
* Abstract Liquibase task. Base class for all Liquibase Phing tasks.
*
* @author Stephan Hochdoerfer <[email protected]>
* @since 2.4.10
* @package phing.tasks.ext.liquibase
*/
abstract class AbstractLiquibaseTask extends Task
{
/**
* Used for liquibase -Dname=value properties.
*/
private $properties = [];
/**
* Used to set liquibase --name=value parameters
*/
private $parameters = [];
protected $jar;
protected $changeLogFile;
protected $username;
protected $password;
protected $url;
protected $classpathref;
/**
* Whether to display the output of the command.
* True by default to preserve old behaviour
*
* @var boolean
*/
protected $display = true;
/**
* Whether liquibase return code can cause a Phing failure.
*
* @var boolean
*/
protected $checkreturn = false;
/**
* Set true if we should run liquibase with PHP passthru
* instead of exec.
*/
protected $passthru = true;
/**
* Property name to set with output value from exec call.
*
* @var string
*/
protected $outputProperty;
/**
* Sets the absolute path to liquibase jar.
*
* @param string the absolute path to the liquibase jar.
*/
public function setJar($jar)
{
$this->jar = $jar;
}
/**
* Sets the absolute path to the changelog file to use.
*
* @param string the absolute path to the changelog file
*/
public function setChangeLogFile($changelogFile)
{
$this->changeLogFile = $changelogFile;
}
/**
* Sets the username to connect to the database.
*
* @param string the username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* Sets the password to connect to the database.
*
* @param string the password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* Sets the url to connect to the database in jdbc style, e.g.
* <code>
* jdbc:postgresql://psqlhost/mydatabase
* </code>
*
* @param string jdbc connection string
*/
public function setUrl($url)
{
$this->url = $url;
}
/**
* Sets the Java classpathref.
*
* @param string A reference to the classpath that contains the database
* driver, liquibase.jar, and the changelog.xml file
*/
public function setclasspathref($classpathref)
{
$this->classpathref = $classpathref;
}
/**
* Sets whether to display the output of the command
*
* @param boolean $display
*/
public function setDisplay($display)
{
$this->display = StringHelper::booleanValue($display);
}
/**
* Whether to check the liquibase return code.
*
* @param boolean $checkreturn
*/
public function setCheckreturn($checkreturn)
{
$this->checkreturn = StringHelper::booleanValue($checkreturn);
}
/**
* Whether to check the liquibase return code.
*
* @param $passthru
* @internal param bool $checkreturn
*/
public function setPassthru($passthru)
{
$this->passthru = StringHelper::booleanValue($passthru);
}
/**
* the name of property to set to output value from exec() call.
*
* @param string $prop property name
*
* @return void
*/
public function setOutputProperty($prop)
{
$this->outputProperty = $prop;
}
/**
* Creates a nested <property> tag.
*
* @return LiquibaseProperty Argument object
*/
public function createProperty()
{
$prop = new LiquibaseProperty();
$this->properties[] = $prop;
return $prop;
}
/**
* Creates a nested <parameter> tag.
*
* @return LiquibaseParameter Argument object
*/
public function createParameter()
{
$param = new LiquibaseParameter();
$this->parameters[] = $param;
return $param;
}
/**
* Ensure that correct parameters were passed in.
*
* @throws BuildException
* @return void
*/
protected function checkParams()
{
if ((null === $this->jar) or !file_exists($this->jar)) {
throw new BuildException(
sprintf(
'Specify the name of the LiquiBase.jar. "%s" does not exist!',
$this->jar
)
);
}
$this->checkChangeLogFile();
if (null === $this->classpathref) {
throw new BuildException('Please provide a classpath!');
}
if (null === $this->username) {
throw new BuildException('Please provide a username for database acccess!');
}
if (null === $this->password) {
throw new BuildException('Please provide a password for database acccess!');
}
if (null === $this->url) {
throw new BuildException('Please provide a url for database acccess!');
}
}
/**
* Executes the given command and returns the output.
*
* @param $lbcommand
* @param string $lbparams the command to execute
* @throws BuildException
* @return string the output of the executed command
*/
protected function execute($lbcommand, $lbparams = '')
{
$nestedparams = "";
foreach ($this->parameters as $p) {
$nestedparams .= $p->getCommandline($this->project) . ' ';
}
$nestedprops = "";
foreach ($this->properties as $p) {
$nestedprops .= $p->getCommandline($this->project) . ' ';
}
$command = sprintf(
'java -jar %s --changeLogFile=%s --url=%s --username=%s --password=%s --classpath=%s %s %s %s %s 2>&1',
escapeshellarg($this->jar),
escapeshellarg($this->changeLogFile),
escapeshellarg($this->url),
escapeshellarg($this->username),
escapeshellarg($this->password),
escapeshellarg($this->classpathref),
$nestedparams,
escapeshellarg($lbcommand),
$lbparams,
$nestedprops
);
if ($this->passthru) {
passthru($command);
} else {
$output = [];
$return = null;
exec($command, $output, $return);
$output = implode(PHP_EOL, $output);
if ($this->display) {
print $output;
}
if (!empty($this->outputProperty)) {
$this->project->setProperty($this->outputProperty, $output);
}
if ($this->checkreturn && $return != 0) {
throw new BuildException("Liquibase exited with code $return");
}
}
return;
}
protected function checkChangeLogFile()
{
if (null === $this->changeLogFile) {
throw new BuildException('Specify the name of the changelog file.');
}
foreach (explode(":", $this->classpathref) as $path) {
if (file_exists($path . DIRECTORY_SEPARATOR . $this->changeLogFile)) {
return;
}
}
if (!file_exists($this->changeLogFile)) {
throw new BuildException(
sprintf(
'The changelog file "%s" does not exist!',
$this->changeLogFile
)
);
}
}
}