module-shipper icon indicating copy to clipboard operation
module-shipper copied to clipboard

Module setup fails during creation of customer address attributes

Open robaimes opened this issue 4 years ago • 12 comments

Problem:

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

Reproduction:

  • Install Magento (2.4.4) with preconditions above
  • composer require shipperhq/module-shipper
  • bin/magento setup:upgrade

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?

robaimes avatar Jul 14 '22 15:07 robaimes

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

ibraheemnabeelfauzi avatar Jul 14 '22 19:07 ibraheemnabeelfauzi

@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.

robaimes avatar Jul 14 '22 19:07 robaimes

@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/

ibraheemnabeelfauzi avatar Jul 15 '22 13:07 ibraheemnabeelfauzi

@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.

robaimes avatar Jul 15 '22 13:07 robaimes

Thanks @robaimes let me check into this further.

ibraheemnabeelfauzi avatar Jul 15 '22 13:07 ibraheemnabeelfauzi

@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!

ibraheemnabeelfauzi avatar Jul 18 '22 14:07 ibraheemnabeelfauzi

Great to hear, thank you 🙂

robaimes avatar Jul 18 '22 14:07 robaimes

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

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 @@
+<?php
+/*
+ * ShipperHQ
+ *
+ * @category ShipperHQ
+ * @package ShipperHQ_Shipper
+ * @copyright Copyright (c) 2022 Zowta LTD and Zowta LLC (http://www.ShipperHQ.com)
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
+ * @author ShipperHQ Team [email protected]
+ */
+declare(strict_types=1);
+
+namespace ShipperHQ\Shipper\Setup\Patch\Data;
+
+use Magento\Customer\Setup\CustomerSetup;
+use Magento\Customer\Setup\CustomerSetupFactory;
+use Magento\Framework\DB\Ddl\Table;
+use Magento\Framework\Setup\ModuleDataSetupInterface;
+use Magento\Framework\Setup\Patch\DataPatchInterface;
+
+class AddAttributesToForm implements DataPatchInterface
+{
+    /**
+     * @var ModuleDataSetupInterface $moduleDataSetup
+     */
+    private $moduleDataSetup;
+
+    /**
+     * Customer setup factory
+     * @var CustomerSetupFactory
+     */
+    private $customerSetupFactory;
+
+    /**
+     * @var string[]
+     */
+    private $attributes = [
+        'destination_type',
+        'validation_status',
+    ];
+
+    /**
+     * @param ModuleDataSetupInterface $moduleDataSetup
+     */
+    public function __construct(
+        ModuleDataSetupInterface $moduleDataSetup,
+        CustomerSetupFactory     $customerSetupFactory
+    ) {
+        $this->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();
         }
     }


robaimes avatar Aug 11 '22 10:08 robaimes

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

shqsupport avatar Aug 11 '22 19:08 shqsupport

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!

bawwwa avatar Sep 06 '22 03:09 bawwwa

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!

shqsupport avatar Sep 06 '22 16:09 shqsupport

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

wsajosh avatar Mar 01 '23 12:03 wsajosh