Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the "level" column in "log" table to be integer again #5

Merged
merged 2 commits into from
Jan 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/migrations/m180103_040100_log_change_level_to_int.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

use yii\base\InvalidConfigException;
use yii\db\Migration;
use yii\log\DbTarget;

/**
*
* @author Dzhuneyt Ahmed <dzhuneyt@dzhuneyt.com>
*/
class m180103_040100_log_change_level_to_int extends Migration
{

/**
* @var DbTarget[] Targets to create log table for
*/
protected $dbTargets = [];

/**
* @throws InvalidConfigException
* @return DbTarget[]
*/
protected function getDbTargets()
{
if ($this->dbTargets === []) {
$logger = Yii::getLogger();
if (!$logger instanceof \yii\log\Logger) {
throw new InvalidConfigException('You should configure "logger" to be instance of "\yii\log\Logger" before executing this migration.');
}

$usedTargets = [];
foreach ($logger->targets as $target) {
if ($target instanceof DbTarget) {
$currentTarget = [
$target->db,
$target->logTable,
];
if (!in_array($currentTarget, $usedTargets, true)) {
// do not create same table twice
$usedTargets[] = $currentTarget;
$this->dbTargets[] = $target;
}
}
}

if ($this->dbTargets === []) {
throw new InvalidConfigException('You should configure "log" component to use one or more database targets before executing this migration.');
}
}

return $this->dbTargets;
}

public function up()
{
foreach ($this->getDbTargets() as $target) {
$this->db = $target->db;
$this->alterColumn($target->logTable, 'level', $this->integer());
}
}

public function down()
{
foreach ($this->getDbTargets() as $target) {
$this->db = $target->db;
$this->alterColumn($target->logTable, 'level', $this->string());
}
}
}