Commit 7c5938f9 by Alexander Makarov

#4791: Added more colors to `yii migrate` command

parent 338b3342
......@@ -316,6 +316,7 @@ Yii Framework 2 Change Log
- Enh #4062: Added 'caseSensitive' option to `yii\helpers\BaseFileHelper::findFiles()` (klimov-paul)
- Enh #4691: Encoding on `ActiveForm` and `ActiveField` validation errors is now configurable (Alex-Code)
- Enh #4740: Added `yii\web\Session::addFlash()` (restyler)
- Enh #4791: Added more colors to `yii migrate` command (6pblcb, samdark)
- Enh #4897: Added `yii\helpers\FileHelper::mimeMagicFile` (qiangxue)
- Enh #5058: Added `$pageSize` parameter to `Pagination::createUrl()` to allow creating URLs with arbitrary page sizes (cdcchen, qiangxue)
- Enh #5089: Added asset debugger panel (arturf, qiangxue)
......
......@@ -10,6 +10,7 @@ namespace yii\console\controllers;
use Yii;
use yii\console\Exception;
use yii\console\Controller;
use yii\helpers\Console;
use yii\helpers\FileHelper;
/**
......@@ -66,13 +67,12 @@ abstract class BaseMigrateController extends Controller
if (parent::beforeAction($action)) {
$path = Yii::getAlias($this->migrationPath);
if (!is_dir($path)) {
echo "";
FileHelper::createDirectory($path);
}
$this->migrationPath = $path;
$version = Yii::getVersion();
echo "Yii Migration Tool (based on Yii v{$version})\n\n";
$this->stdout("Yii Migration Tool (based on Yii v{$version})\n\n");
return true;
} else {
......@@ -98,7 +98,7 @@ abstract class BaseMigrateController extends Controller
{
$migrations = $this->getNewMigrations();
if (empty($migrations)) {
echo "No new migration found. Your system is up-to-date.\n";
$this->stdout("No new migration found. Your system is up-to-date.\n", Console::FG_GREEN);
return self::EXIT_CODE_NORMAL;
}
......@@ -111,25 +111,25 @@ abstract class BaseMigrateController extends Controller
$n = count($migrations);
if ($n === $total) {
echo "Total $n new " . ($n === 1 ? 'migration' : 'migrations') . " to be applied:\n";
$this->stdout("Total $n new " . ($n === 1 ? 'migration' : 'migrations') . " to be applied:\n", Console::FG_YELLOW);
} else {
echo "Total $n out of $total new " . ($total === 1 ? 'migration' : 'migrations') . " to be applied:\n";
$this->stdout("Total $n out of $total new " . ($total === 1 ? 'migration' : 'migrations') . " to be applied:\n", Console::FG_YELLOW);
}
foreach ($migrations as $migration) {
echo " $migration\n";
$this->stdout("\t$migration\n");
}
echo "\n";
$this->stdout("\n");
if ($this->confirm('Apply the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
foreach ($migrations as $migration) {
if (!$this->migrateUp($migration)) {
echo "\nMigration failed. The rest of the migrations are canceled.\n";
$this->stdout("\nMigration failed. The rest of the migrations are canceled.\n", Console::FG_RED);
return self::EXIT_CODE_ERROR;
}
}
echo "\nMigrated up successfully.\n";
$this->stdout("\nMigrated up successfully.\n", Console::FG_GREEN);
}
}
......@@ -163,7 +163,7 @@ abstract class BaseMigrateController extends Controller
$migrations = $this->getMigrationHistory($limit);
if (empty($migrations)) {
echo "No migration has been done before.\n";
$this->stdout("No migration has been done before.\n", Console::FG_YELLOW);
return self::EXIT_CODE_NORMAL;
}
......@@ -171,21 +171,21 @@ abstract class BaseMigrateController extends Controller
$migrations = array_keys($migrations);
$n = count($migrations);
echo "Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be reverted:\n";
$this->stdout("Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be reverted:\n", Console::FG_YELLOW);
foreach ($migrations as $migration) {
echo " $migration\n";
$this->stdout("\t$migration\n");
}
echo "\n";
$this->stdout("\n");
if ($this->confirm('Revert the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
foreach ($migrations as $migration) {
if (!$this->migrateDown($migration)) {
echo "\nMigration failed. The rest of the migrations are canceled.\n";
$this->stdout("\nMigration failed. The rest of the migrations are canceled.\n", Console::FG_RED);
return self::EXIT_CODE_ERROR;
}
}
echo "\nMigrated down successfully.\n";
$this->stdout("\nMigrated down successfully.\n", Console::FG_GREEN);
}
}
......@@ -221,7 +221,7 @@ abstract class BaseMigrateController extends Controller
$migrations = $this->getMigrationHistory($limit);
if (empty($migrations)) {
echo "No migration has been done before.\n";
$this->stdout("No migration has been done before.\n", Console::FG_YELLOW);
return self::EXIT_CODE_NORMAL;
}
......@@ -229,28 +229,28 @@ abstract class BaseMigrateController extends Controller
$migrations = array_keys($migrations);
$n = count($migrations);
echo "Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be redone:\n";
$this->stdout("Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be redone:\n", Console::FG_YELLOW);
foreach ($migrations as $migration) {
echo " $migration\n";
$this->stdout("\t$migration\n");
}
echo "\n";
$this->stdout("\n");
if ($this->confirm('Redo the above ' . ($n === 1 ? 'migration' : 'migrations') . "?")) {
foreach ($migrations as $migration) {
if (!$this->migrateDown($migration)) {
echo "\nMigration failed. The rest of the migrations are canceled.\n";
$this->stdout("\nMigration failed. The rest of the migrations are canceled.\n", Console::FG_RED);
return self::EXIT_CODE_ERROR;
}
}
foreach (array_reverse($migrations) as $migration) {
if (!$this->migrateUp($migration)) {
echo "\nMigration failed. The rest of the migrations migrations are canceled.\n";
$this->stdout("\nMigration failed. The rest of the migrations migrations are canceled.\n", Console::FG_RED);
return self::EXIT_CODE_ERROR;
}
}
echo "\nMigration redone successfully.\n";
$this->stdout("\nMigration redone successfully.\n", Console::FG_GREEN);
}
}
......@@ -322,7 +322,7 @@ abstract class BaseMigrateController extends Controller
for ($j = 0; $j <= $i; ++$j) {
$this->addMigrationHistory($migrations[$j]);
}
echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n";
$this->stdout("The migration history is set at $originalVersion.\nNo actual migration was performed.\n", Console::FG_GREEN);
}
return self::EXIT_CODE_NORMAL;
......@@ -334,13 +334,13 @@ abstract class BaseMigrateController extends Controller
foreach ($migrations as $i => $migration) {
if (strpos($migration, $version . '_') === 0) {
if ($i === 0) {
echo "Already at '$originalVersion'. Nothing needs to be done.\n";
$this->stdout("Already at '$originalVersion'. Nothing needs to be done.\n", Console::FG_YELLOW);
} else {
if ($this->confirm("Set migration history at $originalVersion?")) {
for ($j = 0; $j < $i; ++$j) {
$this->removeMigrationHistory($migrations[$j]);
}
echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n";
$this->stdout("The migration history is set at $originalVersion.\nNo actual migration was performed.\n", Console::FG_GREEN);
}
}
......@@ -381,16 +381,16 @@ abstract class BaseMigrateController extends Controller
$migrations = $this->getMigrationHistory($limit);
if (empty($migrations)) {
echo "No migration has been done before.\n";
$this->stdout("No migration has been done before.\n", Console::FG_YELLOW);
} else {
$n = count($migrations);
if ($limit > 0) {
echo "Showing the last $n applied " . ($n === 1 ? 'migration' : 'migrations') . ":\n";
$this->stdout("Showing the last $n applied " . ($n === 1 ? 'migration' : 'migrations') . ":\n", Console::FG_YELLOW);
} else {
echo "Total $n " . ($n === 1 ? 'migration has' : 'migrations have') . " been applied before:\n";
$this->stdout("Total $n " . ($n === 1 ? 'migration has' : 'migrations have') . " been applied before:\n", Console::FG_YELLOW);
}
foreach ($migrations as $version => $time) {
echo " (" . date('Y-m-d H:i:s', $time) . ') ' . $version . "\n";
$this->stdout("\t(" . date('Y-m-d H:i:s', $time) . ') ' . $version . "\n");
}
}
}
......@@ -425,18 +425,18 @@ abstract class BaseMigrateController extends Controller
$migrations = $this->getNewMigrations();
if (empty($migrations)) {
echo "No new migrations found. Your system is up-to-date.\n";
$this->stdout("No new migrations found. Your system is up-to-date.\n", Console::FG_GREEN);
} else {
$n = count($migrations);
if ($limit && $n > $limit) {
$migrations = array_slice($migrations, 0, $limit);
echo "Showing $limit out of $n new " . ($n === 1 ? 'migration' : 'migrations') . ":\n";
$this->stdout("Showing $limit out of $n new " . ($n === 1 ? 'migration' : 'migrations') . ":\n", Console::FG_YELLOW);
} else {
echo "Found $n new " . ($n === 1 ? 'migration' : 'migrations') . ":\n";
$this->stdout("Found $n new " . ($n === 1 ? 'migration' : 'migrations') . ":\n", Console::FG_YELLOW);
}
foreach ($migrations as $migration) {
echo " " . $migration . "\n";
$this->stdout("\t" . $migration . "\n");
}
}
}
......@@ -468,7 +468,7 @@ abstract class BaseMigrateController extends Controller
if ($this->confirm("Create new migration '$file'?")) {
$content = $this->renderFile(Yii::getAlias($this->templateFile), ['className' => $name]);
file_put_contents($file, $content);
echo "New migration created successfully.\n";
$this->stdout("New migration created successfully.\n", Console::FG_GREEN);
}
}
......@@ -483,19 +483,19 @@ abstract class BaseMigrateController extends Controller
return true;
}
echo "*** applying $class\n";
$this->stdout("*** applying $class\n", Console::FG_YELLOW);
$start = microtime(true);
$migration = $this->createMigration($class);
if ($migration->up() !== false) {
$this->addMigrationHistory($class);
$time = microtime(true) - $start;
echo "*** applied $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
$this->stdout("*** applied $class (time: " . sprintf("%.3f", $time) . "s)\n\n", Console::FG_GREEN);
$this->refreshSchema();
return true;
} else {
$time = microtime(true) - $start;
echo "*** failed to apply $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
$this->stdout("*** failed to apply $class (time: " . sprintf("%.3f", $time) . "s)\n\n", Console::FG_RED);
return false;
}
......@@ -512,19 +512,19 @@ abstract class BaseMigrateController extends Controller
return true;
}
echo "*** reverting $class\n";
$this->stdout("*** reverting $class\n", Console::FG_YELLOW);
$start = microtime(true);
$migration = $this->createMigration($class);
if ($migration->down() !== false) {
$this->removeMigrationHistory($class);
$time = microtime(true) - $start;
echo "*** reverted $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
$this->stdout("*** reverted $class (time: " . sprintf("%.3f", $time) . "s)\n\n", Console::FG_GREEN);
return true;
} else {
$time = microtime(true) - $start;
echo "*** failed to revert $class (time: " . sprintf("%.3f", $time) . "s)\n\n";
$this->stdout("*** failed to revert $class (time: " . sprintf("%.3f", $time) . "s)\n\n", Console::FG_RED);
return false;
}
......@@ -555,7 +555,7 @@ abstract class BaseMigrateController extends Controller
++$count;
}
if ($count === 0) {
echo "Nothing needs to be done.\n";
$this->stdout("Nothing needs to be done.\n", Console::FG_GREEN);
} else {
$this->actionDown($count);
}
......@@ -586,7 +586,7 @@ abstract class BaseMigrateController extends Controller
foreach ($migrations as $i => $migration) {
if (strpos($migration, $version . '_') === 0) {
if ($i === 0) {
echo "Already at '$originalVersion'. Nothing needs to be done.\n";
$this->stdout("Already at '$originalVersion'. Nothing needs to be done.\n", Console::FG_YELLOW);
} else {
$this->actionDown($i);
}
......
......@@ -12,6 +12,7 @@ use yii\console\Exception;
use yii\db\Connection;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\helpers\Console;
/**
* Manages application migrations.
......@@ -144,7 +145,7 @@ class MigrateController extends BaseMigrateController
protected function createMigrationHistoryTable()
{
$tableName = $this->db->schema->getRawTableName($this->migrationTable);
echo "Creating migration history table \"$tableName\"...";
$this->stdout("Creating migration history table \"$tableName\"...", Console::FG_YELLOW);
$this->db->createCommand()->createTable($this->migrationTable, [
'version' => 'varchar(180) NOT NULL PRIMARY KEY',
'apply_time' => 'integer',
......@@ -153,7 +154,7 @@ class MigrateController extends BaseMigrateController
'version' => self::BASE_MIGRATION,
'apply_time' => time(),
])->execute();
echo "done.\n";
$this->stdout("Done.\n", Console::FG_GREEN);
}
/**
......@@ -185,6 +186,6 @@ class MigrateController extends BaseMigrateController
protected function refreshSchema($name = 'db')
{
$this->db->schema->refresh();
echo "DB schema cache was flushed.\n";
$this->stdout("DB schema cache was flushed.\n", Console::FG_GREEN);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment