Skip to content

Specify connexion for db upgrade #18728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
80 changes: 58 additions & 22 deletions app/code/Magento/Sales/Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\DB\AggregatedFieldDataConverter;
use Magento\Framework\DB\DataConverter\SerializedToJson;
use Magento\Framework\DB\FieldToConvert;
use Magento\Framework\Module\Manager;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
Expand Down Expand Up @@ -62,6 +63,11 @@ class UpgradeData implements UpgradeDataInterface
*/
private $state;

/**
* @var Manager
*/
private $moduleManager;

/**
* @param SalesSetupFactory $salesSetupFactory
* @param Config $eavConfig
Expand All @@ -70,6 +76,7 @@ class UpgradeData implements UpgradeDataInterface
* @param OrderFactory $orderFactory
* @param QuoteFactory $quoteFactory
* @param State $state
* @param Manager $moduleManager
*/
public function __construct(
SalesSetupFactory $salesSetupFactory,
Expand All @@ -78,7 +85,8 @@ public function __construct(
AddressCollectionFactory $addressCollFactory,
OrderFactory $orderFactory,
QuoteFactory $quoteFactory,
State $state
State $state,
Manager $moduleManager
) {
$this->salesSetupFactory = $salesSetupFactory;
$this->eavConfig = $eavConfig;
Expand All @@ -87,6 +95,7 @@ public function __construct(
$this->orderFactory = $orderFactory;
$this->quoteFactory = $quoteFactory;
$this->state = $state;
$this->moduleManager = $moduleManager;
}

/**
Expand Down Expand Up @@ -178,27 +187,54 @@ private function convertSerializedDataToJson($setupVersion, SalesSetup $salesSet
*/
public function fillQuoteAddressIdInSalesOrderAddress(ModuleDataSetupInterface $setup)
{
$addressTable = $setup->getTable('sales_order_address');
$updateOrderAddress = $setup->getConnection()
->select()
->joinInner(
['sales_order' => $setup->getTable('sales_order')],
$addressTable . '.parent_id = sales_order.entity_id',
['quote_address_id' => 'quote_address.address_id']
)
->joinInner(
['quote_address' => $setup->getTable('quote_address')],
'sales_order.quote_id = quote_address.quote_id
if (
$this->moduleManager->isEnabled('Magento_ScalableCheckout') ||
$this->moduleManager->isEnabled('Magento_ScalableInventory') ||
$this->moduleManager->isEnabled('Magento_ScalableOms')
Copy link
Contributor

Choose a reason for hiding this comment

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

Well, actually we cannot refer to Magento Commerce modules from Magento Open Source ones.

) {
$addressCollection = $this->addressCollectionFactory->create();
$addressCollection->addFieldToFilter('quote_address_id', ['null' => true]);

/** @var \Magento\Sales\Model\Order\Address $orderAddress */
foreach ($addressCollection as $orderAddress) {
$orderId = $orderAddress->getParentId();
$addressType = $orderAddress->getAddressType();
/** @var \Magento\Sales\Model\Order $order */
$order = $this->orderFactory->create()->load($orderId);
$quoteId = $order->getQuoteId();
$quote = $this->quoteFactory->create()->load($quoteId);
if ($addressType == \Magento\Sales\Model\Order\Address::TYPE_SHIPPING) {
$quoteAddressId = $quote->getShippingAddress()->getId();
$orderAddress->setData('quote_address_id', $quoteAddressId);
} elseif ($addressType == \Magento\Sales\Model\Order\Address::TYPE_BILLING) {
$quoteAddressId = $quote->getBillingAddress()->getId();
$orderAddress->setData('quote_address_id', $quoteAddressId);
}
$orderAddress->save();
}
} else {
$addressTable = $setup->getTable('sales_order_address', 'sales');
$updateOrderAddress = $setup->getConnection()
->select()
->joinInner(
['sales_order' => $setup->getTable('sales_order', 'sales')],
$addressTable . '.parent_id = sales_order.entity_id',
['quote_address_id' => 'quote_address.address_id']
)
->joinInner(
['quote_address' => $setup->getTable('quote_address', 'checkout')],
'sales_order.quote_id = quote_address.quote_id
AND ' . $addressTable . '.address_type = quote_address.address_type',
[]
)
->where(
$addressTable . '.quote_address_id IS NULL'
[]
)
->where(
$addressTable . '.quote_address_id IS NULL'
);
$updateOrderAddress = $setup->getConnection()->updateFromSelect(
$updateOrderAddress,
$addressTable
);
$updateOrderAddress = $setup->getConnection()->updateFromSelect(
$updateOrderAddress,
$addressTable
);
$setup->getConnection()->query($updateOrderAddress);
$setup->getConnection()->query($updateOrderAddress);
}
}
}
}