Enhanced Rails Migrations

Enhanced Rails Migrations is a plugin that makes rails development easier in a large team with multiple code branches.

Native rails db migrations are great for most of the teams. The troubles begin when many developers try to add a migration or when code is merged between branches. Since native migrations are based on incremental numbers, the name clashing is frequent. Same happens with merged code on an even larger scale. This plugin was born at the Revolution Health Group team to address the problems with large multi-branched development (see our blog entry - revolutiononrails.blogspot.com/2007/01/db-migrations-usage-at-rhg.html) where it has been successfully used since February ‘07.

The Solution

There are a couple of patches against the Edge rails approaching the problem from different angles. You might want to try them unless you work on 1.1.6 or prefer plugins to handle such things.

The plugin monkey-patches ActiveRecord classes to replace the sequential number based rails migration mechanism with the timestamp based one. That allows to avoid name collisions. In addition, it maintains a tracking table of already run migrations instead of the standard single number schema_info. The table is being used for decision whether to run a migration. The biggest difference from the standard way is that migrations below the current schema version are still applied if they have not been applied yet (not in the tracking table).

Important: See the First Run section for details how to use it in existing projects.

Compatibility

The code has been used mostly on 1.1.6 and has been tested against 1.2.5.

Setup

Installation

Install as any other Rails plugin:

ruby script/plugin install svn://rubyforge.org/var/svn/enhanced-mgrtns/enhanced_migrations

Usage

There is no difference in migration usage - use ‘script/generate migration’ to generate a new migration and ‘rake db:migrate’ for migration.

‘schema_version’ is depreciated. If you need to find the last run migration, use: ‘SELECT MAX(id) FROM migrations_info’

First Run

If you are adding the plugin in a middle of the development process, when some migrations have been created and applied, you need to mark those as already run or ‘rake db:migrate’ would try to apply them again. The project contains a sample zero-number migration which queries the DB for the last run migration number and marks those in the new tracking table. It also deletes the schema_version table. To use it, copy ‘migrations/000_run_migrations_marker.rb’ from the project to your rails ‘db/migrate’ directory.

License

Enhanced Rails Migrations is released under the MIT license.

Support

The plugin RubyForge page is rubyforge.org/projects/enhanced-mgrtns