adjusted all framework commands to use proper exit codes

use console exit codes introduced in pull request #647
This commit is contained in:
Carsten Brandt
2012-04-27 23:01:24 +02:00
parent a6d8222df1
commit 4fd934d0bd
8 changed files with 69 additions and 41 deletions

View File

@@ -15,6 +15,13 @@
* the yii-dbmigrations extension ((https://github.com/pieterclaerhout/yii-dbmigrations),
* authored by Pieter Claerhout.
*
* Since version 1.1.11 this command will exit with the following exit status codes:
* <ul>
* <li>0 on success</li>
* <li>1 on general error</li>
* <li>2 on failing migration.</li>
* </ul>
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.cli.commands
@@ -60,14 +67,16 @@ class MigrateCommand extends CConsoleCommand
public function beforeAction($action,$params)
{
$path=Yii::getPathOfAlias($this->migrationPath);
if($path===false || !is_dir($path))
die('Error: The migration directory does not exist: '.$this->migrationPath."\n");
if($path===false || !is_dir($path)) {
echo 'Error: The migration directory does not exist: '.$this->migrationPath."\n";
exit(1);
}
$this->migrationPath=$path;
$yiiVersion=Yii::getVersion();
echo "\nYii Migration Tool v1.0 (based on Yii v{$yiiVersion})\n\n";
return true;
return parent::beforeAction($action,$params);
}
public function actionUp($args)
@@ -75,7 +84,7 @@ class MigrateCommand extends CConsoleCommand
if(($migrations=$this->getNewMigrations())===array())
{
echo "No new migration found. Your system is up-to-date.\n";
return;
return 0;
}
$total=count($migrations);
@@ -100,7 +109,7 @@ class MigrateCommand extends CConsoleCommand
if($this->migrateUp($migration)===false)
{
echo "\nMigration failed. All later migrations are canceled.\n";
return;
return 2;
}
}
echo "\nMigrated up successfully.\n";
@@ -110,13 +119,15 @@ class MigrateCommand extends CConsoleCommand
public function actionDown($args)
{
$step=isset($args[0]) ? (int)$args[0] : 1;
if($step<1)
die("Error: The step parameter must be greater than 0.\n");
if($step<1) {
echo "Error: The step parameter must be greater than 0.\n";
return 1;
}
if(($migrations=$this->getMigrationHistory($step))===array())
{
echo "No migration has been done before.\n";
return;
return 0;
}
$migrations=array_keys($migrations);
@@ -133,7 +144,7 @@ class MigrateCommand extends CConsoleCommand
if($this->migrateDown($migration)===false)
{
echo "\nMigration failed. All later migrations are canceled.\n";
return;
return 2;
}
}
echo "\nMigrated down successfully.\n";
@@ -143,13 +154,15 @@ class MigrateCommand extends CConsoleCommand
public function actionRedo($args)
{
$step=isset($args[0]) ? (int)$args[0] : 1;
if($step<1)
die("Error: The step parameter must be greater than 0.\n");
if($step<1) {
echo "Error: The step parameter must be greater than 0.\n";
return 1;
}
if(($migrations=$this->getMigrationHistory($step))===array())
{
echo "No migration has been done before.\n";
return;
return 0;
}
$migrations=array_keys($migrations);
@@ -166,7 +179,7 @@ class MigrateCommand extends CConsoleCommand
if($this->migrateDown($migration)===false)
{
echo "\nMigration failed. All later migrations are canceled.\n";
return;
return 2;
}
}
foreach(array_reverse($migrations) as $migration)
@@ -174,7 +187,7 @@ class MigrateCommand extends CConsoleCommand
if($this->migrateUp($migration)===false)
{
echo "\nMigration failed. All later migrations are canceled.\n";
return;
return 2;
}
}
echo "\nMigration redone successfully.\n";
@@ -191,18 +204,17 @@ class MigrateCommand extends CConsoleCommand
$originalVersion=$version;
if(preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/',$version,$matches))
$version='m'.$matches[1];
else
die("Error: The version option must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).\n");
else {
echo "Error: The version option must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).\n";
return 1;
}
// try migrate up
$migrations=$this->getNewMigrations();
foreach($migrations as $i=>$migration)
{
if(strpos($migration,$version.'_')===0)
{
$this->actionUp(array($i+1));
return;
}
return $this->actionUp(array($i+1));
}
// try migrate down
@@ -211,15 +223,16 @@ class MigrateCommand extends CConsoleCommand
{
if(strpos($migration,$version.'_')===0)
{
if($i===0)
if($i===0) {
echo "Already at '$originalVersion'. Nothing needs to be done.\n";
else
$this->actionDown(array($i));
return;
return 0;
} else
return $this->actionDown(array($i));
}
}
die("Error: Unable to find the version '$originalVersion'.\n");
echo "Error: Unable to find the version '$originalVersion'.\n";
return 1;
}
public function actionMark($args)
@@ -231,8 +244,10 @@ class MigrateCommand extends CConsoleCommand
$originalVersion=$version;
if(preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/',$version,$matches))
$version='m'.$matches[1];
else
die("Error: The version option must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).\n");
else {
echo "Error: The version option must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).\n";
return 1;
}
$db=$this->getDbConnection();
@@ -254,7 +269,7 @@ class MigrateCommand extends CConsoleCommand
}
echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n";
}
return;
return 0;
}
}
@@ -276,11 +291,12 @@ class MigrateCommand extends CConsoleCommand
echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n";
}
}
return;
return 0;
}
}
die("Error: Unable to find the version '$originalVersion'.\n");
echo "Error: Unable to find the version '$originalVersion'.\n";
return 1;
}
public function actionHistory($args)
@@ -330,8 +346,10 @@ class MigrateCommand extends CConsoleCommand
else
$this->usageError('Please provide the name of the new migration.');
if(!preg_match('/^\w+$/',$name))
die("Error: The name of the migration must contain letters, digits and/or underscore characters only.\n");
if(!preg_match('/^\w+$/',$name)) {
echo "Error: The name of the migration must contain letters, digits and/or underscore characters only.\n";
return 1;
}
$name='m'.gmdate('ymd_His').'_'.$name;
$content=strtr($this->getTemplate(), array('{ClassName}'=>$name));
@@ -418,8 +436,9 @@ class MigrateCommand extends CConsoleCommand
return $this->_db;
else if(($this->_db=Yii::app()->getComponent($this->connectionID)) instanceof CDbConnection)
return $this->_db;
else
die("Error: CMigrationCommand.connectionID '{$this->connectionID}' is invalid. Please make sure it refers to the ID of a CDbConnection application component.\n");
echo "Error: CMigrationCommand.connectionID '{$this->connectionID}' is invalid. Please make sure it refers to the ID of a CDbConnection application component.\n";
exit(1);
}
protected function getMigrationHistory($limit)

View File

@@ -77,6 +77,7 @@ EOD;
/**
* Execute the action.
* @param array command line parameters specific for this command
* @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
@@ -84,7 +85,7 @@ EOD;
{
echo "Error: controller name is required.\n";
echo $this->getHelp();
return;
return 1;
}
$module=Yii::app();

View File

@@ -84,6 +84,7 @@ EOD;
/**
* Execute the action.
* @param array command line parameters specific for this command
* @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
@@ -91,7 +92,7 @@ EOD;
{
echo "Error: data model class is required.\n";
echo $this->getHelp();
return;
return 1;
}
$module=Yii::app();
$modelClass=$args[0];

View File

@@ -61,6 +61,7 @@ EOD;
/**
* Execute the action.
* @param array command line parameters specific for this command
* @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
@@ -68,7 +69,7 @@ EOD;
{
echo "Error: both model class and view name are required.\n";
echo $this->getHelp();
return;
return 1;
}
$scenario=isset($args[2]) ? $args[2] : '';
$modelClass=Yii::import($args[0],true);

View File

@@ -24,6 +24,7 @@ class HelpCommand extends CConsoleCommand
/**
* Execute the action.
* @param array command line parameters specific for this command
* @return integer non zero application exit code for help
*/
public function run($args)
{
@@ -54,6 +55,7 @@ EOD;
}
else
echo $runner->createCommand($name)->getHelp();
return 1;
}
/**

View File

@@ -246,6 +246,7 @@ EOD;
/**
* Execute the action.
* @param array command line parameters specific for this command
* @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
@@ -253,7 +254,7 @@ EOD;
{
echo "Error: model class name is required.\n";
echo $this->getHelp();
return;
return 1;
}
$className=$args[0];
@@ -262,7 +263,7 @@ EOD;
echo "Error: an active 'db' connection is required.\n";
echo "If you already added 'db' component in application configuration,\n";
echo "please quit and re-enter the yiic shell.\n";
return;
return 1;
}
$db->active=true;
@@ -271,7 +272,7 @@ EOD;
if(!preg_match('/^[\w\.\-\*]*(.*?)$/',$className,$matches))
{
echo "Error: model class name is invalid.\n";
return;
return 1;
}
if(empty($matches[1])) // without regular expression

View File

@@ -44,6 +44,7 @@ EOD;
/**
* Execute the action.
* @param array command line parameters specific for this command
* @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
@@ -51,7 +52,7 @@ EOD;
{
echo "Error: module ID is required.\n";
echo $this->getHelp();
return;
return 1;
}
$moduleID=$args[0];

View File

@@ -33,6 +33,7 @@ class CHelpCommand extends CConsoleCommand
/**
* Execute the action.
* @param array $args command line parameters specific for this command
* @return integer non zero application exit code after printing help
*/
public function run($args)
{
@@ -62,6 +63,7 @@ class CHelpCommand extends CConsoleCommand
}
else
echo $runner->createCommand($name)->getHelp();
return 1;
}
/**