From 00554d78caaaa56c87a002a71191d647f4e29ba0 Mon Sep 17 00:00:00 2001 From: Dzhuneyt <1754428+Dzhuneyt@users.noreply.github.com> Date: Fri, 4 Jan 2019 16:09:53 +0200 Subject: [PATCH] Changed the "level" column in "log" table to be integer again (#5) --- ...m180103_040100_log_change_level_to_int.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/migrations/m180103_040100_log_change_level_to_int.php diff --git a/src/migrations/m180103_040100_log_change_level_to_int.php b/src/migrations/m180103_040100_log_change_level_to_int.php new file mode 100644 index 000000000..6994f5d16 --- /dev/null +++ b/src/migrations/m180103_040100_log_change_level_to_int.php @@ -0,0 +1,74 @@ + + */ +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()); + } + } +}