Mage Check Facebook icon Mage Check Linkedin icon

Magento How to do a setup script

$ If you are in the business of creating your own custom extension (module) for Magento, then chances are that at some point you will need your extension to be able to persist data to database, this is where the installation scripts come in place.

$ Later on, when you upgrade your extension and decide to add few new tables to database or few new columns to database table that was initially created by the original installation extension you will need to look for upgrade script.

$ Finally, if you want to set some initial default values in the database tables of your extension you will need to look for data scripts.

Step 1: Declare Module

Magento 1 (2007 - 2014)

File path: app/code/etc/modules/MageCheck_Tutorial.xml


Magento 2 Open Source (2014 -)

File path: app/code/MageCheck/Tutorial/registration.php


File path: app/code/MageCheck/Tutorial/etc/module.xml


Step 2: Create Configuration Files

Magento 1 (2007 - 2014)

File path: app/code/community/MageCheck/Tutorial/etc/config.xml


Magento 2 Open Source (2014 -)

File path: app/code/MageCheck/Tutorial/etc/config.xml


Step 3: Configure Module and Create Functions

Magento 1 (2007 - 2014)

File path: app/code/community/MageCheck/Tutorial/Model/Resource/Ticket.php


File path: app/code/community/MageCheck/Tutorial/Model/Resource/Ticket/Collection.php


Magento 2 Open Source (2014 -)

To enable custom module manually in Magento 2, two commands needs to be executed from the shell:

php bin/magento module:enable MageCheck_Tutorialphp bin/magento setup:upgrade

First command adds your module to the modules list in app/etc/config.php. Second one installs your module

If module has setup scripts, they are executed and current module version is saved in setup_module table. Magento regulates what needs setup by comparing module versions with what it sees in that table. If module is installing for the first time, Install + Upgrade scripts are triggered. If module was installed before, but module version is increased, only Upgrade scripts are triggered.

To disable module, you will run:

php bin/magento module:disable MageCheck_Tutorial

This will flag module as disabled in config.php.

To uninstall the module, you have to run the following command:

php bin/magento module:uninstall MageCheck_Tutorial

Step 4: Create InstallData

Magento 1 (2007 - 2014)

File path: app/code/community/MageCheck/Tutorial/sql/magecheck_tutorial_setup/install-


Magento 2 Open Source (2014 -)

File path: app/code/MageCheck/Tutorial/Setup/InstallData.php


Step 5: Create UpgradeData

Magento 1 (2007 - 2014)

File path: app/code/community/MageCheck/Tutorial/sql/magecheck_tutorial_setup/upgrade-


Note: Do not forget to modify the version from config.xml file in order to upgrade your script.

Magento 2 Open Source (2014 -)

File path: app/code/MageCheck/Tutorial/Setup/UpgradeData.php


Extra Content (Magento 2 only)

Install or Upgrade Data

Data setup scripts contain entries that the module needs to insert into the database. Attributes that come with Magento by default, 404 and other Cms pages, various default groups and roles, are all examples of data setup. Data setup is executed after Schema setup, they function in a similar fashion.

Recurring (post install)

Recurring script is executed after any module setup. The idea is that Module1 can do something after Module2 and Module3 are installed, if needed. The only example in current Magento 2 is Magento\Indexer\Setup\Recurring class where Magento_Indexer module checks for new defined indexers and adds them to indexer_state table.


This works only with modules installed with Composer. Magento 2 modules can have Uninstall scripts. For example:

This is the sample way to Setup Script in Magento!