From b0623fe8046113982c91697de41e3930e3d0b17b Mon Sep 17 00:00:00 2001 From: Richard Griffith Date: Thu, 9 Nov 2023 21:42:20 -0600 Subject: [PATCH] Fix issues with upd-2.0.18-to-2.3.0 step MySQL changes removing the 'IGNORE' options on INSERT statements in versions 5.7 and above caused upgrade of block_module_link table to fail. --- upgrade/upd-2.0.18-to-2.3.0/index.php | 56 ++++++++++++++++----------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/upgrade/upd-2.0.18-to-2.3.0/index.php b/upgrade/upd-2.0.18-to-2.3.0/index.php index 90845a6c0..bc7b3f5a6 100644 --- a/upgrade/upd-2.0.18-to-2.3.0/index.php +++ b/upgrade/upd-2.0.18-to-2.3.0/index.php @@ -9,6 +9,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ +use Xmf\Database\Tables; + /** * Upgrader from 2.0.18 to 2.3.0 * @@ -67,7 +69,8 @@ public function check_cache() return false; } - return $GLOBALS['xoopsDB']->getRowsNum($result) > 0; + $temp = $GLOBALS['xoopsDB']->getRowsNum($result) > 0; + return $temp; /* $sql = "SELECT COUNT(*) FROM `" . $GLOBALS['xoopsDB']->prefix('cache_model') . "`"; @@ -106,29 +109,38 @@ public function check_bmlink() */ public function apply_bmlink() { - $sql = 'SHOW KEYS FROM `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . '`'; - $result = $GLOBALS['xoopsDB']->queryF($sql); - if (!$GLOBALS['xoopsDB']->isResultSet($result)) { - return false; + $tableName = 'block_module_link'; + $tableNameOld = $tableName . '_old'; + + $tables = new Tables(); + + $tables->useTable($tableName); + $tables->renameTable($tableName, $tableNameOld); + $result = $tables->executeQueue(true); + if (true!==$result) { + throw new \RuntimeException( + __METHOD__ . ' failed.', E_USER_ERROR + ); } - $keys_drop = array(); - $primary_add = true; - while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { - if ($row['Key_name'] === 'PRIMARY') { - $primary_add = false; - } - if (in_array($row['Key_name'], array('block_id', 'module_id'))) { - $keys_drop[] = $row['Key_name']; - } - } - foreach ($keys_drop as $drop) { - $sql = 'ALTER TABLE `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . "` DROP KEY `{$drop}`"; - $GLOBALS['xoopsDB']->queryF($sql); + $tables->resetQueue(); + $tables->addTable($tableName); + $tables->addColumn($tableName, 'block_id', 'int'); + $tables->addColumn($tableName, 'module_id', 'int'); + $tables->addPrimaryKey($tableName, 'block_id, module_id'); + $result = $tables->executeQueue(true); + if (true!==$result) { + throw new \RuntimeException( + __METHOD__ . ' failed.', E_USER_ERROR + ); } - if ($primary_add) { - $sql = 'ALTER IGNORE TABLE `' . $GLOBALS['xoopsDB']->prefix('block_module_link') . '` ADD PRIMARY KEY (`block_id`, `module_id`)'; - - return $GLOBALS['xoopsDB']->queryF($sql); + $prefixedName = $GLOBALS['xoopsDB']->prefix('block_module_link'); + $sql = 'INSERT INTO `' . $prefixedName . '` (`block_id`, `module_id`) ' . + 'SELECT DISTINCT `block_id`, `module_id` FROM `' . $prefixedName . '_old`'; + $result = $GLOBALS['xoopsDB']->queryF($sql); + if (true!==$result) { + throw new \RuntimeException( + __METHOD__ . ' failed.', E_USER_ERROR + ); } return true;