Force-run module install script in Magento 2
-
22-01-2021 - |
Question
I have added some install data to my module (EAV attributes), and it doesn't look like it's working. However, I don't seem to get any relevant errors in the logs, and am a bit lost on whether the install script ever ran in the first place. I tried two ways to run it:
- Disable/enable module
- Running setup:upgrade
Is there a way to force-run just my install script? If not, does setup:upgrade
always run it, and if so, which log should I be looking at?
Edit: Here is the relevant part of the install data script:
$customerSetup->addAttribute(Customer::ENTITY, 'some_action_timestamp', [
'type' => 'int',
'label' => 'Some action timestamp',
'input' => 'text',
'required' => false,
'visible' => false,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'some_action_timestamp')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],
]);
$attribute->save();
Solution
If you are trying to run installation scripts for a "local" module (not installed with composer) and the module is already installed, you should:
- Remove module installation registry from database, in the
setup_module
table. - Re-run
bin/magento setup:upgrade
This will execute installation scripts as if the module wasn't previously installed.
OTHER TIPS
Thanks to @Dani I was able to resolve this. I had a module install that went bad and I was getting:
Table 'magento.mgz_ninjamenus_menu' doesn't exist
I use docker and so here is the complete list of commands I used to help this (might help a Magento noob):
#view list docker container names
docker ps --format "{{.Names}}"
#connect to the docker instance
docker exec -it name_of_mysql_container bash
mysql -umagento -pmagento magento
use magento;
select * from setup_module;
#find the name of the module in the displayed list
delete from setup_module where module = 'module_name_from_the_query_above';
#you should see something like:
# Query OK, 1 row affected (0.00 sec)
exit
exit
#now run your own flush commands including:
# bin/magento setup:upgrade
i realized there's no record in setup_module table (neither install script does run for your module) unless you define setup_version attribute into < module > element of etc/module.xml
/magento v2.3.5p1/