9

I've been banging my head for a few hours now. I have setup a small Magento 2 module. It is enabled everything is working except the Setup scripts never run. Actually from my diagnosis right now, they aren't even required in the setup:upgrade process.

If it could be of any help here are the module configuration files:

registration.php:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Wizbusiness_CustomerSubscriptions',
    __DIR__
);

etc/module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
                <module name="Wizbusiness_CustomerSubscriptions" setup_version="0.0.9" />
</config>

Setup/InstallData.php:

<?php
namespace Wizbusiness\CustomerSubscriptions\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        /** @var CustomerSetup $customerSetup */
        $customerSetup->addAttribute(
            Customer::ENTITY,
            'subscribedtags',
            [
                'type' => 'text',
                'input' => 'multiselect',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_GLOBAL,
                'label' => 'Subscribed Tags',
                'required' => 0,
                'system' => 0, // <-- important, otherwise values aren't saved.
                               // @see Magento\Customer\Model\Metadata\CustomerMetadata::getCustomAttributesMetadata()
                'position' => 100,
                'filterable' => true,
                'option' => [ 'values' => ['Test', 'Test2', 'Test3' ] ]
            ]
        );

        $used_in_forms=array();
        $used_in_forms[]="adminhtml_customer";
        $used_in_forms[]="checkout_register";
        $used_in_forms[]="customer_account_create";
        $used_in_forms[]="customer_account_edit";
        $used_in_forms[]="adminhtml_checkout";
        $customerSetup->getEavConfig()->getAttribute('customer', 'subscribedtags')
            ->setData('used_in_forms', [$used_in_forms])
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_user_defined", 1)
            ->save();
        $setup->endSetup();
    }
}

Also a smaller test in Setup/Recurring.php:

<?php
namespace Wizbusiness\CustomerSubscriptions\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class Recurring implements InstallSchemaInterface {

    protected $logger;

    public function __construct(
        \Psr\Log\LoggerInterface $loggerInterface
    ) {
        $this->logger = $loggerInterface;
    }

    public function install( SchemaSetupInterface $setup, ModuleContextInterface $context ) {
        touch("<path here>/recurring.txt");
        $this->logger->debug('This is running');
    }
}

If anybody knows what's up, please help me!

1
  • Just in case it helps someone: my issue was that I was using class InstallSchema in InstallData.php instead of class InstallData.
    – bfontaine
    Feb 24, 2021 at 17:17

6 Answers 6

5

Install scripts only run when the module is initially installed. To have it run again, you need to remove that module's row from the setup_module table with a query like below:

DELETE FROM setup_module WHERE module=Module_Name

1
  • Maked as the answer even tough it wasn't the solution to my problem as this is the most common trouble seen. Aug 19, 2016 at 4:30
3

Same problem, I found the problem will appear when the module name have two or more capital letters, like Vendor\CapitalModule. I had set the folder name capitalization, namespace and class name is right too, but the Setup scripts still never run. Strange problem OTZ

I solved the problem by changing the module name to only one capital letter, like changing Vendor\CapitalModule to Vendor\Capitalmodule, then the Setup InstallSchema scripts will run.

2

I've finally found a solution. I renamed the module folder to match it's name(capitalization included) and now the scripts are running as they should.

1
  • For me, this has the solution. Dec 12, 2016 at 15:18
1

i found that i was missing setup_version in etc/module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Name_ModuleName" setup_version="0.0.1">
</module>
0

Let's see this: https://www.mageplaza.com/magento-2-module-development/magento-2-how-to-create-sql-setup-script.html, do not do it yourself. I think you should coppy the names, paths, do not write by hand

0

I am having the same issue in my custom module. The issue was not the updated namespace of InstallSchema class.

I updated and it works

namespace Customvendorname\CustomModuleName\Setup;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.