From 52bae7a01ee438132a47c4872cc3305bb57b763e Mon Sep 17 00:00:00 2001 From: morgan Date: Mon, 28 Oct 2024 11:55:04 +0100 Subject: [PATCH] Create DbWrapper class, responsible for calls to the core DB class --- classes/DbWrapper.php | 149 ++++++++++++++++++ .../Exceptions/UpdateDatabaseException.php | 34 ++++ .../CoreUpgrader/CoreUpgrader.php | 32 ++-- upgrade/php/add_column.php | 17 +- upgrade/php/add_hook.php | 13 +- ...ing_unique_key_from_authorization_role.php | 25 ++- upgrade/php/add_new_status_stock.php | 32 ++-- upgrade/php/add_new_tab.php | 29 ++-- upgrade/php/copy_tab_rights.php | 12 +- upgrade/php/deactivate_custom_modules.php | 28 ++-- upgrade/php/drop_column_if_exists.php | 15 +- upgrade/php/execute_sql_if_table_exists.php | 15 +- ...30_add_quick_access_evaluation_catalog.php | 17 +- ...om_store_to_store_lang_and_clean_store.php | 13 +- upgrade/php/ps_1740_update_module_tabs.php | 6 +- upgrade/php/ps_1750_update_module_tabs.php | 18 ++- upgrade/php/ps_1751_update_module_sf_tab.php | 6 +- ...py_data_from_currency_to_currency_lang.php | 15 +- upgrade/php/ps_1760_update_tabs.php | 6 +- upgrade/php/ps_1761_update_currencies.php | 5 +- upgrade/php/ps_1763_update_tabs.php | 12 +- upgrade/php/ps_1770_preset_tab_enabled.php | 10 +- upgrade/php/ps_1770_update_charset.php | 31 ++-- .../ps_1770_update_order_status_colors.php | 7 +- upgrade/php/ps_1771_update_customer_note.php | 15 +- upgrade/php/ps_1780_add_feature_flag_tab.php | 15 +- upgrade/php/ps_800_add_security_tab.php | 13 +- ..._810_update_product_page_feature_flags.php | 17 +- upgrade/php/ps_811_update_redirect_type.php | 21 ++- upgrade/php/ps_remove_controller_tab.php | 51 ++++-- upgrade/php/ps_update_tab_lang.php | 8 +- upgrade/php/ps_update_tabs.php | 21 ++- upgrade/php/rename_tab.php | 15 +- upgrade/php/update_null_values.php | 12 +- 34 files changed, 579 insertions(+), 156 deletions(-) create mode 100644 classes/DbWrapper.php create mode 100644 classes/Exceptions/UpdateDatabaseException.php diff --git a/classes/DbWrapper.php b/classes/DbWrapper.php new file mode 100644 index 000000000..2e140d716 --- /dev/null +++ b/classes/DbWrapper.php @@ -0,0 +1,149 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ + +namespace PrestaShop\Module\AutoUpgrade; + +use Db; +use mysqli_result; +use PDOStatement; +use PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException; + +class DbWrapper +{ + /** + * @param array $data + * + * @throws UpdateDatabaseException + */ + public static function update(string $table, array $data, string $where = '', int $limit = 0, bool $null_values = false, bool $use_cache = true, bool $add_prefix = true): bool + { + $result = Db::getInstance()->update($table, $data, $where, $limit, $null_values, $use_cache, $add_prefix); + self::validateDBQuerySuccess(); + + return $result; + } + + /** + * @throws UpdateDatabaseException + */ + public static function execute(string $sql, bool $use_cache = true): bool + { + $result = Db::getInstance()->execute($sql, $use_cache); + self::validateDBQuerySuccess(); + + return $result; + } + + /** + * @throws UpdateDatabaseException + * + * @return array|bool|mysqli_result|PDOStatement|resource|null + */ + public static function executeS(string $sql, bool $array = true, bool $use_cache = true) + { + $result = Db::getInstance()->executeS($sql, $array, $use_cache); + self::validateDBQuerySuccess(); + + return $result; + } + + /** + * @throws UpdateDatabaseException + * + * @return string|false|null Returns false if no results + */ + public static function getValue(string $sql, bool $use_cache = true) + { + $result = Db::getInstance()->getValue($sql, $use_cache); + self::validateDBQuerySuccess(); + + return $result; + } + + /** + * @param array $data + * + * @throws UpdateDatabaseException + */ + public static function insert(string $table, array $data, bool $null_values = false, bool $use_cache = true, int $type = Db::INSERT, bool $add_prefix = true): bool + { + $result = Db::getInstance()->insert($table, $data, $null_values, $use_cache, $type, $add_prefix); + self::validateDBQuerySuccess(); + + return $result; + } + + /** + * @return int|string + * + * @throws UpdateDatabaseException + */ + public static function Insert_ID() + { + $result = Db::getInstance()->Insert_ID(); + self::validateDBQuerySuccess(); + + return $result; + } + + /** + * @throws UpdateDatabaseException + */ + public static function delete(string $table, string $where = '', int $limit = 0, bool $use_cache = true, bool $add_prefix = true): bool + { + $result = Db::getInstance()->delete($table, $where, $limit, $use_cache, $add_prefix); + self::validateDBQuerySuccess(); + + return $result; + } + + /** + * @throws UpdateDatabaseException + * + * @return array|bool|object|null + */ + public static function getRow(string $sql, bool $use_cache = true) + { + $result = Db::getInstance()->getRow($sql, $use_cache); + self::validateDBQuerySuccess(); + + return $result; + } + + /** + * @throws UpdateDatabaseException + */ + public static function validateDBQuerySuccess(): void + { + $previousQueryError = Db::getInstance()->getMsgError(); + $previousQueryErrorCode = Db::getInstance()->getNumberError(); + + if (!empty($previousQueryError) || !empty($previousQueryErrorCode)) { + throw new UpdateDatabaseException($previousQueryError, $previousQueryErrorCode); + } + } +} diff --git a/classes/Exceptions/UpdateDatabaseException.php b/classes/Exceptions/UpdateDatabaseException.php new file mode 100644 index 000000000..bcad9d806 --- /dev/null +++ b/classes/Exceptions/UpdateDatabaseException.php @@ -0,0 +1,34 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ + +namespace PrestaShop\Module\AutoUpgrade\Exceptions; + +use Exception; + +class UpdateDatabaseException extends Exception +{ +} diff --git a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php index 4f641f85b..386c8c911 100644 --- a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php +++ b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php @@ -32,6 +32,7 @@ use InvalidArgumentException; use Language; use ParseError; +use PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException; use PrestaShop\Module\AutoUpgrade\Exceptions\UpgradeException; use PrestaShop\Module\AutoUpgrade\Log\LoggerInterface; use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration; @@ -430,7 +431,11 @@ protected function runPhpQuery(string $upgrade_file, string $query): void } require_once $pathToPhpDirectory . strtolower($func_name) . '.php'; - $phpRes = call_user_func_array($func_name, $parameters); + try { + $phpRes = call_user_func_array($func_name, $parameters); + } catch (UpdateDatabaseException $exception) { + $this->logPhpError($upgrade_file, $query, $exception->getMessage()); + } } // Or an object method else { @@ -440,7 +445,9 @@ protected function runPhpQuery(string $upgrade_file, string $query): void } if ($this->hasPhpError($phpRes)) { - $this->logPhpError($upgrade_file, $query, $phpRes); + $message = (empty($phpRes['error']) ? '' : $phpRes['error'] . "\n") . + (empty($phpRes['msg']) ? '' : ' - ' . $phpRes['msg'] . "\n"); + $this->logPhpError($upgrade_file, $query, $message); } else { $this->logger->debug('Migration file: ' . $upgrade_file . ', Query: ' . $query); } @@ -456,32 +463,21 @@ private function hasPhpError($phpRes): bool return isset($phpRes) && (is_array($phpRes) && !empty($phpRes['error'])) || $phpRes === false; } - /** - * @param string $upgrade_file - * @param string $query - * @param mixed $phpRes - * - * @return void - */ - private function logPhpError(string $upgrade_file, string $query, $phpRes): void + private function logPhpError(string $upgrade_file, string $query, string $message): void { - $this->logger->error( - '[ERROR] PHP ' . $upgrade_file . ' ' . $query . "\n" . - (empty($phpRes['error']) ? '' : $phpRes['error'] . "\n") . - (empty($phpRes['msg']) ? '' : ' - ' . $phpRes['msg'] . "\n") - ); + $this->logger->error('PHP ' . $upgrade_file . ' ' . $query . ": \n" . $message); $this->container->getState()->setWarningExists(true); } private function logMissingFileError(string $path, string $func_name, string $query): void { - $this->logger->error('[ERROR] ' . $path . strtolower($func_name) . ' PHP - missing file ' . $query); + $this->logger->error($path . strtolower($func_name) . ' PHP - missing file ' . $query); $this->container->getState()->setWarningExists(true); } private function logForbiddenObjectMethodError(string $phpString, string $upgrade_file): void { - $this->logger->error('[ERROR] ' . $upgrade_file . ' PHP - Object Method call is forbidden (' . $phpString . ')'); + $this->logger->error($upgrade_file . ' PHP - Object Method call is forbidden (' . $phpString . ')'); $this->container->getState()->setWarningExists(true); } @@ -821,7 +817,7 @@ protected function updateRTLFiles(): void $commandBus->handle($adaptThemeToTRLLanguages); } catch (CoreException $e) { $this->logger->error(' - [ERROR] PHP Impossible to generate RTL files for theme' . $theme['name'] . "\n" . + PHP Impossible to generate RTL files for theme' . $theme['name'] . "\n" . $e->getMessage() ); diff --git a/upgrade/php/add_column.php b/upgrade/php/add_column.php index 5347208ed..c39fb6ec0 100644 --- a/upgrade/php/add_column.php +++ b/upgrade/php/add_column.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function add_column($table, $column, $parameters) { - $column_exists = Db::getInstance()->executeS('SHOW COLUMNS FROM `' . _DB_PREFIX_ . $table . "` WHERE Field = '" . $column . "'"); + $column_exists = DbWrapper::executeS('SHOW COLUMNS FROM `' . _DB_PREFIX_ . $table . "` WHERE Field = '" . $column . "'"); if (!empty($column_exists)) { // If it already exists, we will modify the structure - Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . $table . '` CHANGE `' . $column . '` `' . $column . '` ' . $parameters); + DbWrapper::execute('ALTER TABLE `' . _DB_PREFIX_ . $table . '` CHANGE `' . $column . '` `' . $column . '` ' . $parameters); } else { // Otherwise, we add it - Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . $table . '` ADD `' . $column . '` ' . $parameters); + DbWrapper::execute('ALTER TABLE `' . _DB_PREFIX_ . $table . '` ADD `' . $column . '` ' . $parameters); } } diff --git a/upgrade/php/add_hook.php b/upgrade/php/add_hook.php index fc588cb6a..d3ecb3f75 100644 --- a/upgrade/php/add_hook.php +++ b/upgrade/php/add_hook.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return bool + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function add_hook($hook, $title, $description, $position = 1) { - return (bool) Db::getInstance()->execute(' + return (bool) DbWrapper::execute(' INSERT INTO `' . _DB_PREFIX_ . 'hook` (`name`, `title`, `description`, `position`) VALUES ("' . pSQL($hook) . '", "' . pSQL($title) . '", "' . pSQL($description) . '", ' . (int) $position . ') ON DUPLICATE KEY UPDATE `title` = VALUES(`title`), `description` = VALUES(`description`) diff --git a/upgrade/php/add_missing_unique_key_from_authorization_role.php b/upgrade/php/add_missing_unique_key_from_authorization_role.php index e2d6507c7..a1d679802 100644 --- a/upgrade/php/add_missing_unique_key_from_authorization_role.php +++ b/upgrade/php/add_missing_unique_key_from_authorization_role.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors */ -// Allows you to catch up on a forgotten uniqueness constraint on the roles +/** + * Allows you to catch up on a forgotten uniqueness constraint on the roles + * + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException + */ function add_missing_unique_key_from_authorization_role() { // Verify if we need to create unique key - $keys = Db::getInstance()->executeS( + $keys = DbWrapper::executeS( 'SHOW KEYS FROM ' . _DB_PREFIX_ . "authorization_role WHERE Key_name='slug'" ); @@ -37,7 +46,7 @@ function add_missing_unique_key_from_authorization_role() } // We recover the duplicates that we want to keep - $duplicates = Db::getInstance()->executeS( + $duplicates = DbWrapper::executeS( 'SELECT MIN(id_authorization_role) AS keep_ID, slug FROM ' . _DB_PREFIX_ . 'authorization_role GROUP BY slug HAVING COUNT(*) > 1' ); @@ -47,21 +56,21 @@ function add_missing_unique_key_from_authorization_role() foreach ($duplicates as $duplicate) { // We recover the duplicates that we want to remove - $elementsToRemoves = Db::getInstance()->executeS( + $elementsToRemoves = DbWrapper::executeS( 'SELECT id_authorization_role FROM ' . _DB_PREFIX_ . "authorization_role WHERE slug = '" . $duplicate['slug'] . "' AND id_authorization_role != " . $duplicate['keep_ID'] ); foreach ($elementsToRemoves as $elementToRemove) { // We update the access table which may have used a duplicate role - Db::getInstance()->execute( + DbWrapper::execute( 'UPDATE ' . _DB_PREFIX_ . "access SET id_authorization_role = '" . $duplicate['keep_ID'] . "' WHERE id_authorization_role = " . $elementToRemove['id_authorization_role'] ); // We remove the role - Db::getInstance()->delete('authorization_role', '`id_authorization_role` = ' . (int) $elementToRemove['id_authorization_role']); + DbWrapper::delete('authorization_role', '`id_authorization_role` = ' . (int) $elementToRemove['id_authorization_role']); } } - Db::getInstance()->execute( + DbWrapper::execute( 'ALTER TABLE ' . _DB_PREFIX_ . 'authorization_role ADD UNIQUE KEY `slug` (`slug`)' ); } diff --git a/upgrade/php/add_new_status_stock.php b/upgrade/php/add_new_status_stock.php index 8a5c19f2f..7b549b9ff 100644 --- a/upgrade/php/add_new_status_stock.php +++ b/upgrade/php/add_new_status_stock.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function add_new_status_stock() { @@ -30,18 +39,18 @@ function add_new_status_stock() $languages = Language::getLanguages(); // insert ps_tab AdminStockManagement - $count = (int) Db::getInstance()->getValue( + $count = (int) DbWrapper::getValue( 'SELECT count(id_tab) FROM `' . _DB_PREFIX_ . 'tab` WHERE `class_name` = \'AdminStockManagement\' AND `id_parent` = 9' ); if (!$count) { - Db::getInstance()->execute( + DbWrapper::execute( 'INSERT INTO `' . _DB_PREFIX_ . 'tab` (`id_tab`, `id_parent`, `position`, `module`, `class_name`, `active`, `hide_host_mode`, `icon`) VALUES (null, 9, 7, NULL, \'AdminStockManagement\', 1, 0, \'\')' ); - $lastIdTab = (int) Db::getInstance()->Insert_ID(); + $lastIdTab = (int) DbWrapper::Insert_ID(); // ps_tab_lang foreach ($languages as $lang) { @@ -54,7 +63,7 @@ function add_new_status_stock() $lang['locale'] ) ); - Db::getInstance()->execute( + DbWrapper::execute( 'INSERT INTO `' . _DB_PREFIX_ . 'tab_lang` (`id_tab`, `id_lang`, `name`) VALUES ( ' . $lastIdTab . ', @@ -96,14 +105,14 @@ function add_new_status_stock() } // ps_stock_mvt_reason - Db::getInstance()->execute( + DbWrapper::execute( 'INSERT INTO `' . _DB_PREFIX_ . 'stock_mvt_reason` (`sign`, `date_add`, `date_upd`, `deleted`) VALUES (' . $d['sign'] . ', NOW(), NOW(), "0")' ); // ps_configuration - $lastInsertedId = Db::getInstance()->Insert_ID(); - Db::getInstance()->execute( + $lastInsertedId = DbWrapper::Insert_ID(); + DbWrapper::execute( 'INSERT INTO `' . _DB_PREFIX_ . 'configuration` (`name`, `value`, `date_add`, `date_upd`) VALUES ("' . $d['conf'] . '", ' . (int) $lastInsertedId . ', NOW(), NOW())' ); @@ -118,7 +127,7 @@ function add_new_status_stock() $lang['locale'] ) ); - Db::getInstance()->execute( + DbWrapper::execute( 'INSERT INTO `' . _DB_PREFIX_ . 'stock_mvt_reason_lang` (`id_stock_mvt_reason`, `id_lang`, `name`) VALUES (' . (int) $lastInsertedId . ', ' . (int) $lang['id_lang'] . ', "' . $mvtName . '")' ); @@ -136,9 +145,12 @@ function add_new_status_stock() } } +/** + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException + */ function configuration_exists($confName) { - $count = (int) Db::getInstance()->getValue( + $count = (int) DbWrapper::getValue( 'SELECT count(id_configuration) FROM `' . _DB_PREFIX_ . 'configuration` WHERE `name` = \'' . $confName . '\'' diff --git a/upgrade/php/add_new_tab.php b/upgrade/php/add_new_tab.php index e5181b7f3..9ef4c0564 100644 --- a/upgrade/php/add_new_tab.php +++ b/upgrade/php/add_new_tab.php @@ -24,17 +24,20 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; use PrestaShopBundle\Security\Voter\PageVoter; /** * Common method to handle the tab registration * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException + * * @internal */ function register_tab($className, $name, $id_parent, $returnId = false, $parentTab = null, $module = '') { if (null !== $parentTab && !empty($parentTab) && strtolower(trim($parentTab)) !== 'null') { - $id_parent = (int) Db::getInstance()->getValue('SELECT `id_tab` FROM `' . _DB_PREFIX_ . 'tab` WHERE `class_name` = \'' . pSQL($parentTab) . '\''); + $id_parent = (int) DbWrapper::getValue('SELECT `id_tab` FROM `' . _DB_PREFIX_ . 'tab` WHERE `class_name` = \'' . pSQL($parentTab) . '\''); } $array = []; @@ -43,14 +46,14 @@ function register_tab($className, $name, $id_parent, $returnId = false, $parentT $array[$temp[0]] = $temp[1]; } - if (!(int) Db::getInstance()->getValue('SELECT count(id_tab) FROM `' . _DB_PREFIX_ . 'tab` WHERE `class_name` = \'' . pSQL($className) . '\' ')) { - Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'tab` (`id_parent`, `class_name`, `module`, `position`) VALUES (' . (int) $id_parent . ', \'' . pSQL($className) . '\', \'' . pSQL($module) . '\', + if (!(int) DbWrapper::getValue('SELECT count(id_tab) FROM `' . _DB_PREFIX_ . 'tab` WHERE `class_name` = \'' . pSQL($className) . '\' ')) { + DbWrapper::execute('INSERT INTO `' . _DB_PREFIX_ . 'tab` (`id_parent`, `class_name`, `module`, `position`) VALUES (' . (int) $id_parent . ', \'' . pSQL($className) . '\', \'' . pSQL($module) . '\', (SELECT IFNULL(MAX(t.position),0)+ 1 FROM `' . _DB_PREFIX_ . 'tab` t WHERE t.id_parent = ' . (int) $id_parent . '))'); } - $languages = Db::getInstance()->executeS('SELECT id_lang, iso_code FROM `' . _DB_PREFIX_ . 'lang`'); + $languages = DbWrapper::executeS('SELECT id_lang, iso_code FROM `' . _DB_PREFIX_ . 'lang`'); foreach ($languages as $lang) { - Db::getInstance()->execute(' + DbWrapper::execute(' INSERT IGNORE INTO `' . _DB_PREFIX_ . 'tab_lang` (`id_lang`, `id_tab`, `name`) VALUES (' . (int) $lang['id_lang'] . ', ( SELECT `id_tab` @@ -64,12 +67,14 @@ function register_tab($className, $name, $id_parent, $returnId = false, $parentT /** * Common method for getting the new tab ID * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException + * * @internal */ function get_new_tab_id($className, $returnId = false) { if ($returnId && strtolower(trim($returnId)) !== 'false') { - return (int) Db::getInstance()->getValue('SELECT `id_tab` + return (int) DbWrapper::getValue('SELECT `id_tab` FROM `' . _DB_PREFIX_ . 'tab` WHERE `class_name` = \'' . pSQL($className) . '\''); } @@ -86,6 +91,8 @@ function get_new_tab_id($className, $returnId = false) * @param string $module * * @return int|null Tab id if requested + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function add_new_tab_17($className, $name, $id_parent, $returnId = false, $parentTab = null, $module = '') { @@ -94,7 +101,7 @@ function add_new_tab_17($className, $name, $id_parent, $returnId = false, $paren // Preliminary - Get Parent class name for slug generation $parentClassName = null; if ($id_parent) { - $parentClassName = Db::getInstance()->getValue(' + $parentClassName = DbWrapper::getValue(' SELECT `class_name` FROM `' . _DB_PREFIX_ . 'tab` WHERE `id_tab` = "' . (int) $id_parent . '" @@ -106,11 +113,11 @@ function add_new_tab_17($className, $name, $id_parent, $returnId = false, $paren foreach ([PageVoter::CREATE, PageVoter::READ, PageVoter::UPDATE, PageVoter::DELETE] as $role) { // 1- Add role $roleToAdd = strtoupper('ROLE_MOD_TAB_' . $className . '_' . $role); - Db::getInstance()->execute('INSERT IGNORE INTO `' . _DB_PREFIX_ . 'authorization_role` (`slug`) + DbWrapper::execute('INSERT IGNORE INTO `' . _DB_PREFIX_ . 'authorization_role` (`slug`) VALUES ("' . pSQL($roleToAdd) . '")'); - $newID = Db::getInstance()->Insert_ID(); + $newID = DbWrapper::Insert_ID(); if (!$newID) { - $newID = Db::getInstance()->getValue(' + $newID = DbWrapper::getValue(' SELECT `id_authorization_role` FROM `' . _DB_PREFIX_ . 'authorization_role` WHERE `slug` = "' . pSQL($roleToAdd) . '" @@ -120,7 +127,7 @@ function add_new_tab_17($className, $name, $id_parent, $returnId = false, $paren // 2- Copy access from the parent if (!empty($parentClassName) && !empty($newID)) { $parentRole = strtoupper('ROLE_MOD_TAB_' . pSQL($parentClassName) . '_' . $role); - Db::getInstance()->execute( + DbWrapper::execute( 'INSERT IGNORE INTO `' . _DB_PREFIX_ . 'access` (`id_profile`, `id_authorization_role`) SELECT a.`id_profile`, ' . (int) $newID . ' as `id_authorization_role` FROM `' . _DB_PREFIX_ . 'access` a join `' . _DB_PREFIX_ . 'authorization_role` ar on a.`id_authorization_role` = ar.`id_authorization_role` diff --git a/upgrade/php/copy_tab_rights.php b/upgrade/php/copy_tab_rights.php index c218818a1..e93d1dc32 100644 --- a/upgrade/php/copy_tab_rights.php +++ b/upgrade/php/copy_tab_rights.php @@ -24,8 +24,12 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; use PrestaShopBundle\Security\Voter\PageVoter; +/** + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException + */ function copy_tab_rights($fromTabName, $toTabName) { if (empty($fromTabName) || empty($toTabName)) { @@ -34,11 +38,11 @@ function copy_tab_rights($fromTabName, $toTabName) foreach ([PageVoter::CREATE, PageVoter::READ, PageVoter::UPDATE, PageVoter::DELETE] as $role) { // 1- Add role $roleToAdd = strtoupper('ROLE_MOD_TAB_' . $toTabName . '_' . $role); - Db::getInstance()->execute('INSERT IGNORE INTO `' . _DB_PREFIX_ . 'authorization_role` (`slug`) + DbWrapper::execute('INSERT IGNORE INTO `' . _DB_PREFIX_ . 'authorization_role` (`slug`) VALUES ("' . pSQL($roleToAdd) . '")'); - $newID = Db::getInstance()->Insert_ID(); + $newID = DbWrapper::Insert_ID(); if (!$newID) { - $newID = Db::getInstance()->getValue(' + $newID = DbWrapper::getValue(' SELECT `id_authorization_role` FROM `' . _DB_PREFIX_ . 'authorization_role` WHERE `slug` = "' . pSQL($roleToAdd) . '" @@ -48,7 +52,7 @@ function copy_tab_rights($fromTabName, $toTabName) // 2- Copy access if (!empty($newID)) { $parentRole = strtoupper('ROLE_MOD_TAB_' . pSQL($fromTabName) . '_' . $role); - Db::getInstance()->execute( + DbWrapper::execute( 'INSERT IGNORE INTO `' . _DB_PREFIX_ . 'access` (`id_profile`, `id_authorization_role`) SELECT a.`id_profile`, ' . (int) $newID . ' as `id_authorization_role` FROM `' . _DB_PREFIX_ . 'access` a join `' . _DB_PREFIX_ . 'authorization_role` ar on a.`id_authorization_role` = ar.`id_authorization_role` diff --git a/upgrade/php/deactivate_custom_modules.php b/upgrade/php/deactivate_custom_modules.php index cc598f578..247ad867a 100644 --- a/upgrade/php/deactivate_custom_modules.php +++ b/upgrade/php/deactivate_custom_modules.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return bool + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function deactivate_custom_modules(): bool { - $db = Db::getInstance(); $modules = scandir(_PS_MODULE_DIR_, SCANDIR_SORT_NONE); foreach ($modules as $name) { if (!in_array($name, ['.', '..', 'index.php', '.htaccess']) && @is_dir(_PS_MODULE_DIR_ . $name . DIRECTORY_SEPARATOR) && @file_exists(_PS_MODULE_DIR_ . $name . DIRECTORY_SEPARATOR . $name . '.php')) { @@ -61,7 +69,7 @@ function deactivate_custom_modules(): bool } $arrNonNative = []; if ($arrNativeModules) { - $arrNonNative = $db->executeS(' + $arrNonNative = DbWrapper::executeS(' SELECT * FROM `' . _DB_PREFIX_ . 'module` m WHERE name NOT IN (' . implode(',', $arrNativeModules) . ') '); @@ -82,12 +90,12 @@ function deactivate_custom_modules(): bool $uninstallMe[$k] = '"' . pSQL($v) . '"'; } - $return = Db::getInstance()->execute(' + $return = DbWrapper::execute(' UPDATE `' . _DB_PREFIX_ . 'module` SET `active` = 0 WHERE `name` IN (' . implode(',', $uninstallMe) . ')'); - if (count(Db::getInstance()->executeS('SHOW TABLES LIKE \'' . _DB_PREFIX_ . 'module_shop\'')) > 0) { + if (count(DbWrapper::executeS('SHOW TABLES LIKE \'' . _DB_PREFIX_ . 'module_shop\'')) > 0) { foreach ($uninstallMe as $k => $uninstall) { - $return &= Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'module_shop` WHERE `id_module` = ' . (int) $k); + $return &= DbWrapper::execute('DELETE FROM `' . _DB_PREFIX_ . 'module_shop` WHERE `id_module` = ' . (int) $k); } } @@ -96,16 +104,18 @@ function deactivate_custom_modules(): bool /** * @param mixed $moduleRepository + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function deactivate_custom_modules80($moduleRepository): bool { $nonNativeModulesList = $moduleRepository->getNonNativeModules(); - $return = Db::getInstance()->execute( + $return = DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'module` SET `active` = 0 WHERE `name` IN (' . implode(',', array_map('add_quotes', $nonNativeModulesList)) . ')' ); - $nonNativeModules = \Db::getInstance()->executeS(' + $nonNativeModules = DbWrapper::executeS(' SELECT * FROM `' . _DB_PREFIX_ . 'module` m WHERE name IN (' . implode(',', array_map('add_quotes', $nonNativeModulesList)) . ') '); @@ -121,7 +131,7 @@ function deactivate_custom_modules80($moduleRepository): bool $sql = 'DELETE FROM `' . _DB_PREFIX_ . 'module_shop` WHERE `id_module` IN (' . implode(',', array_map('add_quotes', $toBeUninstalled)) . ') '; - return $return && Db::getInstance()->execute($sql); + return $return && DbWrapper::execute($sql); } function add_quotes(string $str): string diff --git a/upgrade/php/drop_column_if_exists.php b/upgrade/php/drop_column_if_exists.php index 59691ced6..ab2f0724d 100644 --- a/upgrade/php/drop_column_if_exists.php +++ b/upgrade/php/drop_column_if_exists.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function drop_column_if_exists($table, $column) { - $column_exists = Db::getInstance()->executeS('SHOW COLUMNS FROM `' . _DB_PREFIX_ . $table . "` WHERE Field = '" . $column . "'"); + $column_exists = DbWrapper::executeS('SHOW COLUMNS FROM `' . _DB_PREFIX_ . $table . "` WHERE Field = '" . $column . "'"); if (!empty($column_exists)) { - Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . $table . '` DROP COLUMN `' . $column . '`'); + DbWrapper::execute('ALTER TABLE `' . _DB_PREFIX_ . $table . '` DROP COLUMN `' . $column . '`'); } } diff --git a/upgrade/php/execute_sql_if_table_exists.php b/upgrade/php/execute_sql_if_table_exists.php index 0d01fa880..597c1ca1d 100644 --- a/upgrade/php/execute_sql_if_table_exists.php +++ b/upgrade/php/execute_sql_if_table_exists.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return bool + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function execute_sql_if_table_exists($table, $sqlQuery) { - if (empty(Db::getInstance()->executeS('SHOW TABLES LIKE "' . _DB_PREFIX_ . $table . '"'))) { + if (empty(DbWrapper::executeS('SHOW TABLES LIKE "' . _DB_PREFIX_ . $table . '"'))) { return true; } $sqlQuery = str_replace('PREFIX', _DB_PREFIX_, $sqlQuery); - return Db::getInstance()->execute($sqlQuery); + return DbWrapper::execute($sqlQuery); } diff --git a/upgrade/php/ps_1730_add_quick_access_evaluation_catalog.php b/upgrade/php/ps_1730_add_quick_access_evaluation_catalog.php index f93da61b4..e96ed1edc 100644 --- a/upgrade/php/ps_1730_add_quick_access_evaluation_catalog.php +++ b/upgrade/php/ps_1730_add_quick_access_evaluation_catalog.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1730_add_quick_access_evaluation_catalog() { @@ -33,12 +42,12 @@ function ps_1730_add_quick_access_evaluation_catalog() if ($isStatscheckupInstalled) { $translator = Context::getContext()->getTranslator(); - Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'quick_access` SET link = "index.php?controller=AdminStats&module=statscheckup" '); + DbWrapper::execute('INSERT INTO `' . _DB_PREFIX_ . 'quick_access` SET link = "index.php?controller=AdminStats&module=statscheckup" '); - $idQuickAccess = (int) Db::getInstance()->Insert_ID(); + $idQuickAccess = (int) DbWrapper::Insert_ID(); foreach (Language::getLanguages() as $language) { - Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'quick_access_lang` SET + DbWrapper::execute('INSERT INTO `' . _DB_PREFIX_ . 'quick_access_lang` SET `id_quick_access` = ' . $idQuickAccess . ', `id_lang` = ' . (int) $language['id_lang'] . ', `name` = "' . pSQL($translator->trans('Catalog evaluation', [], 'Admin.Navigation.Header')) . '" '); diff --git a/upgrade/php/ps_1730_migrate_data_from_store_to_store_lang_and_clean_store.php b/upgrade/php/ps_1730_migrate_data_from_store_to_store_lang_and_clean_store.php index a5b0f3ab5..fc51c2ff0 100755 --- a/upgrade/php/ps_1730_migrate_data_from_store_to_store_lang_and_clean_store.php +++ b/upgrade/php/ps_1730_migrate_data_from_store_to_store_lang_and_clean_store.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1730_migrate_data_from_store_to_store_lang_and_clean_store() { $langs = Language::getLanguages(); foreach ($langs as $lang) { - Db::getInstance()->execute( + DbWrapper::execute( 'INSERT INTO `' . _DB_PREFIX_ . 'store_lang` SELECT `id_store`, ' . $lang['id_lang'] . ' as id_lang , `name`, `address1`, `address2`, `hours`, `note` FROM `' . _DB_PREFIX_ . 'store`' diff --git a/upgrade/php/ps_1740_update_module_tabs.php b/upgrade/php/ps_1740_update_module_tabs.php index ab7b8eeee..4eb4f14b6 100644 --- a/upgrade/php/ps_1740_update_module_tabs.php +++ b/upgrade/php/ps_1740_update_module_tabs.php @@ -24,8 +24,12 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; + /** * File copied from ps_update_tabs.php and modified for only adding modules related tabs + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1740_update_module_tabs() { @@ -41,5 +45,5 @@ function ps_1740_update_module_tabs() add_new_tab_17($className, $translations, 0, false, 'AdminModulesSf'); } - Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'tab` SET `active`=1 WHERE `class_name` IN ("AdminModulesManage", "AdminModulesCatalog", "AdminModulesNotifications")'); + DbWrapper::execute('UPDATE `' . _DB_PREFIX_ . 'tab` SET `active`=1 WHERE `class_name` IN ("AdminModulesManage", "AdminModulesCatalog", "AdminModulesNotifications")'); } diff --git a/upgrade/php/ps_1750_update_module_tabs.php b/upgrade/php/ps_1750_update_module_tabs.php index d794da508..531db429c 100644 --- a/upgrade/php/ps_1750_update_module_tabs.php +++ b/upgrade/php/ps_1750_update_module_tabs.php @@ -24,8 +24,12 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; + /** * File copied from ps_update_tabs.php and modified for only adding modules related tabs + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1750_update_module_tabs() { @@ -44,14 +48,14 @@ function ps_1750_update_module_tabs() include_once 'add_new_tab.php'; foreach ($moduleTabsToBeAdded as $className => $tabDetails) { add_new_tab_17($className, $tabDetails['translations'], 0, false, $tabDetails['parent']); - Db::getInstance()->execute( + DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'tab` SET `active`= 1 WHERE `class_name` = "' . $className . '"' ); } // STEP 2: Rename module tabs (Notifications as Alerts, Module selection as Module Catalog, Module Catalog as Module Selections) include_once 'rename_tab.php'; - $adminModulesNotificationsTabId = Db::getInstance()->getValue( + $adminModulesNotificationsTabId = DbWrapper::getValue( 'SELECT id_tab FROM ' . _DB_PREFIX_ . 'tab WHERE class_name = "AdminModulesNotifications"' ); if (!empty($adminModulesNotificationsTabId)) { @@ -68,7 +72,7 @@ function ps_1750_update_module_tabs() ); } - $adminModulesCatalogTabId = Db::getInstance()->getValue( + $adminModulesCatalogTabId = DbWrapper::getValue( 'SELECT id_tab FROM ' . _DB_PREFIX_ . 'tab WHERE class_name = "AdminModulesCatalog"' ); if (!empty($adminModulesCatalogTabId)) { @@ -85,7 +89,7 @@ function ps_1750_update_module_tabs() ); } - $adminModulesManageTabId = Db::getInstance()->getValue( + $adminModulesManageTabId = DbWrapper::getValue( 'SELECT id_tab FROM ' . _DB_PREFIX_ . 'tab WHERE class_name = "AdminModulesManage"' ); if (!empty($adminModulesManageTabId)) { @@ -102,7 +106,7 @@ function ps_1750_update_module_tabs() ); } - $adminModulesAddonsSelectionsTabId = Db::getInstance()->getValue( + $adminModulesAddonsSelectionsTabId = DbWrapper::getValue( 'SELECT id_tab FROM ' . _DB_PREFIX_ . 'tab WHERE class_name = "AdminAddonsCatalog"' ); if (!empty($adminModulesAddonsSelectionsTabId)) { @@ -121,11 +125,11 @@ function ps_1750_update_module_tabs() // STEP 3: Move the 2 module catalog controllers in the parent one // Get The ID of the parent - $adminParentModuleCatalogTabId = Db::getInstance()->getValue( + $adminParentModuleCatalogTabId = DbWrapper::getValue( 'SELECT id_tab FROM ' . _DB_PREFIX_ . 'tab WHERE class_name = "AdminParentModulesCatalog"' ); foreach (['AdminModulesCatalog', 'AdminAddonsCatalog'] as $key => $className) { - Db::getInstance()->execute( + DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'tab` SET `id_parent`= ' . (int) $adminParentModuleCatalogTabId . ', position = ' . $key . ' WHERE `class_name` = "' . $className . '"' ); } diff --git a/upgrade/php/ps_1751_update_module_sf_tab.php b/upgrade/php/ps_1751_update_module_sf_tab.php index 52d396598..f8a3ccb54 100644 --- a/upgrade/php/ps_1751_update_module_sf_tab.php +++ b/upgrade/php/ps_1751_update_module_sf_tab.php @@ -24,14 +24,18 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; + /** * File copied from ps_update_tabs.php and modified for only adding modules related tabs + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1751_update_module_sf_tab() { // Rename parent module tab (= Module manager) include_once 'rename_tab.php'; - $adminModulesParentTabId = Db::getInstance()->getValue( + $adminModulesParentTabId = DbWrapper::getValue( 'SELECT id_tab FROM ' . _DB_PREFIX_ . 'tab WHERE class_name = "AdminModulesSf"' ); if (!empty($adminModulesParentTabId)) { diff --git a/upgrade/php/ps_1760_copy_data_from_currency_to_currency_lang.php b/upgrade/php/ps_1760_copy_data_from_currency_to_currency_lang.php index 55027ad92..bb997ca7a 100755 --- a/upgrade/php/ps_1760_copy_data_from_currency_to_currency_lang.php +++ b/upgrade/php/ps_1760_copy_data_from_currency_to_currency_lang.php @@ -24,9 +24,15 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; use PrestaShop\PrestaShop\Adapter\SymfonyContainer; use PrestaShop\PrestaShop\Core\Localization\CLDR\LocaleRepository; +/** + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException + */ function ps_1760_copy_data_from_currency_to_currency_lang() { // Force cache reset of languages (load locale column) @@ -34,7 +40,7 @@ function ps_1760_copy_data_from_currency_to_currency_lang() $languages = Language::getLanguages(); foreach ($languages as $language) { - Db::getInstance()->execute( + DbWrapper::execute( 'INSERT INTO `' . _DB_PREFIX_ . 'currency_lang` (`id_currency`, `id_lang`, `name`) SELECT `id_currency`, ' . (int) $language['id_lang'] . ' as id_lang , `name` FROM `' . _DB_PREFIX_ . 'currency` @@ -60,6 +66,9 @@ function ps_1760_copy_data_from_currency_to_currency_lang() ObjectModel::enableCache(); } +/** + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException + */ function refreshLocalizedCurrencyData(Currency $currency, array $languages, LocaleRepository $localeRepoCLDR) { $language = new Language($languages[0]['id_lang']); @@ -71,7 +80,7 @@ function refreshLocalizedCurrencyData(Currency $currency, array $languages, Loca 'numeric_iso_code' => $cldrCurrency->getNumericIsoCode(), 'precision' => $cldrCurrency->getDecimalDigits(), ]; - Db::getInstance()->update('currency', $fields, 'id_currency = ' . (int) $currency->id); + DbWrapper::update('currency', $fields, 'id_currency = ' . (int) $currency->id); } foreach ($languages as $languageData) { @@ -97,6 +106,6 @@ function refreshLocalizedCurrencyData(Currency $currency, array $languages, Loca $where = 'id_currency = ' . (int) $currency->id . ' AND id_lang = ' . (int) $language->id; - Db::getInstance()->update('currency_lang', $fields, $where); + DbWrapper::update('currency_lang', $fields, $where); } } diff --git a/upgrade/php/ps_1760_update_tabs.php b/upgrade/php/ps_1760_update_tabs.php index e36f67d9b..9429e8d32 100644 --- a/upgrade/php/ps_1760_update_tabs.php +++ b/upgrade/php/ps_1760_update_tabs.php @@ -24,8 +24,12 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; + /** * File copied from ps_1750_update_module_tabs.php and modified to add new roles + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1760_update_tabs() { @@ -52,7 +56,7 @@ function ps_1760_update_tabs() include_once 'add_new_tab.php'; foreach ($moduleTabsToBeAdded as $className => $tabDetails) { add_new_tab_17($className, $tabDetails['translations'], 0, false, $tabDetails['parent']); - Db::getInstance()->execute( + DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'tab` SET `active`= 1 WHERE `class_name` = "' . pSQL($className) . '"' ); } diff --git a/upgrade/php/ps_1761_update_currencies.php b/upgrade/php/ps_1761_update_currencies.php index e5c029c20..661f59389 100644 --- a/upgrade/php/ps_1761_update_currencies.php +++ b/upgrade/php/ps_1761_update_currencies.php @@ -24,6 +24,7 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; use PrestaShop\PrestaShop\Adapter\SymfonyContainer; use PrestaShop\PrestaShop\Core\Localization\CLDR\LocaleRepository; @@ -33,6 +34,8 @@ * corresponding values of each currency. * * This upgrade script will cover this need by loading the CLDR data and update the currency if it still has the default table values. + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1761_update_currencies() { @@ -62,7 +65,7 @@ function ps_1761_update_currencies() $currency->precision = (int) $cldrCurrency->getDecimalDigits(); $currency->numeric_iso_code = $cldrCurrency->getNumericIsoCode(); } - Db::getInstance()->execute( + DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'currency` SET `precision` = ' . $currency->precision . ', `numeric_iso_code` = ' . $currency->numeric_iso_code . ' WHERE `id_currency` = ' . $currency->id diff --git a/upgrade/php/ps_1763_update_tabs.php b/upgrade/php/ps_1763_update_tabs.php index 5ce7b6031..7da6ec272 100644 --- a/upgrade/php/ps_1763_update_tabs.php +++ b/upgrade/php/ps_1763_update_tabs.php @@ -24,8 +24,12 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; + /** * File copied from ps_1750_update_module_tabs.php and modified to add new roles + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1763_update_tabs() { @@ -33,20 +37,20 @@ function ps_1763_update_tabs() include_once 'copy_tab_rights.php'; add_new_tab_17('AdminParentMailTheme', 'en:Email Themes', 0, false, 'AdminParentThemes'); - Db::getInstance()->execute( + DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'tab` SET `active`= 1, `position`= 2 WHERE `class_name` = "AdminParentMailTheme"' ); // Move AdminMailTheme's parent from AdminMailThemeParent to AdminParentMailTheme - $toParentTabId = Db::getInstance()->getValue( + $toParentTabId = DbWrapper::getValue( 'SELECT id_tab FROM `' . _DB_PREFIX_ . 'tab` WHERE `class_name` = "AdminParentMailTheme"' ); - Db::getInstance()->execute( + DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'tab` SET `id_parent` = ' . $toParentTabId . ' WHERE class_name = "AdminMailTheme"' ); copy_tab_rights('AdminMailTheme', 'AdminParentMailTheme'); - Db::getInstance()->execute( + DbWrapper::execute( 'DELETE FROM `' . _DB_PREFIX_ . 'tab` WHERE `class_name` = "AdminMailThemeParent"' ); } diff --git a/upgrade/php/ps_1770_preset_tab_enabled.php b/upgrade/php/ps_1770_preset_tab_enabled.php index 1ae2863eb..7b110c816 100644 --- a/upgrade/php/ps_1770_preset_tab_enabled.php +++ b/upgrade/php/ps_1770_preset_tab_enabled.php @@ -24,18 +24,22 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; + /** * Preset enabled new column in tabs to true for all (except for disabled modules) + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1770_preset_tab_enabled() { //First set all tabs enabled - $result = Db::getInstance()->execute( + $result = DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'tab` SET `enabled` = 1' ); //Then search for inactive modules and disable their tabs - $inactiveModules = Db::getInstance()->executeS( + $inactiveModules = DbWrapper::executeS( 'SELECT `name` FROM `' . _DB_PREFIX_ . 'module` WHERE `active` != 1' ); $moduleNames = []; @@ -43,7 +47,7 @@ function ps_1770_preset_tab_enabled() $moduleNames[] = '"' . $inactiveModule['name'] . '"'; } if (count($moduleNames) > 0) { - $result &= Db::getInstance()->execute( + $result &= DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'tab` SET `enabled` = 0 WHERE `module` IN (' . implode(',', $moduleNames) . ')' ); } diff --git a/upgrade/php/ps_1770_update_charset.php b/upgrade/php/ps_1770_update_charset.php index 410abab49..ada7ab748 100644 --- a/upgrade/php/ps_1770_update_charset.php +++ b/upgrade/php/ps_1770_update_charset.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return bool + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1770_update_charset() { $adminFilterTableExists = $adminFilterFilterIdExists = $moduleHistoryTableExists = $translationTableExists = false; try { - $adminFilterTableExists = (bool) Db::getInstance()->executeS( + $adminFilterTableExists = (bool) DbWrapper::executeS( 'SELECT count(*) FROM ' . _DB_PREFIX_ . 'admin_filter' ); if ($adminFilterTableExists) { - $adminFilterFilterIdExists = (bool) Db::getInstance()->executeS( + $adminFilterFilterIdExists = (bool) DbWrapper::executeS( 'SELECT count(filter_id) FROM ' . _DB_PREFIX_ . 'admin_filter' ); } @@ -40,14 +49,14 @@ function ps_1770_update_charset() } try { - $moduleHistoryTableExists = (bool) Db::getInstance()->executeS( + $moduleHistoryTableExists = (bool) DbWrapper::executeS( 'SELECT count(*) FROM ' . _DB_PREFIX_ . 'module_history' ); } catch (Exception $e) { } try { - $translationTableExists = (bool) Db::getInstance()->executeS( + $translationTableExists = (bool) DbWrapper::executeS( 'SELECT count(*) FROM ' . _DB_PREFIX_ . 'translation' ); } catch (Exception $e) { @@ -57,29 +66,29 @@ function ps_1770_update_charset() if ($adminFilterTableExists) { if ($adminFilterFilterIdExists) { - $result &= Db::getInstance()->execute( + $result = $result && DbWrapper::execute( 'UPDATE ' . _DB_PREFIX_ . '`admin_filter` SET `filter_id` = SUBSTRING(`filter_id`, 1, 191)' ); - $result &= Db::getInstance()->execute( + $result = $result && DbWrapper::execute( 'ALTER TABLE ' . _DB_PREFIX_ . '`admin_filter` CHANGE `filter_id` `filter_id` VARCHAR(191) NOT NULL' ); } - $result &= Db::getInstance()->execute( + $result = $result && DbWrapper::execute( 'ALTER TABLE ' . _DB_PREFIX_ . '`admin_filter` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci' ); } if ($moduleHistoryTableExists) { - $result &= Db::getInstance()->execute( + $result = $result && DbWrapper::execute( 'ALTER TABLE ' . _DB_PREFIX_ . '`module_history` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci' ); } if ($translationTableExists) { - $result &= Db::getInstance()->execute( + $result = $result && DbWrapper::execute( 'ALTER TABLE ' . _DB_PREFIX_ . '`translation` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci' ); } - return (bool) $result; + return $result; } diff --git a/upgrade/php/ps_1770_update_order_status_colors.php b/upgrade/php/ps_1770_update_order_status_colors.php index 06c5d815d..c309d820c 100644 --- a/upgrade/php/ps_1770_update_order_status_colors.php +++ b/upgrade/php/ps_1770_update_order_status_colors.php @@ -24,10 +24,13 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; use PrestaShop\PrestaShop\Core\Domain\Order\Status\OrderStatusColor; /** * Updates order status colors according to new color schema + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1770_update_order_status_colors() { @@ -59,7 +62,7 @@ function ps_1770_update_order_status_colors() foreach ($statusColorMap as $color => $statuses) { foreach ($statuses as $statusId) { - Db::getInstance()->execute( + DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'order_state` SET `color` = "' . pSQL($color) . '" WHERE `id_order_state` = ' . (int) $statusId ); } @@ -76,7 +79,7 @@ function ps_1770_update_order_status_colors() foreach ($conditions as $field => $expectedValue) { $whereCondition .= ' AND `' . $field . '` = "' . pSQL($expectedValue) . '"'; } - Db::getInstance()->execute( + DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'order_state` SET `color` = "' . pSQL($color) . '"' . $whereCondition ); } diff --git a/upgrade/php/ps_1771_update_customer_note.php b/upgrade/php/ps_1771_update_customer_note.php index e1862f237..0e4a63c01 100644 --- a/upgrade/php/ps_1771_update_customer_note.php +++ b/upgrade/php/ps_1771_update_customer_note.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors */ -/* +/** * Since note is now TYPE_STRING instead of TYPE_HTML it needs to be decoded + * + * @return bool + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1771_update_customer_note() { - $notes = Db::getInstance()->executeS( + $notes = DbWrapper::executeS( 'SELECT id_customer, note FROM ' . _DB_PREFIX_ . 'customer WHERE note IS NOT NULL AND note != ""' ); $result = true; foreach ($notes as $note) { - $result &= Db::getInstance()->execute( + $result &= DbWrapper::execute( 'UPDATE ' . _DB_PREFIX_ . 'customer SET note = "' . pSQL(html_entity_decode($note['note'])) . '" WHERE id_customer = ' . $note['id_customer'] diff --git a/upgrade/php/ps_1780_add_feature_flag_tab.php b/upgrade/php/ps_1780_add_feature_flag_tab.php index fa34d9257..cd0e228e5 100644 --- a/upgrade/php/ps_1780_add_feature_flag_tab.php +++ b/upgrade/php/ps_1780_add_feature_flag_tab.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_1780_add_feature_flag_tab() { $className = 'AdminFeatureFlag'; - $result = Db::getInstance()->executeS( + $result = DbWrapper::executeS( 'SELECT id_tab FROM `' . _DB_PREFIX_ . 'tab` WHERE `class_name` = \'AdminAdvancedParameters\'' ); @@ -46,7 +55,7 @@ function ps_1780_add_feature_flag_tab() $advancedParametersTabId ); - Db::getInstance()->execute( + DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'tab` SET `active`= 1, `enabled` = 1 WHERE `class_name` = \'' . $className . '\'' ); } diff --git a/upgrade/php/ps_800_add_security_tab.php b/upgrade/php/ps_800_add_security_tab.php index 4bdf18164..60489ca6e 100644 --- a/upgrade/php/ps_800_add_security_tab.php +++ b/upgrade/php/ps_800_add_security_tab.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_800_add_security_tab() { @@ -68,7 +77,7 @@ function ps_800_add_security_tab() foreach ($tabsData[$tab[0]] as $key => $value) { $data[] = '`' . $key . '` = ' . $value; } - Db::getInstance()->execute( + DbWrapper::execute( 'UPDATE `' . _DB_PREFIX_ . 'tab` SET ' . implode(', ', $data) . ' WHERE `class_name` = \'' . $tab[0] . '\'' ); } diff --git a/upgrade/php/ps_810_update_product_page_feature_flags.php b/upgrade/php/ps_810_update_product_page_feature_flags.php index 4d7c55a8a..96b7f9874 100644 --- a/upgrade/php/ps_810_update_product_page_feature_flags.php +++ b/upgrade/php/ps_810_update_product_page_feature_flags.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_810_update_product_page_feature_flags() { - $featureFlags = Db::getInstance()->executeS('SELECT name, state FROM `' . _DB_PREFIX_ . 'feature_flag`'); + $featureFlags = DbWrapper::executeS('SELECT name, state FROM `' . _DB_PREFIX_ . 'feature_flag`'); // Check if one of the feature flag is already enabled $productState = 0; @@ -43,12 +52,12 @@ function ps_810_update_product_page_feature_flags() } // Update product feature flag with stability, and appropriate state - Db::getInstance()->update('feature_flag', [ + DbWrapper::update('feature_flag', [ 'stability' => 'stable', 'state' => $productState, 'label_wording' => 'New product page', ], '`name` = \'product_page_v2\''); // Delete the multishop feature flag - Db::getInstance()->delete('feature_flag', '`name` = \'product_page_v2_multi_shop\''); + DbWrapper::delete('feature_flag', '`name` = \'product_page_v2_multi_shop\''); } diff --git a/upgrade/php/ps_811_update_redirect_type.php b/upgrade/php/ps_811_update_redirect_type.php index aeaabbdd2..411760fa2 100644 --- a/upgrade/php/ps_811_update_redirect_type.php +++ b/upgrade/php/ps_811_update_redirect_type.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return true + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_811_update_redirect_type() { // Get information about redirect_type column - $columnInformation = Db::getInstance()->executeS('SHOW COLUMNS FROM `' . _DB_PREFIX_ . 'product` LIKE "redirect_type"'); + $columnInformation = DbWrapper::executeS('SHOW COLUMNS FROM `' . _DB_PREFIX_ . 'product` LIKE "redirect_type"'); if (empty($columnInformation)) { return true; } @@ -40,14 +49,14 @@ function ps_811_update_redirect_type() } // If not, we execute our upgrade queries - Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . "product` MODIFY COLUMN `redirect_type` ENUM( + DbWrapper::execute('ALTER TABLE `' . _DB_PREFIX_ . "product` MODIFY COLUMN `redirect_type` ENUM( '','404','410','301-product','302-product','301-category','302-category','200-displayed','404-displayed','410-displayed','default' ) NOT NULL DEFAULT 'default';"); - Db::getInstance()->execute('ALTER TABLE `' . _DB_PREFIX_ . "product_shop` MODIFY COLUMN `redirect_type` ENUM( + DbWrapper::execute('ALTER TABLE `' . _DB_PREFIX_ . "product_shop` MODIFY COLUMN `redirect_type` ENUM( '','404','410','301-product','302-product','301-category','302-category','200-displayed','404-displayed','410-displayed','default' ) NOT NULL DEFAULT 'default';"); - Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . "product` SET `redirect_type` = 'default' WHERE `redirect_type` = '404' OR `redirect_type` = '' OR `redirect_type` IS NULL;"); - Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . "product_shop` SET `redirect_type` = 'default' WHERE `redirect_type` = '404' OR `redirect_type` = '' OR `redirect_type` IS NULL;"); + DbWrapper::execute('UPDATE `' . _DB_PREFIX_ . "product` SET `redirect_type` = 'default' WHERE `redirect_type` = '404' OR `redirect_type` = '' OR `redirect_type` IS NULL;"); + DbWrapper::execute('UPDATE `' . _DB_PREFIX_ . "product_shop` SET `redirect_type` = 'default' WHERE `redirect_type` = '404' OR `redirect_type` = '' OR `redirect_type` IS NULL;"); return true; } diff --git a/upgrade/php/ps_remove_controller_tab.php b/upgrade/php/ps_remove_controller_tab.php index 04c412946..0ba041db2 100644 --- a/upgrade/php/ps_remove_controller_tab.php +++ b/upgrade/php/ps_remove_controller_tab.php @@ -1,11 +1,41 @@ + */ + +use PrestaShop\Module\AutoUpgrade\DbWrapper; + +/** + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException + */ function ps_remove_controller_tab($className, $quickAccessUrls = []) { // select given tab $tabsToBeDeleted = []; $rolesToBeDeleted = []; - $tabToDelete = Db::getInstance()->getRow( + $tabToDelete = DbWrapper::getRow( sprintf("SELECT id_tab, id_parent, class_name FROM %stab WHERE class_name = '%s'", _DB_PREFIX_, $className) ); @@ -17,7 +47,7 @@ function ps_remove_controller_tab($className, $quickAccessUrls = []) getElementsToBeDeleted($tabToDelete['id_tab'], $tabToDelete['id_parent'], $className, $tabsToBeDeleted, $rolesToBeDeleted); // delete tabs fetched by the recursive function - Db::getInstance()->execute( + DbWrapper::execute( sprintf( 'DELETE FROM %stab WHERE id_tab IN (%s)', _DB_PREFIX_, @@ -26,7 +56,7 @@ function ps_remove_controller_tab($className, $quickAccessUrls = []) ); // delete orphan tab langs - Db::getInstance()->execute( + DbWrapper::execute( sprintf( 'DELETE FROM `%stab_lang` WHERE `id_tab` NOT IN (SELECT id_tab FROM `%stab`)', _DB_PREFIX_, @@ -43,8 +73,8 @@ function ps_remove_controller_tab($className, $quickAccessUrls = []) $className ); - Db::getInstance()->execute($sqlLegacyQuickAccessLinkDeletion); - Db::getInstance()->execute( + DbWrapper::execute($sqlLegacyQuickAccessLinkDeletion); + DbWrapper::execute( sprintf( "DELETE FROM `%squick_access` WHERE link LIKE '%%controller=%s%%'", _DB_PREFIX_, @@ -57,7 +87,7 @@ function ps_remove_controller_tab($className, $quickAccessUrls = []) foreach ($quickAccessUrls as &$link) { $link = "'" . $link . "'"; } - Db::getInstance()->execute( + DbWrapper::execute( sprintf( 'DELETE FROM %squick_access WHERE link IN (%s)', _DB_PREFIX_, @@ -75,9 +105,12 @@ function ps_remove_controller_tab($className, $quickAccessUrls = []) } $sqlRoleDeletion .= "OR slug LIKE '" . $role . "'"; } - Db::getInstance()->execute($sqlRoleDeletion); + DbWrapper::execute($sqlRoleDeletion); } +/** + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException + */ function getElementsToBeDeleted($idTab, $idParent, $className, &$tabsToBeDeleted, &$rolesToBeDeleted) { // add current tab to tabs that will be deleted @@ -88,7 +121,7 @@ function getElementsToBeDeleted($idTab, $idParent, $className, &$tabsToBeDeleted } // check if parent has any other children - $sibling = Db::getInstance()->getRow( + $sibling = DbWrapper::getRow( sprintf( 'SELECT id_tab FROM %stab WHERE id_parent = ' . $idParent . ' AND id_tab NOT IN (%s)', _DB_PREFIX_, @@ -102,7 +135,7 @@ function getElementsToBeDeleted($idTab, $idParent, $className, &$tabsToBeDeleted } // no sibling, get parent and repeat the process recursively - $parentTab = Db::getInstance()->getRow( + $parentTab = DbWrapper::getRow( sprintf('SELECT id_tab, id_parent, class_name FROM %stab WHERE id_tab = %s', _DB_PREFIX_, $idParent) ); diff --git a/upgrade/php/ps_update_tab_lang.php b/upgrade/php/ps_update_tab_lang.php index e5631c13d..14ea5af46 100644 --- a/upgrade/php/ps_update_tab_lang.php +++ b/upgrade/php/ps_update_tab_lang.php @@ -24,6 +24,8 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; + /** * Updates ps_tab_lang table for a given domain and className * @@ -32,6 +34,8 @@ * * @param string $domain * @param string $className + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_update_tab_lang($domain, $className) { @@ -43,7 +47,7 @@ function ps_update_tab_lang($domain, $className) _DB_PREFIX_, $className ); - $tab = Db::getInstance()->getRow($tabQuery); + $tab = DbWrapper::getRow($tabQuery); if (empty($tab)) { return; @@ -70,6 +74,6 @@ function ps_update_tab_lang($domain, $className) $tab['id_tab'], $lang['id_lang'] ); - Db::getInstance()->execute($updateQuery); + DbWrapper::execute($updateQuery); } } diff --git a/upgrade/php/ps_update_tabs.php b/upgrade/php/ps_update_tabs.php index 7b3ed83d6..dc59b092e 100644 --- a/upgrade/php/ps_update_tabs.php +++ b/upgrade/php/ps_update_tabs.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function ps_update_tabs() { @@ -36,7 +45,7 @@ function ps_update_tabs() $tab_class_name[$tab['class_name']] = $tab['@attributes']['id']; } - $tabs = Db::getInstance()->executeS('SELECT * FROM `' . _DB_PREFIX_ . 'tab`', true, false); + $tabs = DbWrapper::executeS('SELECT * FROM `' . _DB_PREFIX_ . 'tab`', true, false); if (!empty($tabs)) { foreach ($tabs as $tab) { if (isset($tab_class_name[$tab['class_name']])) { @@ -49,7 +58,7 @@ function ps_update_tabs() } if (!empty($tab_class_name)) { - $langs = Db::getInstance()->executeS('SELECT * FROM `' . _DB_PREFIX_ . 'lang` WHERE `iso_code` != "en" ', true, false); + $langs = DbWrapper::executeS('SELECT * FROM `' . _DB_PREFIX_ . 'lang` WHERE `iso_code` != "en" ', true, false); if (!empty($langs)) { foreach ($langs as $lang) { @@ -66,7 +75,7 @@ function ps_update_tabs() // store DB data $tab_db_data = []; - $results = Db::getInstance()->executeS(' + $results = DbWrapper::executeS(' SELECT t.`id_tab`, tl.`id_lang`, t.`class_name`, tl.`name` FROM `' . _DB_PREFIX_ . 'tab` t INNER JOIN `' . _DB_PREFIX_ . 'tab_lang` tl ON tl.`id_tab` = t.`id_tab` WHERE tl.`id_lang` = ' . (int) $lang['id_lang'], true, false); @@ -91,7 +100,7 @@ function ps_update_tabs() (`id_tab`, `id_lang`, `name`) VALUES (' . (int) $tmp_class_id . ',' . (int) $lang['id_lang'] . ',"' . pSQL($tab) . '")'; - Db::getInstance()->execute($sql); + DbWrapper::execute($sql); } else { // if DB is != XML if ($tab_db_data[$tmp_class_name] != $tab) { @@ -101,7 +110,7 @@ function ps_update_tabs() `id_lang` = ' . (int) $lang['id_lang'] . ' AND `name` = "' . pSQL($tab_db_data[$tmp_class_name]) . '" '; - Db::getInstance()->execute($sql); + DbWrapper::execute($sql); } } } diff --git a/upgrade/php/rename_tab.php b/upgrade/php/rename_tab.php index 525e37270..a2bbdab10 100644 --- a/upgrade/php/rename_tab.php +++ b/upgrade/php/rename_tab.php @@ -1,4 +1,7 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + * @author PrestaShop SA and Contributors + */ + +/** + * @return void + * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException */ function renameTab($id_tab, $names) { if (!$id_tab) { return; } - $langues = Db::getInstance()->executeS('SELECT * FROM ' . _DB_PREFIX_ . 'lang'); + $langues = DbWrapper::executeS('SELECT * FROM ' . _DB_PREFIX_ . 'lang'); foreach ($langues as $lang) { if (array_key_exists($lang['iso_code'], $names)) { - Db::getInstance()->update('tab_lang', ['name' => $names[$lang['iso_code']]], '`id_tab`= ' . $id_tab . ' AND `id_lang` =' . $lang['id_lang']); + DbWrapper::update('tab_lang', ['name' => $names[$lang['iso_code']]], '`id_tab`= ' . $id_tab . ' AND `id_lang` =' . $lang['id_lang']); } } } diff --git a/upgrade/php/update_null_values.php b/upgrade/php/update_null_values.php index f08e325e3..4329a992d 100644 --- a/upgrade/php/update_null_values.php +++ b/upgrade/php/update_null_values.php @@ -24,16 +24,18 @@ * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ +use PrestaShop\Module\AutoUpgrade\DbWrapper; + /** * This function aims to update the null values of several columns, to default values, * in order to avoid unexpected behavior on data that does not take null values into account * + * @throws \PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException + * * @internal */ function update_null_values() { - $db = Db::getInstance(); - $updates = [ ['address', 'address2', ''], ['address', 'company', ''], @@ -229,12 +231,12 @@ function update_null_values() list($tabName, $columnName, $newValue) = $update; // Check if the table exists - if (empty($db->executeS('SHOW TABLES LIKE "' . _DB_PREFIX_ . $tabName . '"'))) { + if (empty(DbWrapper::executeS('SHOW TABLES LIKE "' . _DB_PREFIX_ . $tabName . '"'))) { continue; } // Check if the column exists - if (empty($db->executeS('SHOW COLUMNS FROM `' . _DB_PREFIX_ . $tabName . "` WHERE Field = '" . $columnName . "'"))) { + if (empty(DbWrapper::executeS('SHOW COLUMNS FROM `' . _DB_PREFIX_ . $tabName . "` WHERE Field = '" . $columnName . "'"))) { continue; } @@ -242,6 +244,6 @@ function update_null_values() // Update existing null values $updateQuery = 'UPDATE `' . _DB_PREFIX_ . $tabName . '` SET `' . $columnName . '`=' . $newValue . ' WHERE `' . $columnName . '` IS NULL'; - $db->execute($updateQuery); + DbWrapper::execute($updateQuery); } }