Skip to content

Commit

Permalink
Changed the "level" column in "log" table to be integer again (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzhuneyt authored and samdark committed Jan 4, 2019
1 parent 7165fc9 commit 00554d7
Showing 1 changed file with 74 additions and 0 deletions.
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());
}
}
}

0 comments on commit 00554d7

Please sign in to comment.