From c608c7f9a7de59931072bca194dac4faabe33ba7 Mon Sep 17 00:00:00 2001 From: shirase Date: Fri, 21 Apr 2017 01:31:23 +0300 Subject: [PATCH 1/7] CHANGELOG.md --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index f5e77c3b780..361c43dbaa4 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -6,6 +6,7 @@ Yii Framework 2 Change Log - Bug #13362: Fixed return value of `yii\caching\MemCache::setValues()` (masterklavi) - Enh #13963: Added tests for yii\behaviors\TimestampBehavior (vladis84) +- Bug #13890: DbTarget log transaction bug (shirase) - Enh #13820: Add new HTTP status code 451 (yyxx9988) - Bug #13671: Fixed error handler trace to work correctly with XDebug (samdark) - Bug #13657: Fixed `yii\helpers\StringHelper::truncateHtml()` skip extra tags at the end (sam002) From 14a5b83fe4cbe3363bc2a54f21466ccdf81315f7 Mon Sep 17 00:00:00 2001 From: shirase Date: Fri, 21 Apr 2017 01:32:31 +0300 Subject: [PATCH 2/7] testTransactionRollBack --- tests/framework/log/DbTargetTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/framework/log/DbTargetTest.php b/tests/framework/log/DbTargetTest.php index efe0353d40e..06e8a9cf277 100644 --- a/tests/framework/log/DbTargetTest.php +++ b/tests/framework/log/DbTargetTest.php @@ -140,4 +140,29 @@ public function testTimestamp() $loggedTime = $query->createCommand(self::getConnection())->queryScalar(); static::assertEquals($time, $loggedTime); } + + public function testTransactionRollBack() + { + $db = $this->getConnection(); + $logger = Yii::getLogger(); + + $tx = $db->beginTransaction(); + + $messsageData = [ + 'test', + Logger::LEVEL_WARNING, + 'test', + time(), + [] + ]; + + $logger->messages[] = $messsageData; + $logger->flush(true); + + $tx->rollBack(); + + $query = (new Query())->select('COUNT(*)')->from(self::$logTable)->where(['category' => 'test', 'name' => 'test']); + $count = $query->createCommand(self::getConnection())->queryScalar(); + static::assertEquals(1, $count); + } } \ No newline at end of file From ec9fde2c34d5e8ae5b11c5af4e53d431a7c3ea4c Mon Sep 17 00:00:00 2001 From: shirase Date: Fri, 21 Apr 2017 01:33:37 +0300 Subject: [PATCH 3/7] fix DbTarget log transaction bug --- framework/log/DbTarget.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/framework/log/DbTarget.php b/framework/log/DbTarget.php index d77f6e9369a..503360841f2 100644 --- a/framework/log/DbTarget.php +++ b/framework/log/DbTarget.php @@ -60,6 +60,12 @@ public function init() */ public function export() { + if ($tx = $this->db->getTransaction()) { + $this->db = clone($this->db); + $this->db->pdo = null; + $this->db->open(); + } + $tableName = $this->db->quoteTableName($this->logTable); $sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[prefix]], [[message]]) VALUES (:level, :category, :log_time, :prefix, :message)"; From a1235785e07536b0869301cf8fe10844fc88e0e1 Mon Sep 17 00:00:00 2001 From: shirase Date: Fri, 21 Apr 2017 01:45:29 +0300 Subject: [PATCH 4/7] code climate --- framework/log/DbTarget.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/log/DbTarget.php b/framework/log/DbTarget.php index 503360841f2..d4fab50208c 100644 --- a/framework/log/DbTarget.php +++ b/framework/log/DbTarget.php @@ -60,7 +60,7 @@ public function init() */ public function export() { - if ($tx = $this->db->getTransaction()) { + if ($this->db->getTransaction()) { $this->db = clone($this->db); $this->db->pdo = null; $this->db->open(); From 74b50a4f73071911b2164588ae6259c36e5f6676 Mon Sep 17 00:00:00 2001 From: shirase Date: Fri, 21 Apr 2017 01:49:08 +0300 Subject: [PATCH 5/7] fix test --- tests/framework/log/DbTargetTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/framework/log/DbTargetTest.php b/tests/framework/log/DbTargetTest.php index 06e8a9cf277..59e86782d69 100644 --- a/tests/framework/log/DbTargetTest.php +++ b/tests/framework/log/DbTargetTest.php @@ -161,7 +161,7 @@ public function testTransactionRollBack() $tx->rollBack(); - $query = (new Query())->select('COUNT(*)')->from(self::$logTable)->where(['category' => 'test', 'name' => 'test']); + $query = (new Query())->select('COUNT(*)')->from(self::$logTable)->where(['category' => 'test', 'message' => 'test']); $count = $query->createCommand(self::getConnection())->queryScalar(); static::assertEquals(1, $count); } From bc89c183ed8306c8e5e4c35680ace9f951b09b24 Mon Sep 17 00:00:00 2001 From: shirase Date: Fri, 21 Apr 2017 01:52:19 +0300 Subject: [PATCH 6/7] inspect code --- tests/framework/log/DbTargetTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/framework/log/DbTargetTest.php b/tests/framework/log/DbTargetTest.php index 59e86782d69..812bf98ea4b 100644 --- a/tests/framework/log/DbTargetTest.php +++ b/tests/framework/log/DbTargetTest.php @@ -143,7 +143,7 @@ public function testTimestamp() public function testTransactionRollBack() { - $db = $this->getConnection(); + $db = self::getConnection(); $logger = Yii::getLogger(); $tx = $db->beginTransaction(); @@ -162,7 +162,7 @@ public function testTransactionRollBack() $tx->rollBack(); $query = (new Query())->select('COUNT(*)')->from(self::$logTable)->where(['category' => 'test', 'message' => 'test']); - $count = $query->createCommand(self::getConnection())->queryScalar(); + $count = $query->createCommand($db)->queryScalar(); static::assertEquals(1, $count); } } \ No newline at end of file From 44d65220d40c6edc4cbedbd3c23ff77b04b5a593 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Thu, 4 May 2017 21:28:53 +0300 Subject: [PATCH 7/7] Use clone keyword as stated in PHP manual [skip ci] --- framework/log/DbTarget.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/log/DbTarget.php b/framework/log/DbTarget.php index d4fab50208c..922a9cd7870 100644 --- a/framework/log/DbTarget.php +++ b/framework/log/DbTarget.php @@ -61,7 +61,7 @@ public function init() public function export() { if ($this->db->getTransaction()) { - $this->db = clone($this->db); + $this->db = clone $this->db; $this->db->pdo = null; $this->db->open(); }