shipperhq / module-shipper

Base ShipperHQ Repo
Open Software License 3.0
21 stars 20 forks source link

Module setup fails during creation of customer address attributes #121

Closed robaimes closed 1 year ago

robaimes commented 2 years ago

Problem:

Attempting to install the Shipper HQ module produces an error and setup cannot complete.

Reproduction:

Additional Info

The exact error message spat out by the command line during bin/magento setup:upgrade is as below:

Unable to apply data patch ShipperHQ\Shipper\Setup\Patch\Data\InstallDestTypeAttributes for module ShipperHQ_Shipper. Original exception message: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction, query was: INSERT INTO `eav_attribute` (`entity_type_id`, `attribute_code`) VALUES (?, ?)

The specific query is

INSERT INTO `eav_attribute` (`entity_type_id`, `attribute_code`) VALUES ('2', 'destination_type')

The issue is present from lines 84 through 89 of Setup\Patch\Data\InstallDestTypeAttributes, and the same issue applies to lines 106 through 112.

Removing the $attribute->save() call allows setup to continue as normal, but without the used_in_forms data. Perhaps this would be better as a separate patch?

ibraheemnabeelfauzi commented 2 years ago

Hello @robaimes could you please clarify why there are two different database in the .env file?

robaimes commented 2 years ago

@ibraheemnabeelfauzi Same database but 2 different connections. I've later realised that this error occurs regardless. Using only a single connection still yields me this error.

ibraheemnabeelfauzi commented 2 years ago

@robaimes thanks and sorry for delay in the response. With that being said, can you please check and see if that's not the case? https://stagebit.com/magento-2/magento-debugging/sqlstatehy000-general-error-1205-lock-wait-timeout-exceeded-try-restarting-transaction-magento/

robaimes commented 2 years ago

@ibraheemnabeelfauzi This is a way around the issue, yes. But the Patch script (in this case) is what is creating the lock. I also wouldn't be able to do this during a production deployment like I can a development environment.

ibraheemnabeelfauzi commented 2 years ago

Thanks @robaimes let me check into this further.

ibraheemnabeelfauzi commented 2 years ago

@robaimes thank you for your patience. I checked with my folks. This is a deadlock, we'll look into changing our code but it may not be the case as we have 1000s of merchant running this without having such issues. We'll have a look and determine a way!

Thank you for raising this!

robaimes commented 2 years ago

Great to hear, thank you 🙂

robaimes commented 1 year ago

@ibraheemnabeelfauzi If it helps, the following patch allows setup to continue as normal:

Patch ```patch diff --git a/src/Setup/Patch/Data/AddAttributesToForm.php b/src/Setup/Patch/Data/AddAttributesToForm.php new file mode 100644 index 0000000..e7f4c1a --- /dev/null +++ b/src/Setup/Patch/Data/AddAttributesToForm.php @@ -0,0 +1,99 @@ +moduleDataSetup = $moduleDataSetup; + $this->customerSetupFactory = $customerSetupFactory; + } + + /** + * @inheritdoc + */ + public static function getDependencies() + { + return []; + } + + /** + * {@inheritdoc} + */ + public static function getVersion() + { + return '1.0.5'; + } + + /** + * Do Upgrade + * @return void + */ + public function apply() + { + /** @var CustomerSetup $customerSetup */ + $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); + + foreach ($this->attributes as $attributeCode) { + // add attribute to form + $attribute = $customerSetup->getEavConfig()->getAttribute( + 'customer_address', + $attributeCode + ); + $attribute->setData('used_in_forms', [ + 'adminhtml_customer_address', + ]); + + $attribute->save(); + } + } + + /** + * @inheritdoc + */ + public function getAliases() + { + return []; + } +} diff --git a/src/Setup/Patch/Data/InstallDestTypeAttributes.php b/src/Setup/Patch/Data/InstallDestTypeAttributes.php index 56b1c79..cd6adde 100644 --- a/src/Setup/Patch/Data/InstallDestTypeAttributes.php +++ b/src/Setup/Patch/Data/InstallDestTypeAttributes.php @@ -82,11 +82,6 @@ class InstallDestTypeAttributes implements DataPatchInterface 'comment' => 'ShipperHQ Address Type' ]; $customerSetup->addAttribute('customer_address', 'destination_type', $destinationTypeAddressAttr); - - // add attribute to form - $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'destination_type'); - $attribute->setData('used_in_forms', ['adminhtml_customer_address']); - $attribute->save(); } $existingvalidationStatusAttribute = $customerSetup->getAttribute('customer_address', 'validation_status'); @@ -104,12 +99,6 @@ class InstallDestTypeAttributes implements DataPatchInterface 'comment' => 'ShipperHQ Address Validation Status' ]; $customerSetup->addAttribute('customer_address', 'validation_status', $validationStatusAddressAttr); - - // add attribute to form - /** @var $attribute */ - $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'validation_status'); - $attribute->setData('used_in_forms', ['adminhtml_customer_address']); - $attribute->save(); } } ```
shqsupport commented 1 year ago

@robaimes We will look into this. Thank you for the submission!

bawwwa commented 1 year ago

Hi @shqsupport

We faced the same issue with the data patch.

Magento 2.4.4 (EE) PHP 8.1

The patch @robaimes provided fixed the issue.

Thanks!

shqsupport commented 1 year ago

Hi @bawwwa,

Thank you for contacting us regarding this issue you are encountering. We are in the process of looking into it. Have a great day!

wsajosh commented 1 year ago

Hi all,

Thanks for raising this issue and providing the patch @robaimes

I've just released 20.52.0 of module-shipper which includes the patch as well as a couple of other fixes. Please update and it should resolve this issue