Saturday, February 24, 2007

[PLUGIN RELEASE] Enhanced Rails Migrations v1.1.0

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

Native rails db migrations are great for most of the teams. 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 a merged code on 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 for a few last months.

The Solution

There is 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 it was tested against the edge rails.

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 plugin contains a sample zero-number migration which queries the DB for the last run migration number and marks all migrations prior to the number as already run 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

6 comments:

gnufied said...

Apparently, there is a problem when one tries to run "rake
db:test:prepare" and is using enhanced migrations plugin.

The error is simply:

Mysql::Error: Table 'simple_markets_test.schema_info' doesn't exist:
SHOW FIELDS FROM schema_info

Since, enhanced migration seem to replace the schema_info
table name and is of course gets loaded before any migration is run.
Still somehow, rake choses to use older schema_info table, which
throws above exception.

For the time being, I circumvented that error, by using rake
test:clone_structure , but any idea, what would be the permanent fix
to this problem.

Ok also, I have observed that, same problem oocurs when using rake test:units.

Val Aleksenko said...

gnufied: The problem was with schema loader and was fixed yesterday. Please pick up the fresh copy of the plugin.

gnufied said...

The problem is still there, although it comes back with "ActiveRecord not missing costant Base!"

Changing your code:
class ActiveRecord::Schema
def self.define(info={}, &block)
instance_eval(&block)

if info[:version]
initialize_schema_information
if Base.connection.select_one("SELECT id FROM #{ActiveRecord::Migrator.schema_info_table_name} WHERE id = #{info[:version]}") == nil
Base.connection.execute("INSERT INTO #{ActiveRecord::Migrator.schema_info_table_name} VALUES(#{info[:version]}, NOW())")
end
end
end
end

To this fixes the problem:
module ActiveRecord
class Schema < Migration
def self.define(info={}, &block)
instance_eval(&block)

if info[:version]
initialize_schema_information
if Base.connection.select_one("SELECT id FROM #{ActiveRecord::Migrator.schema_info_table_name} WHERE id = #{info[:version]}") == nil
Base.connection.execute("INSERT INTO #{ActiveRecord::Migrator.schema_info_table_name} VALUES(#{info[:version]}, NOW())")
end
end
end
end
end

Thanks for cool plugin.

Anonymous said...

How do you roll back a migration? Adding the VERSION flag to the rake migrate command doesn't appear to do anything.

Anonymous said...

I'd also like to know how to migrate backwards - any clues?

Tim Harper said...

rake db:migrate:previous