This is part of a series on Magento Best Practices. Click here to read more.
Installation scripts allow you to modify the database according to your module’s specific needs. There is an entire series on installation scripts here.
When it comes to optimizing your installation scripts, you should follow these best practices:
- One Setup Per Module
- Always Use The ORM
- Make Installation Scripts Easy to Read
- Separate Script Installation and Data Installation
- Use Installation Scripts Responsibly
One Setup Per Module
It is technically possible to setup multiple setups inside a single module. Magento’s flexibility can sometimes be it’s downfall. You can use any number of resource setup folders in a file, but it’s a best practice to use only one per module.
Always Use The ORM
In the earlier days of Magento, the installation scripts would just use the run() method and dump a query inside of it. Many developers got stuck in that nasty habit. Even after Magento started using their ORM for creating tables in later versions. Developers are stuck in their ways and sometimes it can be hard to move forward. Using the run() method to install tables is easier, but it essentially hard stamps the version of SQL you write it for into place permanently. This prevents you from following Magento’s patches and upgrades to their ORM model.
Make Your Installation Easy to Read
It should be clear what the installation script is doing. Magento is known for heavy abstraction. Don’t do this in the installation script. Just because you can use a custom class for your installation script doesn’t mean you take the work out of the script and into the class. People have gotten use to the idea of looking for installation procedures in the script itself. Don’t get fancy by taking it out of the file.
Separate Installations and Data Insertions
Installation scripts allow you to modify the database. This includes altering tables, creating new tables, and adding attributes to EAV entities. Data installation scripts allow you to create new rows and data for these newly created portions of the database.
I see people manipulate data all the time in their installation scripts. They insert and update data from within the install files. Do it in the data install. Separate your responsibility.
Use Installation Scripts Responsibly
Do you need to refresh the indexes? Clear the cache? Insert 50,000 rows into a new table? Maybe the installation scripts aren’t the best place to do that.
Not every store is built the same and some of these procedures can take hours to perform. If you need to reindex or refresh the cache, set the flag that says so instead. Don’t make the decision without the user’s consent.
If you need to insert a lot of rows into your database, consider writing a Dataflow profile and letting the user insert these records themselves. The last thing you want is a user wondering why the page isn’t loading correctly and they stop the execution themselves. This effectively ruins the installation, your module, and your module’s reputation as well.