Sep 212007
 

My current project has a CruiseControl.rb build dedicated to testing our database rollback scripts. Since this does not seem to be a common practice, I thought that I would share our experience.

The setup:

We have one DDL file and one DML file per release. These are written using rails migration syntax, but we hand over only SQL to the client for releasing. The client wants a rollback script for each release to rollback the database schema and data (as much as possible) to the previous release.

The problem:

Rollback scripts are hand created and prone to bugs, so we wanted to test them.

Our solution:

We set up a cruise loop specifically designed to test the rollback script. Our criteria for determining if the rollback was successful is:

  • The schema of the old version matches the schema of the rolled back database
  • The content tables are identical to the old versions. These are tables full of static data that we show on the page. They change with each release as the web pages evolve.
  • We can run the old version of the tests against the rolled back database.

Say the team is currently working on release 3. Here is what the cruise build loop does:

  1. Builds the database to release 2 by running each version of the DDL and DML in succession (eg, DDL v1, DML v1, DDL v2, DML v2)
  2. Dumps the database schema to a file using “rake db:schema:dump”
  3. Copies the static data tables to backup tables, eg: content -> content_last_release
  4. Builds the database to release 3 (DDL v3, DML v3)
  5. Runs the rollback script
  6. Dumps the database schema again and compares it with the saved file from step #2
  7. Checks that the static tables match the backups (content and content_last_release are identical)
  8. Runs the test suite from the release 2 branch on the current database

Notes

When comparing the content tables, it is tempting to write code that pulls the data from both tables as arrays and then does an assert_equal:

content = [..]
content_last_release = [..]
assert_equal content_last_release, content

However, when this assertion fails, the output will be two huge arrays that are mostly similar. It is very hard to look at the output and figure out what is different. We used code like this instead:

content = [..]
content_last_release = [..]
assert_equal [], content - content_last_release
assert_equal [], content_last_release - content

Now, only the differences are displayed when it fails.

The Results

The cruise loop has been incredibly helpful. We found numerous bugs in the rollback script. Additionally, we no longer forget to update it when we make database changes.

Obviously, there is always some data that cannot be rolled back. The rolled back database will not be identical to the database before the upgrade. However, this cruise loop ensures that it is as similar as possible.

  2 Responses to “Automated testing of database rollback scripts in rails”

  1. Looks cool. I’m curious about your last paragraph:

    “Obviously, there is always some data that cannot be rolled back. The rolled back database will not be identical to the database before the upgrade.”

    So does this mean that the cruise loop could be “red” for the duration of a release? Or do you change the assertion to account for the differences?

    Also, in some industries it might be absolutely necessary that seamless rollback is necessary due to regulatory/legal issues. In those scenarios, you’ll have to keep dead data/schema around in some recoverable way.

    Something similar was setup in my last project for testing the ported data, iirc. But I’m not sure roll-back was tested in this fashion. Of course, the DDL/DML breakdown was not release based, so it would have been difficult.

    Thanks for posting this!

  2. Hey Presanna,

    What I meant was if the site is live after the upgrade, then we do not want to remove the collected data. We only want to roll back the changes that were part of the upgrade. The rolled back database will not be identical since it has extra data. This is why the only data that we check is in the static tables.

Sorry, the comment form is closed at this time.