Skip to content

Commit

Permalink
chore: reset full sync for products
Browse files Browse the repository at this point in the history
  • Loading branch information
fox-john committed Dec 17, 2024
1 parent 45b4572 commit 9230fa8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
3 changes: 1 addition & 2 deletions src/Api/SyncApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use PrestaShop\Module\PsEventbus\Config\Config;
use PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService;
use Prestashop\ModuleLibGuzzleAdapter\ClientFactory;
use Prestashop\ModuleLibGuzzleAdapter\Interfaces\HttpClientInterface;
use Symfony\Component\HttpClient\HttpClient;

if (!defined('_PS_VERSION_')) {
Expand Down Expand Up @@ -86,7 +85,7 @@ public function validateJobId($jobId)
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $this->jwt,
'User-Agent' => 'ps-eventbus/' . $this->module->version,
]
],
]
);
} else {
Expand Down
1 change: 0 additions & 1 deletion src/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public static function create($level = '')
}

/**
*
* @return MonologLogger
*/
public static function getInstance()
Expand Down
75 changes: 40 additions & 35 deletions upgrade/Upgrade-4.0.0.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,57 +32,63 @@
*/
function upgrade_module_4_0_0()
{
if (!removeDuplicateEntryFromTypeSyncTable()) {
$db = Db::getInstance();

// Retirer les doublons de la table type sync
if (!removeDuplicateEntryFromTypeSyncTable($db)) {
throw new \PrestaShopException('Failed to remove duplicate entry from eventbus_type_sync.');
}

// Ajouter la clé primaire
if (!addPrimaryKeyToTypeSyncTable()) {
if (!addPrimaryKeyToTypeSyncTable($db)) {
throw new \PrestaShopException('Failed to add primary key to eventbus_type_sync.');
}

// Ajouter la colonne action
if (!addActionToIncrementalSyncTable()) {
if (!addActionToIncrementalSyncTable($db)) {
throw new \PrestaShopException("Failed to add 'action' column to eventbus_incremental_sync.");
}

// Migrer les données
if (!migrateDeleteTableToIncremantalTable()) {
if (!migrateDeleteTableToIncremantalTable($db)) {
throw new \PrestaShopException('Failed to migrate data to eventbus_incremental_sync.');
}

// relancer une full sync pour products (erreurs de synchro par le passé)
if (!resetProductFullSync($db)) {
throw new \PrestaShopException('Failed to migrate data to eventbus_incremental_sync.');
}

return true;
}

function removeDuplicateEntryFromTypeSyncTable()
function removeDuplicateEntryFromTypeSyncTable($db)
{
if (tableTypeSyncAlreadyMigrated()) {
if (tableTypeSyncAlreadyMigrated($db)) {
return true;
}

$db = Db::getInstance();

// Check if old table exist (after error at install ?)
$checkOldTableQuery = "SHOW TABLES LIKE '" . _DB_PREFIX_ . "eventbus_type_sync_old';";
$oldTableExists = $db->executeS($checkOldTableQuery);

if ($oldTableExists) {
// If temp table exist (after error at install ?), rename to "eventbus_type_sync"
$renameOldTableQuery = 'RENAME TABLE `' . _DB_PREFIX_ . 'eventbus_type_sync_old` TO `' . _DB_PREFIX_ . 'eventbus_type_sync`;';
$renameOldTableQuery = "RENAME TABLE `" . _DB_PREFIX_ . "eventbus_type_sync_old` TO `" . _DB_PREFIX_ . "eventbus_type_sync`;";
$db->query($renameOldTableQuery);
}

// Rename original table to old (for temporary edit)
$renameTableQuery = 'RENAME TABLE `' . _DB_PREFIX_ . 'eventbus_type_sync` TO `' . _DB_PREFIX_ . 'eventbus_type_sync_old`;';
$renameTableQuery = "RENAME TABLE `" . _DB_PREFIX_ . "eventbus_type_sync` TO `" . _DB_PREFIX_ . "eventbus_type_sync_old`;";
$db->query($renameTableQuery);

// Create new table, clone of original table (old here)
$createNewTableQuery = 'CREATE TABLE `' . _DB_PREFIX_ . 'eventbus_type_sync` LIKE `' . _DB_PREFIX_ . 'eventbus_type_sync_old`;';
$createNewTableQuery = "CREATE TABLE `" . _DB_PREFIX_ . "eventbus_type_sync` LIKE `" . _DB_PREFIX_ . "eventbus_type_sync_old`;";
$db->query($createNewTableQuery);

// Migrate data from old table to new table, and remove duplicate entries
$migrateToNewTableQuery = '
INSERT INTO `' . _DB_PREFIX_ . 'eventbus_type_sync` (`type`, `offset`, `id_shop`, `lang_iso`, `full_sync_finished`, `last_sync_date`)
$migrateToNewTableQuery = "
INSERT INTO `" . _DB_PREFIX_ . "eventbus_type_sync` (`type`, `offset`, `id_shop`, `lang_iso`, `full_sync_finished`, `last_sync_date`)
SELECT
`type`,
CASE
Expand All @@ -99,44 +105,40 @@ function removeDuplicateEntryFromTypeSyncTable()
WHEN COUNT(*) > 1 THEN MAX(`last_sync_date`) -- Si plusieurs entrées similaires, garder la dernière date
ELSE MAX(`last_sync_date`) -- Sinon, on garde la date existante
END AS `last_sync_date`
FROM `' . _DB_PREFIX_ . 'eventbus_type_sync_old`
FROM `" . _DB_PREFIX_ . "eventbus_type_sync_old`
GROUP BY `type`, `id_shop`, `lang_iso`;
';
";
$db->query($migrateToNewTableQuery);

// remove old table
$dropOldTableQuery = 'DROP TABLE `' . _DB_PREFIX_ . 'eventbus_type_sync_old`;';
$dropOldTableQuery = "DROP TABLE `" . _DB_PREFIX_ . "eventbus_type_sync_old`;";
$db->query($dropOldTableQuery);

return true; // Succès
}

function addPrimaryKeyToTypeSyncTable()
function addPrimaryKeyToTypeSyncTable($db)
{
if (tableTypeSyncAlreadyMigrated()) {
if (tableTypeSyncAlreadyMigrated($db)) {
return true;
}

$db = Db::getInstance();

// Add primary key
$editTypeSyncTable = 'ALTER TABLE `' . _DB_PREFIX_ . 'eventbus_type_sync` ADD PRIMARY KEY (type, id_shop, lang_iso);';
$editTypeSyncTable = "ALTER TABLE `" . _DB_PREFIX_ . "eventbus_type_sync` ADD PRIMARY KEY (type, id_shop, lang_iso);";

return (bool) $db->query($editTypeSyncTable);
}

function addActionToIncrementalSyncTable()
function addActionToIncrementalSyncTable($db)
{
$db = Db::getInstance();

// Check if the 'action' column exists in the table, if column not exist, this migration script aren't executed before
$checkColumnQuery = 'SHOW COLUMNS FROM `' . _DB_PREFIX_ . "eventbus_incremental_sync` LIKE 'action';";
$checkColumnQuery = "SHOW COLUMNS FROM `" . _DB_PREFIX_ . "eventbus_incremental_sync` LIKE 'action';";
$columns = $db->executeS($checkColumnQuery);

// Add 'action' column if it does'nt exist
if (empty($columns)) {
$editIncrementalTable = '
ALTER TABLE `' . _DB_PREFIX_ . "eventbus_incremental_sync`
$editIncrementalTable = "
ALTER TABLE `" . _DB_PREFIX_ . "eventbus_incremental_sync`
ADD action varchar(50) NOT NULL DEFAULT 'upsert',
MODIFY COLUMN id_object VARCHAR(50);
";
Expand All @@ -147,10 +149,8 @@ function addActionToIncrementalSyncTable()
return true; // Column already exists, no need to alter
}

function migrateDeleteTableToIncremantalTable()
function migrateDeleteTableToIncremantalTable($db)
{
$db = Db::getInstance();

// check if 'ps_eventbus_deleted_objects' exist
$checkDeletedTableExistsQuery = "SHOW TABLES LIKE '" . _DB_PREFIX_ . "eventbus_deleted_objects'";
$tableExists = $db->executeS($checkDeletedTableExistsQuery);
Expand Down Expand Up @@ -188,19 +188,24 @@ function migrateDeleteTableToIncremantalTable()

if ($migrationSucceded) {
// Drop eventbus_deleted_objects table
$dropDeletedTable = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'eventbus_deleted_objects`';
$dropDeletedTable = "DROP TABLE IF EXISTS `" . _DB_PREFIX_ . "eventbus_deleted_objects`";

return $db->query($dropDeletedTable);
}
}

function tableTypeSyncAlreadyMigrated()
function tableTypeSyncAlreadyMigrated($db)
{
$db = Db::getInstance();

// Check if the primary key exists by inspecting the indexes
$checkPrimaryKeyQuery = 'SHOW INDEXES FROM `' . _DB_PREFIX_ . "eventbus_type_sync` WHERE Key_name = 'PRIMARY';";
$checkPrimaryKeyQuery = "SHOW INDEXES FROM `" . _DB_PREFIX_ . "eventbus_type_sync` WHERE Key_name = 'PRIMARY';";

// Exécuter la requête pour obtenir les index et vérifier s'il y a un index primaire
return $db->executeS($checkPrimaryKeyQuery);
}

function resetProductFullSync($db)
{
$resetProductFullSyncQuery = "DELETE FROM `" . _DB_PREFIX_ . "eventbus_type_sync` WHERE type = 'products'";

return $db->query($resetProductFullSyncQuery);
}

0 comments on commit 9230fa8

Please sign in to comment.