What are Setup Scripts in Magento 2 and why you should use them

What-are-Setup-Scripts-in-Magento-2

So you have created your first Magento 2 module.

You have created the registration.php, the module.xml, everything. You have taken good care to see that your code quality is good and you have put comments for every single class and function you’ve developed. Sounds like a perfect module right? One small catch.

Your module requires a table to be created in the database, let’s say “my_first_module_table”. And it has four columns.

Now, whoever downloads your module, and installs it into their Magento installation, has to create that table, having the exact same schema, for your module to work properly. Seems kind of a headache right? So what do you do?

Don’t worry. You don’t have to go to every single client of yours to run the “CREATE TABLE” mySQL command at their system. Magento has provided a way for the modules that you create to modify the schema of the Magento database.

Setup Scripts

Setup scripts are certain classes inside your module which are executed, either once, or whenever your module version is updated. Setup scripts in Magento 2 database configuration files that modify the Magento installation. They are usually used for database configuration but can be used for many other things.Magento supports 4 kinds of setup scripts

Author

Leave a Reply

Your email address will not be published.

Related Posts

Join 40,000+ Magento pros who receive eCommerce insights, tips, and best practices.