Drupal gotchas: Never ever ever use anything less than module AND delta to specify blocks

One of the common mistakes I run into is people not specifying enough information when deleting or updating entries in the {blocks} table. You should always use module AND delta to identify the block you’re working with. Delta by itself is not enough, and title is right out.

I learned this the hard way when my blocks suddenly started failing. I jumped back to the last known-good revision, and then stepped forward. All the update functions ran okay, one after the other. When I ran all the update functions after a database refresh, though, things failed. It turned out that another developer had changed the title of a block to a blank string, but left a db_query(“DELETE FROM {blocks} WHERE title=’%s’, $title”); in the function. And since it was called fairly early in our update cycle, it didn’t turn up during the incremental updates from my known-good database.

That took me about 45 minutes to find and fix. Thank goodness for tests and version control!

LESSON: Never ever ever use anything less than module AND delta to specify blocks.

  • “I jumped back to the last known-good revision”

    How do you have things set up that you can just jump back?

  • I remembered the last known good revision well because I had put a bit of effort into making sure that our regression tests all ran (uncovering a number of bugs along the way). Once I figured out the revision number (it was #1947), I used svn update -r 1947 to go back to the revision.

    We test all of our updates against a copy of the production database and all the behavior-related changes are in the source code, so then it was a matter of stepping forward until I found what broke. I started stepping through it incrementally (without refreshing the database from scratch) because I thought that would save me time, but the error didn’t reappear. When I refreshed the database for each update, though… BOOM! turned out it was the very next revision. When I figured out which revision it was and what code was in that revision, it was MUCH easier to figure out what the problem was.

    The other approach is to figure out, based on the symptoms of the current system, what’s going wrong. I had tried that before starting to chase down revisions, and I didn’t get anywhere because the bug was nowhere I would’ve looked myself.

  • jmorahan

    As of Drupal 6, the {blocks} table has a regular old primary key, {blocks}.bid which you can use instead of the module+delta combination that is unfortunately still needed in D5.

  • Someday I’ll get to join you folks in the 21st century! =) I can’t _wait_ to be able to take advantage of _menu_alter…