Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions upgrade/sql/9.2.0.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
/* https://github.com/PrestaShop/PrestaShop/pull/40224 */
SET SESSION sql_mode='';
SET NAMES 'utf8mb4';

-- https://github.com/PrestaShop/PrestaShop/pull/40830
/* PHP:add_column('cart_rule', 'total_quantity', 'int(10) UNSIGNED DEFAULT NULL AFTER `minimum_product_quantity`'); */;
Comment thread
jolelievre marked this conversation as resolved.

-- Populate the new total_quantity column for existing cart rules.
-- Previously, the `quantity` field represented the number of uses LEFT (decremented on each use).
-- The new `total_quantity` field represents the ORIGINAL total number of allowed uses.
-- Formula: total_quantity = quantity (remaining) + quantityUsed (consumed in non-error orders)
-- Cart rules with quantity IS NULL are unlimited and keep total_quantity as NULL.
-- The subquery mirrors the logic from DiscountRepository::getQuantityUsedInOrders,
-- counting non-deleted order_cart_rule entries on orders not in error state.
UPDATE `PREFIX_cart_rule` cr
LEFT JOIN (
SELECT ocr.`id_cart_rule`, COUNT(*) as quantity_used
Comment on lines +15 to +16
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering the impact of having a nest subquery when running it on large tables.

Copy link
Copy Markdown
Member

@Quetzacoalt91 Quetzacoalt91 Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look fine

+------+-------------+------------------+--------+-----------------------+---------+---------+-----------------------------+------+----------------------------------------------+
| id   | select_type | table            | type   | possible_keys         | key     | key_len | ref                         | rows | Extra                                        |
+------+-------------+------------------+--------+-----------------------+---------+---------+-----------------------------+------+----------------------------------------------+
|    1 | PRIMARY     | cr               | ALL    | NULL                  | NULL    | NULL    | NULL                        |    1 | Using where                                  |
|    1 | PRIMARY     | <derived2>       | ref    | key0                  | key0    | 5       | psreference.cr.id_cart_rule |    2 |                                              |
|    2 | DERIVED     | ocr              | ALL    | id_order,id_cart_rule | NULL    | NULL    | NULL                        |    1 | Using where; Using temporary; Using filesort |
|    2 | DERIVED     | o                | eq_ref | PRIMARY,current_state | PRIMARY | 4       | psreference.ocr.id_order    |    1 | Using where                                  |
|    3 | SUBQUERY    | ks_configuration | ref    | name                  | name    | 1018    | const                       |    1 | Using where                                  |
+------+-------------+------------------+--------+-----------------------+---------+---------+-----------------------------+------+----------------------------------------------+

We don't have a DEPENDENT SUBQUERY in the list. The subrequest is made once.

FROM `PREFIX_order_cart_rule` ocr
INNER JOIN `PREFIX_orders` o ON ocr.`id_order` = o.`id_order`
WHERE ocr.`deleted` = 0
AND o.`current_state` != (
SELECT CAST(`value` AS UNSIGNED)
FROM `PREFIX_configuration`
WHERE `name` = 'PS_OS_ERROR'
)
GROUP BY ocr.`id_cart_rule`
) used ON used.`id_cart_rule` = cr.`id_cart_rule`
SET cr.`total_quantity` = cr.`quantity` + COALESCE(used.`quantity_used`, 0)
WHERE cr.`quantity` IS NOT NULL;

-- New B2B feature

-- https://github.com/PrestaShop/PrestaShop/pull/40224
INSERT INTO `PREFIX_feature_flag` (`name`, `type`, `label_wording`, `label_domain`, `description_wording`, `description_domain`, `state`, `stability`) VALUES
('improved_b2b', 'env,dotenv,db', 'Improved B2B', 'Admin.Advparameters.Feature', 'Enable / Disable the improved B2B mode. To use the feature activate the B2B mode in General Settings', 'Admin.Advparameters.Help', 0, 'beta');

/* Insert B2B foundation */
/* https://github.com/PrestaShop/PrestaShop/pull/40632 */
-- Insert B2B foundation
-- https://github.com/PrestaShop/PrestaShop/pull/40632
CREATE TABLE `PREFIX_business_entity`
(
`id_business_entity` INT UNSIGNED AUTO_INCREMENT NOT NULL,
Expand Down Expand Up @@ -97,3 +128,5 @@ CREATE TABLE `PREFIX_b2b_role_authorization_role`

-- https://github.com/PrestaShop/PrestaShop/pull/41028
/* PHP:ps_920_business_entities_tabs(); */;

-- End of B2B feature
Loading