From 87784ffc89a40c852d7c567f788b5a209c23b3a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20H=C3=BCnig?= Date: Mon, 28 Jun 2021 23:29:52 +0200 Subject: [PATCH] backport showTableStatus from Magento2 At the moment, Magento2 has a special code path inside showTableStatus() when MySQL8 is used. This is needed, to flush information_schema_stats Cache introuded in MySQL 8. As some modules, are using showTableStatus to recieve autoinvrement ID, this ones will break with MySQL 8 when information_schema_stats_expiry is > 0. This PR will allow the use of showTableStatus without the need to disable status information_schema_stats Cache. --- lib/Varien/Db/Adapter/Pdo/Mysql.php | 37 +++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/Varien/Db/Adapter/Pdo/Mysql.php b/lib/Varien/Db/Adapter/Pdo/Mysql.php index 5a5ae13dff7..109b1ad13bf 100644 --- a/lib/Varien/Db/Adapter/Pdo/Mysql.php +++ b/lib/Varien/Db/Adapter/Pdo/Mysql.php @@ -221,6 +221,14 @@ class Varien_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql implements V * @var array|null */ protected $_queryHook = null; + + /** + * Save if mysql engine is 8 or not. + * + * @var bool + */ + private $isMysql8Engine; + /** * Begin new DB transaction for connection @@ -1057,6 +1065,22 @@ public function modifyColumn($tableName, $columnName, $definition, $flushData = return $this; } + + /** + * Checks if the engine is mysql 8 + * + * @return bool + */ + private function isMysql8EngineUsed(): bool + { + if (!$this->isMysql8Engine) { + $version = $this->fetchPairs("SHOW variables LIKE 'version'")['version']; + $this->isMysql8Engine = (bool) preg_match('/^(8\.)/', $version); + } + + return $this->isMysql8Engine; + } + /** * Show table status @@ -1071,9 +1095,18 @@ public function showTableStatus($tableName, $schemaName = null) if ($schemaName !== null) { $fromDbName = ' FROM ' . $this->quoteIdentifier($schemaName); } - $query = sprintf('SHOW TABLE STATUS%s LIKE %s', $fromDbName, $this->quote($tableName)); + $query = sprintf('SHOW TABLE STATUS%s LIKE %s', $fromDbName, $this->quote($tableName)); + //checks which slq engine used + if (!$this->isMysql8EngineUsed()) { + //if it's not MySQl-8 we just fetch results + return $this->rawFetchRow($query); + } + // Run show table status query in different connection because DDL queries do it in transaction, + // and we don't have actual table statistic in this case + $connection = $this->_transactionLevel ? $this->createConnection() : $this; + $connection->query(sprintf('ANALYZE TABLE %s', $this->quoteIdentifier($tableName))); - return $this->raw_fetchRow($query); + return $connection->query($query)->fetch(\PDO::FETCH_ASSOC); } /**