@ -99,17 +99,19 @@ class MigrateController extends Controller
public function beforeAction($action)
public function beforeAction($action)
{
{
if (parent::beforeAction($action)) {
$path = Yii::getAlias($this->migrationPath);
$path = Yii::getAlias($this->migrationPath);
if ($path === false || !is_dir($path)) {
if ($path === false || !is_dir($path)) {
echo 'Error: the migration directory does not exist "' . $this->migrationPath . "\"\n";
echo 'Error: the migration directory does not exist "' . $this->migrationPath . "\"\n";
return false;
return false;
}
}
$this->migrationPath = $path;
$this->migrationPath = $path;
$version = Yii::getVersion();
$yiiVersion = Yii::getVersion();
echo "\nYii Migration Tool v2.0 (based on Yii v{$version})\n\n";
echo "\nYii Migration Tool v2.0 (based on Yii v{$yiiVersion})\n\n";
return true;
} else {
return parent::beforeAction($action);
return false;
}
}
}
/**
/**
@ -129,15 +131,15 @@ class MigrateController extends Controller
}
}
$n = count($migrations);
$n = count($migrations);
if ($n === $total)
if ($n === $total) {
echo "Total $n new " . ($n === 1 ? 'migration' : 'migrations') . " to be applied:\n";
echo "Total $n new " . ($n === 1 ? 'migration' : 'migrations') . " to be applied:\n";
else
} else {
{
echo "Total $n out of $total new " . ($total === 1 ? 'migration' : 'migrations') . " to be applied:\n";
echo "Total $n out of $total new " . ($total === 1 ? 'migration' : 'migrations') . " to be applied:\n";
}
}
foreach ($migrations as $migration)
foreach ($migrations as $migration) {
echo " $migration\n";
echo " $migration\n";
}
echo "\n";
echo "\n";
if ($this->confirm('Apply the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
if ($this->confirm('Apply the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
@ -154,8 +156,9 @@ class MigrateController extends Controller
public function actionDown($args)
public function actionDown($args)
{
{
$step = isset($args[0]) ? (int)$args[0] : 1;
$step = isset($args[0]) ? (int)$args[0] : 1;
if ($step <1)
if ($step <1){
die("Error: The step parameter must be greater than 0.\n");
die("Error: The step parameter must be greater than 0.\n");
}
if (($migrations = $this->getMigrationHistory($step)) === array()) {
if (($migrations = $this->getMigrationHistory($step)) === array()) {
echo "No migration has been done before.\n";
echo "No migration has been done before.\n";
@ -165,8 +168,9 @@ class MigrateController extends Controller
$n = count($migrations);
$n = count($migrations);
echo "Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be reverted:\n";
echo "Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be reverted:\n";
foreach ($migrations as $migration)
foreach ($migrations as $migration) {
echo " $migration\n";
echo " $migration\n";
}
echo "\n";
echo "\n";
if ($this->confirm('Revert the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
if ($this->confirm('Revert the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
@ -183,8 +187,9 @@ class MigrateController extends Controller
public function actionRedo($args)
public function actionRedo($args)
{
{
$step = isset($args[0]) ? (int)$args[0] : 1;
$step = isset($args[0]) ? (int)$args[0] : 1;
if ($step <1)
if ($step <1){
die("Error: The step parameter must be greater than 0.\n");
die("Error: The step parameter must be greater than 0.\n");
}
if (($migrations = $this->getMigrationHistory($step)) === array()) {
if (($migrations = $this->getMigrationHistory($step)) === array()) {
echo "No migration has been done before.\n";
echo "No migration has been done before.\n";
@ -194,8 +199,9 @@ class MigrateController extends Controller
$n = count($migrations);
$n = count($migrations);
echo "Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be redone:\n";
echo "Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be redone:\n";
foreach ($migrations as $migration)
foreach ($migrations as $migration) {
echo " $migration\n";
echo " $migration\n";
}
echo "\n";
echo "\n";
if ($this->confirm('Redo the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
if ($this->confirm('Redo the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
@ -217,16 +223,18 @@ class MigrateController extends Controller
public function actionTo($args)
public function actionTo($args)
{
{
if (isset($args[0]))
if (isset($args[0])) {
$version = $args[0];
$version = $args[0];
else
} else {
$this->usageError('Please specify which version to migrate to.');
$this->usageError('Please specify which version to migrate to.');
}
$originalVersion = $version;
$originalVersion = $version;
if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches))
if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches)) {
$version = 'm' . $matches[1];
$version = 'm' . $matches[1];
else
} 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");
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");
}
// try migrate up
// try migrate up
$migrations = $this->getNewMigrations();
$migrations = $this->getNewMigrations();
@ -241,10 +249,11 @@ class MigrateController extends Controller
echo "Already at '$originalVersion'. Nothing needs to be done.\n";
echo "Already at '$originalVersion'. Nothing needs to be done.\n";
else
} else {
$this->actionDown(array($i));
$this->actionDown(array($i));
}
return;
return;
}
}
}
}
@ -254,15 +263,17 @@ class MigrateController extends Controller
public function actionMark($args)
public function actionMark($args)
{
{
if (isset($args[0]))
if (isset($args[0])) {
$version = $args[0];
$version = $args[0];
else
} else {
$this->usageError('Please specify which version to mark to.');
$this->usageError('Please specify which version to mark to.');
}
$originalVersion = $version;
$originalVersion = $version;
if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches))
if (preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/', $version, $matches)) {
$version = 'm' . $matches[1];
$version = 'm' . $matches[1];
else
} 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");
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");
}
$db = $this->getDb();
$db = $this->getDb();
@ -288,13 +299,14 @@ class MigrateController extends Controller
@ -363,8 +381,9 @@ class MigrateController extends Controller
protected function migrateUp($class)
protected function migrateUp($class)
{
{
if ($class === self::BASE_MIGRATION)
if ($class === self::BASE_MIGRATION) {
return;
return;
}
echo "*** applying $class\n";
echo "*** applying $class\n";
$start = microtime(true);
$start = microtime(true);
@ -385,8 +404,9 @@ class MigrateController extends Controller
protected function migrateDown($class)
protected function migrateDown($class)
{
{
if ($class === self::BASE_MIGRATION)
if ($class === self::BASE_MIGRATION) {
return;
return;
}
echo "*** reverting $class\n";
echo "*** reverting $class\n";
$start = microtime(true);
$start = microtime(true);
@ -419,13 +439,16 @@ class MigrateController extends Controller
protected function getDb()
protected function getDb()
{
{
if ($this->_db !== null)
if ($this->_db !== null) {
return $this->_db;
return $this->_db;
else if (($this->_db = Yii::$application->getComponent($this->connectionID)) instanceof CDbConnection)
} else {
if (($this->_db = Yii::$application->getComponent($this->connectionID)) instanceof CDbConnection) {
return $this->_db;
return $this->_db;
else
} else {
die("Error: CMigrationCommand.connectionID '{$this->connectionID}' is invalid. Please make sure it refers to the ID of a CDbConnection application component.\n");
die("Error: CMigrationCommand.connectionID '{$this->connectionID}' is invalid. Please make sure it refers to the ID of a CDbConnection application component.\n");
}
}
}
}
protected function getMigrationHistory($limit)
protected function getMigrationHistory($limit)
{
{
@ -459,18 +482,21 @@ class MigrateController extends Controller
protected function getNewMigrations()
protected function getNewMigrations()
{
{
$applied = array();
$applied = array();
foreach ($this->getMigrationHistory(-1) as $version => $time)
foreach ($this->getMigrationHistory(-1) as $version => $time) {