Upgrading magento2.3 to 2.3.3

<h1>The Beginnings:</h1>
This is the article for [Magento 2 Advent Calendar 2018](https://adventar.org/calendars/4213) 25th day of 2019.

In this article, I am going to describe some of my experience in upgrading magento2.3.0 to 2.3.3 and its consequences errors and important steps to take care off before starting up.

<h1>Execution Enviroment</h1>
Magento 2.3.0
PHP 7.1.23 
MySQL 5.7.24 Apache 2.4.29

<h1>Steps to reproduces:</h1>
1. Back up the magento2.3 database
2. Back up composer.json file from root in the Magento installation directory.
3. use command: composer require magento/product-community-edition=2.3.3 --no-update
4. composer update

<h1>consequent errors might occurs:</h1>
Unfortunately using above command is not to be and we would stuck into something like the following: 
1. <img width="1013" alt="スクリーンショット 2019-04-10 16.10.46.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/a81685da-e48c-f5bf-5668-77df7c178c7c.png">
if this type of error occurs, then we need to remove all the third party extensions from composer.json and try once again.

2. "The following exception is caused by a lack of memory or swap, or not having swap configured
Check https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors for details"
solution: use this command: 
php -d memory_limit=2G /usr/local/bin/composer update

If all goes well we will see the upgrade process happening:
<img width="625" alt="スクリーンショット 2019-12-25 22.05.13.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/6d809e02-afca-b95c-df15-f44a6aa55239.png">

Any errors at this stage are likely caused by the extensions that need to be upgraded at the same time as Magento to stay compatible.

Now that we have the latest code the only thing left to do is make sure that the database itself is also upgraded with.
Use this command:

bin/magento setup:upgrade

And then we can confirm in the admin that we have upgraded to 2.3.3.
<img width="1361" alt="スクリーンショット 2019-12-25 22.44.29.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/2a0620a6-a39e-1951-7fe1-d98fe3a5c178.png">

<h2>After successful upgrade to 2.3.3 you may also face other consequent errors.</h2>
1. Catalog pagination issue on Elasticsearch
2. Email Message Interface backward compatibility error
3. Uncaught TypeError: urldecode() expects parameter 1 to be string, boolean given in
(in CommunityEngineering/JapaneseUrlRewrite)
4. Warning: Declaration of CommunityEngineering\JapaneseName\Model\DataObject\Copy::setAttributeValueFromExtensibleDataObject($target,$code,$value)...
5. Insufficient inventory error when shipping items
6. When creating a cancellation / credit memo, it will get an error that the product cannot be returned
7. Even if the available quantity is 0, the product will still be displayed on the product list page.

<h2>Solution for those errors:</h2>
1. Catalog_pagination_issue_on_Elasticsearch_6_composer-2019-10-11-08-07-41.patch this is security patches which needs to be applied
command: in shell use patch -p1 < Catalog_pagination_issue_on_Elasticsearch_6_composer-2019-10-11-08-07-41.patch

2. EmailMessageInterface_2.3.3_backward_compatibility_composer-2019-10-30-10-20-09.patch this is another patches needs to be inserted
command: in shell use patch -p1 < EmailMessageInterface_2.3.3_backward_compatibility_composer-2019-10-30-10-20-09.patch

3. CommunityEngineering/JapaneseUrlRewrite needs to be disabled
   php bin/magento module:disable:CommunityEngineering_JapaneseUrlRewrite
4. add some lines in CommunityEngineering/JapaneseName/Model/DataObject/Copy.php 
   + use Magento\Framework\Api\ExtensibleDataInterface;
   + protected function setAttributeValueFromExtensibleDataObject(ExtensibleDataInterface $target, $code, $value)
   <img width="938" alt="スクリーンショット 2019-12-25 22.52.24.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327994/b315b58f-b3b2-d66b-bc51-1fa8fcea5bd3.png">


5. first confirm the order data only using : php bin/magento inventory:reservation:list-inconsistencies
then use this command:
bin/magento inventory:reservation:list-inconsistencies -r | bin/magento inventory:reservation:create-compensations

6. use this command to solve above one : insert into inventory_shipment_source(shipment_id, source_code) values({ShipmentId}, {return stock source_name})

7. use this sql:
SELECT 
     e.entity_id,
     csi.is_in_stock,
     isi.quantity + SUM(ir.quantity) as  salable_quantity
FROM
    catalog_product_entity AS e
	LEFT JOIN inventory_source_item isi ON e.sku=isi.sku   
    LEFT JOIN inventory_reservation ir ON e.sku = ir.sku
    LEFT JOIN cataloginventory_stock_item as csi on e.entity_id=csi.product_id
GROUP BY e.sku
HAVING salable_quantity=0 and is_in_stock=1;
and check if salable_quantity=0 then change the status to out_of_stock.

<h1>In short</h1>
There are lot more problems occurred in upgrading 2.3.3 and most of them are likely solved during upgrading process. I was stucked into those problems and tried to find out the solution for those errors. And finally able to solve those problem just googling and consulting to friends.
So, before upgrading to 2.3.3 we need to take care lot of steps and the above steps may help to someone else who tries to do the same as I had done before.