Skip to content

Commit

Permalink
release: module update from ifthenpay/dev_prestashop_8 (ifthenpay/dev…
Browse files Browse the repository at this point in the history
  • Loading branch information
joaocouto-ifthenpay committed Aug 14, 2023
1 parent 02011ee commit a5e4c02
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions ifthenpay/classes/Config/IfthenpaySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

namespace PrestaShop\Module\Ifthenpay\Config;
use PrestaShop\Module\Ifthenpay\Log\IfthenpayLogProcess;

if (!defined('_PS_VERSION_')) {
exit;
Expand Down Expand Up @@ -116,6 +117,55 @@ public function __construct($userPaymentMethods = null)
$this->ifthenpayStatusKeys = ['IFTHENPAY_{paymentMethod}_OS_WAITING', 'IFTHENPAY_{paymentMethod}_OS_CONFIRMED'];
}

private function migrate_from_17_to_8()
{
$count = 0;
$tablesToCheck = array(
array('name' => _DB_PREFIX_ . 'ifthenpay_multibanco', 'oldColumnName' => 'request_id'),
array('name' => _DB_PREFIX_ . 'ifthenpay_mbway', 'oldColumnName' => 'id_transacao'),
array('name' => _DB_PREFIX_ . 'ifthenpay_ccard', 'oldColumnName' => 'requestId'),
array('name' => _DB_PREFIX_ . 'ifthenpay_payshop', 'oldColumnName' => 'id_transacao')
);

foreach ($tablesToCheck as $tableInfo) {

$columnCheckResult = checkColumnsExistence($tableInfo['name'], $tableInfo['oldColumnName']);

if ($columnCheckResult == 1) {
$alterColumnResponse = alterColumnName($tableInfo['name'], $tableInfo['oldColumnName']);
$count += $alterColumnResponse ? 1 : 0;
}
}

return $count > 0 ? true : false;
}

function checkColumnsExistence($tableName, $oldColumnName)
{
$query = 'SELECT COUNT(*) AS column_exists
FROM information_schema.columns
WHERE table_name = \'' . pSQL($tableName) . '\'
AND column_name IN (\'' . pSQL($oldColumnName) . '\')';

$count = \Db::getInstance()->getValue($query);

return $count > 0 ? 1 : 0;
}

function alterColumnName($tableName, $oldColumnName)
{
$newColumnName = 'transaction_id';

$alterQuery = 'ALTER TABLE `' . pSQL($tableName) . '`
CHANGE `' . pSQL($oldColumnName) . '` `' . pSQL($newColumnName) . '` VARCHAR(20) NULL';

$result = \Db::getInstance()->execute($alterQuery);

IfthenpayLogProcess::addLog('Ran migration script (alterColumnName()) for table ' . $tableName . ' with result code = ' . $result, IfthenpayLogProcess::INFO, 0);

return $result == 1 ? true : false;
}

private function createShopSql()
{
foreach ($this->userPaymentMethods as $paymentMethod) {
Expand Down Expand Up @@ -176,6 +226,7 @@ public function install()
{
$this->createIfthenpaySql();
$this->createShopSql();
$this->migrate_from_17_to_8();
}

public function uninstall()
Expand Down

0 comments on commit a5e4c02

Please sign in to comment.