Skip to content

Commit

Permalink
Update missing unique key script
Browse files Browse the repository at this point in the history
  • Loading branch information
M0rgan01 committed Apr 29, 2024
1 parent 21f907d commit 2affe63
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@
*/

// Allows you to catch up on a forgotten uniqueness constraint on the roles
function remove_duplicates_from_authorization_role()
function add_missing_unique_key_from_authorization_role()
{
// Verify if we need to create unique key
$keys = Db::getInstance()->executeS(
'SHOW KEYS FROM ' . _DB_PREFIX_ . "authorization_role WHERE Key_name='slug'"
);

if (empty($keys)) {
return;
}

// We recover the duplicates that we want to keep
$duplicates = Db::getInstance()->executeS(
'SELECT MIN(id_authorization_role) AS keep_ID, slug FROM ' . _DB_PREFIX_ . 'authorization_role GROUP BY slug HAVING COUNT(*) > 1'
Expand All @@ -51,4 +60,8 @@ function remove_duplicates_from_authorization_role()
Db::getInstance()->delete('authorization_role', '`id_authorization_role` = ' . (int) $elementToRemove['id_authorization_role']);
}
}

Db::getInstance()->execute(
'ALTER TABLE ' . _DB_PREFIX_ . "authorization_role ADD UNIQUE KEY `slug` (`slug`)"
);
}
4 changes: 1 addition & 3 deletions upgrade/sql/1.7.1.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,4 @@ ALTER TABLE `PREFIX_tab` CHANGE `icon` `icon` VARCHAR(32) DEFAULT NULL;
ALTER TABLE `PREFIX_tab_lang` CHANGE `id_tab` `id_tab` INT(11) NOT NULL;
ALTER TABLE `PREFIX_tab_lang` CHANGE `id_lang` `id_lang` INT(11) NOT NULL;

/* PHP:remove_duplicates_from_authorization_role(); */;

ALTER TABLE `PREFIX_authorization_role` ADD UNIQUE KEY `slug` (`slug`);
/* PHP:add_missing_unique_key_from_authorization_role(); */;
3 changes: 1 addition & 2 deletions upgrade/sql/8.1.6-catchup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ ALTER TABLE `PREFIX_product_shop` CHANGE `id_type_redirected` `id_type_redirecte
ALTER TABLE `PREFIX_tab` CHANGE `active` `active` TINYINT(1) NOT NULL;
ALTER TABLE `PREFIX_tab` CHANGE `icon` `icon` VARCHAR(32) DEFAULT NULL;

/* PHP:remove_duplicates_from_authorization_role(); */;
ALTER TABLE `PREFIX_authorization_role` ADD UNIQUE KEY `slug` (`slug`);
/* PHP:add_missing_unique_key_from_authorization_role(); */;

/* 1.7.2.0 */
ALTER TABLE `PREFIX_stock_mvt` CHANGE `sign` `sign` SMALLINT(6) NOT NULL DEFAULT '1';
Expand Down

0 comments on commit 2affe63

Please sign in to comment.