magento / magento/inventory

MigrateCatalogInventoryNotifyStockQuantityData patch crashes when there is more then one stock in cataloginventory_stock

Open
#3,399 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Progress: ready for grooming
Dominant language
PHP
Stars
357
Forks
262
PR merge metrics
No merged PRs in 30d

Description

### Preconditions (*)
1. Seen on Magento OS 2.4.6-p2 with MSI modules at version 1.2.6-p2

### Steps to reproduce (*)
1. We have a client with this super old Magento shop on version 2.2.3 that we are finally upgrading to 2.4.6-p2
2. This old shop has a module Magestore_Webpos that years ago created a second stock in the `cataloginventory_stock` table, and we also have a whole bunch of entries in the `cataloginventory_stock_item` table that references that second stock (id: 2)
3. When we upgrade this shop to Magento 2.4.6-p2 and enable the MSI modules, then running `bin/magento setup:upgrade` results in the following error:
```
...
layout: 1
block_html: 1
full_page: 1
Unable to apply data patch Magento\InventoryLowQuantityNotification\Setup\Patch\Data\MigrateCatalogInventoryNotifyStockQuantityData for module Magento_InventoryLowQuantityNotification. Original exception message: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'default-KB001' for key 'PRIMARY', query was: INSERT INTO `inventory_low_stock_notification_configuration` (`source_code`, `sku`, `notify_stock_qty`) SELECT `source_item`.`source_code`, `source_item`.`sku`, `stock_item`.`notify_stock_qty` FROM `cataloginventory_stock_item` AS `stock_item`
INNER JOIN `catalog_product_entity` AS `product` ON product.entity_id = stock_item.product_id
INNER JOIN `inventory_source_item` AS `source_item` ON source_item.sku = product.sku WHERE (stock_item.use_config_notify_stock_qty = 0) AND (source_item.source_code = 'default')
```

Running the SELECT part of the query results in:
```sql
mysql> SELECT `source_item`.`source_code`, `source_item`.`sku`, `stock_item`.`notify_stock_qty` FROM `cataloginventory_stock_item` AS `stock_item`
INNER JOIN `catalog_product_entity` AS `product` ON product.entity_id = stock_item.product_id
INNER JOIN `inventory_source_item` AS `source_item` ON source_item.sku = product.sku WHERE (stock_item.use_config_notify_stock_qty = 0) AND (source_item.source_code = 'default')
+-------------+-------+------------------+
| source_code | sku | notify_stock_qty |
+-------------+-------+------------------+
| default | KB001 | 0.0000 |
| default | KB001 | 0.0000 |
+-------------+-------+------------------+
2 rows in set (0.00 sec)
```

And indeed, there are 2 rows in `cataloginventory_stock_item` that correspond with this SKU (entity_id: 9158)
```sql
mysql> SELECT item_id, product_id, stock_id, use_config_notify_stock_qty FROM cataloginventory_stock_item WHERE product_id = '9158';
+---------+------------+----------+-----------------------------+
| item_id | product_id | stock_id | use_config_notify_stock_qty |
+---------+------------+----------+-----------------------------+
| 9158 | 9158 | 1 | 0 |
| 18830 | 9158 | 2 | 0 |
+---------+------------+----------+-----------------------------+
2 rows in set (0.00 sec)
```

So it sounds like this `MigrateCatalogInventoryNotifyStockQuantityData` patch can't deal with databases that have more then a single stock before MSI got installed.

### Expected result (*)
1. No errors

### Actual result (*)
1. See above

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Magento\InventoryLowQuantityNotification\Setup\Patch\Data\MigrateCatalogInventoryNotifyStockQuantityData and inspect the INSERT query against cataloginventory_stock_item, using the duplicate rows and setup:upgrade reproduction described here. Done means an installation with multiple legacy stocks completes without the duplicate-key error and the migration behavior is covered by appropriate tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.