Installer and Setup Scripts
Warning
If possible use a data patch instead.
Creating Setup Scripts¶
There are four types of setup scripts, stored in the module's Setup folder:
- InstallSchema: Setup of database table structure, or other entities. Run once when a new module is installed
- InstallData: Populating any new data. Run once when a new module is installed
- UpgradeSchema: Setup of database table structure, or other entities. Run whenever the version in the
module.xml
is greater than the value in thesetup_module
table - UpgradeData: Populating any new data. Run whenever the version in the
module.xml
is greater than the value in thesetup_module
table - Recurring: Run every time
setup:upgrade
is run, regardless of versions
More detail at http://inchoo.net/magento-2/setup-scripts-magento-2/
Debugging Setup Scripts¶
Check if a script should run¶
- If you're using an Install script, check the
setup_module
table. If an entry exists for your module, the Install script will not run - If you're using an Upgrade script, check the
setup_module
table. If the entry version is equal to or greater than the version in yourmodule.xml
, the Upgrade script will not run
Checking if Magento is attempting to run your script¶
- Try setting an xdebug breakpoint at the start of the
install
/upgrade
function - Try adding an
xdebug_break()
call - Try adding a
die()
statement - Make the PHP file invalid, which should make the
setup:upgrade
command fail to complete - Xdebug
(Magento Root)/setup/src/Magento/Setup/Model/Installer.php
'sgetSchemaDataHandler()
function, which attempts to find your install/upgrade script