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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
<?php
/**
* Copyright (c) 2007-2011 bitExpert AG
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
require_once 'phing/Task.php';
require_once 'phing/tasks/system/ExecTask.php';
/**
* Abstract Liquibase task. Base class for all Liquibase Phing tasks.
*
* @author Stephan Hochdoerfer <S.Hochdoerfer@bitExpert.de>
* @version $Id: 3c8b30c0b13290cf7b158dbda6d28491afeb326e $
* @since 2.4.10
* @package phing.tasks.ext.liquibase
*/
abstract class AbstractLiquibaseTask extends Task
{
/**
* Used for liquibase -Dname=value properties.
*/
private $properties = array();
/**
* Used to set liquibase --name=value parameters
*/
private $parameters = array();
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 boolean $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.
*
* @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
)
);
}
if((null === $this->changeLogFile) or !file_exists($this->changeLogFile))
{
throw new BuildException(
sprintf(
'Specify the name of the Changelog file. "%s" does not exist!',
$this->changeLogFile
)
);
}
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 string the command to execute
* @param string additional parameters
* @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 = array();
$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;
}
}
/**
* @author Stephan Hochdoerfer <S.Hochdoerfer@bitExpert.de>
* @version $Id: 3c8b30c0b13290cf7b158dbda6d28491afeb326e $
* @since 2.4.10
* @package phing.tasks.ext.liquibase
*/
class LiquibaseParameter extends DataType
{
private $name;
private $value;
public function setName($name) {
$this->name = $name;
}
public function setValue($value) {
$this->value = $value;
}
public function getCommandline(Project $p) {
if ($this->isReference()) {
return $this->getRef($p)->getCommandline($p);
}
return sprintf("--%s=%s", $this->name, escapeshellarg($this->value));
}
public function getRef(Project $p) {
if ( !$this->checked ) {
$stk = array();
array_push($stk, $this);
$this->dieOnCircularReference($stk, $p);
}
$o = $this->ref->getReferencedObject($p);
if ( !($o instanceof LiquibaseParameter) ) {
throw new BuildException($this->ref->getRefId()." doesn't denote a LiquibaseParameter");
} else {
return $o;
}
}
}
/**
* @author Stephan Hochdoerfer <S.Hochdoerfer@bitExpert.de>
* @version $Id: 3c8b30c0b13290cf7b158dbda6d28491afeb326e $
* @since 2.4.10
* @package phing.tasks.ext.liquibase
*/
class LiquibaseProperty extends DataType
{
private $name;
private $value;
public function setName($name) {
$this->name = $name;
}
public function setValue($value) {
$this->value = $value;
}
public function getCommandline(Project $p) {
if ($this->isReference()) {
return $this->getRef($p)->getCommandline($p);
}
return sprintf("-D%s=%s", $this->name, escapeshellarg($this->value));
}
public function getRef(Project $p) {
if ( !$this->checked ) {
$stk = array();
array_push($stk, $this);
$this->dieOnCircularReference($stk, $p);
}
$o = $this->ref->getReferencedObject($p);
if ( !($o instanceof LiquibaseProperty) ) {
throw new BuildException($this->ref->getRefId()." doesn't denote a LiquibaseProperty");
} else {
return $o;
}
}
}
|