-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the "level" column in "log" table to be integer again (#5)
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |