👽 Update log_init migration to upstream

This commit is contained in:
Daniel Kesselberg 2018-08-13 15:50:06 +02:00
parent 587bd8d082
commit cf3c49cfb4

View File

@ -1,12 +1,11 @@
<?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\Schema;
use yii\db\Migration;
use yii\log\DbTarget;
@ -22,9 +21,8 @@ use yii\log\DbTarget;
*/
class m141106_185632_log_init extends Migration
{
/**
* @var DbTarget[]
* @var DbTarget[] Targets to create log table for
*/
private $dbTargets = [];
@ -37,9 +35,18 @@ class m141106_185632_log_init extends Migration
if ($this->dbTargets === []) {
$log = Yii::$app->getLog();
$usedTargets = [];
foreach ($log->targets as $target) {
if ($target instanceof DbTarget) {
$this->dbTargets[] = $target;
$currentTarget = [
$target->db,
$target->logTable,
];
if (!in_array($currentTarget, $usedTargets, true)) {
// do not create same table twice
$usedTargets[] = $currentTarget;
$this->dbTargets[] = $target;
}
}
}
@ -47,14 +54,13 @@ class m141106_185632_log_init extends Migration
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()
{
$targets = $this->getDbTargets();
foreach ($targets as $target) {
foreach ($this->getDbTargets() as $target) {
$this->db = $target->db;
$tableOptions = null;
@ -63,33 +69,26 @@ class m141106_185632_log_init extends Migration
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
try {
$this->createTable($target->logTable, [
'id' => Schema::TYPE_BIGPK,
'level' => Schema::TYPE_INTEGER,
'category' => Schema::TYPE_STRING,
'log_time' => Schema::TYPE_DOUBLE,
'prefix' => Schema::TYPE_TEXT,
'message' => Schema::TYPE_TEXT,
], $tableOptions);
$this->createTable($target->logTable, [
'id' => $this->bigPrimaryKey(),
'level' => $this->integer(),
'category' => $this->string(),
'log_time' => $this->double(),
'prefix' => $this->text(),
'message' => $this->text(),
], $tableOptions);
$this->createIndex('idx_log_level', $target->logTable, 'level');
$this->createIndex('idx_log_category', $target->logTable, 'category');
} catch (\yii\db\Exception $ex) {
}
$this->createIndex('idx_log_level', $target->logTable, 'level');
$this->createIndex('idx_log_category', $target->logTable, 'category');
}
$this->dropTable("logging");
}
public function down()
{
$targets = $this->getDbTargets();
foreach ($targets as $target) {
foreach ($this->getDbTargets() as $target) {
$this->db = $target->db;
$this->dropTable($target->logTable);
}
}
}