Drupal update scripts are a great way of automating database updates. They are extremely important to contributed modules when changing the database schema and updating existing data to the new schema. Specifically I will be discussing using update scripts in your custom modules as part of deploying new features. I'm not saying update scripts should replace using the Features module, in fact I use them together, update scripts are great for things that might not be managed in features. This will be the first in a two-part series that will cover using Drupal update scripts.
- Why you should write them
- Real examples of how to write them
Arguments Against Update Scripts
First off, the top complaints I've seen about having to write update scripts are:
- It takes longer to write the script then it does to do it manually in the browser
- There isn't an easy way to do X
- Learn to do it later, just want to get this done
Why You Should Write Update Scripts
Writing Update Scripts Improves Quality
It takes longer to write the script then it does to do it manually in the browser
This argument is actually true, but I'll tell you why it doesn't matter.
- By doing it manually in the browser, you risk missing a critical step. This leads to you having to spend more time debugging the issue later.
- Hopefully you have a deployment process for staging changes for approval before deploying them to the live site; without an update script now you have to perform those changes manually twice, doubling (or tripling if you have more then 2 stages) your time.
- Spending the extra time to write an update script allows you to automate those changes, which allows you to test the update script in development and perform quality assurance, which makes everybody more confident and stress-free when deploying those changes to production.
Writing Update Scripts is a Learning Tool
There isn't an easy way to do X
There are some things that aren't as simple as calling a function or two, and sometimes require you to call some internal functions or writing a helper function. This shouldn't be an excuse not to write update scripts. While I've been developing website for a lot longer, I'm still fairly new to Drupal. I attribute my fast growth in Drupal to 1) my amazing co-workers and 2) writing update scripts. Writing update scripts forces you to learn some of the Drupal internals, which helps you learn and retain that knowledge. Between Google and the Drupal API site you can learn of better and easier ways of doing things.
Learn to do it later, just want to get this done
I heard the "do it quick-and-dirty now and go back and do it right later" excuse a lot at my previous job. Not only does "later" never happen, but it was a real drain to my motivation and job satisfaction. Most people like to take pride in their work and in doing a good job, and I don't think anybody can describe 'quick-and-dirty' as doing a good job. Metal Toad holds high standards for quality, and so it is much easier to get the time to do things the right way. Obviously there are some times when you need to get something done to meet a deadline or fix a critical issue, but the bottom line is don't push it off. In fact, I would say that if you have to do something that is less then ideal to meet a deadline, you should do that and immediately schedule time to look into a more ideal solution.
For Next Time
In my next post I will give real examples of update scripts I've used in projects. Is there something you needed to do, but didn't know how to do in an update script? Let me know and I will try and include it in my next post of examples.