* This method is invoked right before an action is to be executed (after all possible filters.)
* It checks the existence of the [[migrationPath]].
* @param \yii\base\Action $action the action to be executed.
* @return boolean whether the action should continue to be executed.
* @throws Exception if the migration directory does not exist.
*/
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
$path = Yii::getAlias($this->migrationPath);
if ($path === false || !is_dir($path)) {
echo 'Error: the migration directory does not exist "' . $this->migrationPath . "\"\n";
return false;
throw new Exception("The migration directory \"{$this->migrationPath}\" does not exist.");
}
$this->migrationPath = $path;
$version = Yii::getVersion();
echo "\nYii Migration Tool v2.0 (based on Yii v{$version})\n\n";
echo "\nYii Migration Tool (based on Yii v{$version})\n\n";
return true;
} else {
return false;
@ -115,7 +130,15 @@ class MigrateController extends Controller
}
/**
* @param array $args
* Upgrades the application by applying new migrations. For example,
*
* ~~~
* yiic migrate # apply all new migrations
* yiic migrate 3 # apply the first 3 new migrations
* ~~~
*
* @param array $args the number of new migrations to be applied. If not provided,
* all new migrations will be applied.
*/
public function actionUp($args)
{
@ -145,7 +168,7 @@ class MigrateController extends Controller
if ($this->confirm('Apply the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
foreach ($migrations as $migration) {
if ($this->migrateUp($migration) === false) {
echo "\nMigration failed. All later migrations are canceled.\n";
echo "\nMigration failed. The rest of the new migrations are canceled.\n";
return;
}
}
@ -153,11 +176,23 @@ class MigrateController extends Controller
}
}
/**
* Downgrades the application by reverting old migrations. For example,
*
* ~~~
* yiic migrate/down # revert the last migration
* yiic migrate/down 3 # revert the last 3 migrations
* ~~~
*
* @param array $args the number of migrations to be reverted. If not provided,
* the last applied migration will be reverted.
* @throws Exception if the number of the steps is less than 1.
*/
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");
throw new Exception("The step parameter must be greater than 0.");
}
if (($migrations = $this->getMigrationHistory($step)) === array()) {
@ -226,14 +261,14 @@ class MigrateController extends Controller
if (isset($args[0])) {
$version = $args[0];
} else {
$this->usageError('Please specify which version to migrate to.');
throw new Exception("Please specify which version to migrate to.");
}
$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");
throw new Exception("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).");
}
// try migrate up
@ -258,7 +293,7 @@ class MigrateController extends Controller
}
}
die("Error: Unable to find the version '$originalVersion'.\n");
throw new Exception("Unable to find the version '$originalVersion'.");
}
public function actionMark($args)
@ -266,13 +301,13 @@ class MigrateController extends Controller
if (isset($args[0])) {
$version = $args[0];
} else {
$this->usageError('Please specify which version to mark to.');
throw new Exception('Please specify which version to mark to.');
}
$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");
throw new Exception("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).");
}
$db = $this->getDb();
@ -357,12 +392,17 @@ class MigrateController extends Controller
}
}
/**
* Creates a new migration.
* @param array $args the name of the new migration.
* @throws Exception if the name of the new migration is not provided
*/
public function actionCreate($args)
{
if (isset($args[0])) {
$name = $args[0];
} else {
$this->usageError('Please provide the name of the new migration.');
throw new Exception('Please provide the name of the new migration.');
}
if (!preg_match('/^\w+$/', $name)) {
@ -428,7 +468,7 @@ class MigrateController extends Controller
@ -445,7 +485,7 @@ class MigrateController extends Controller
if (($this->_db = Yii::$application->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");
throw new Exception("Invalid DB connection: {$this->connectionID}.");
}
}
}
@ -505,7 +545,7 @@ class MigrateController extends Controller