Thursday, December 21, 2006

Rake: db:migration:conflict

This task originally written by Stephen Chu.

If you are using migrations on a large team, migration conflicts can and do occur. To help avoid conflicts you can add the following task to your codebase and run it after updating and before checking in (hopefully in your commit task).
namespace :db do
namespace :migration do
desc "After downloading migrations from server, check to see if we have conflicting migration numbers."
task :conflict do
existing_migration_numbers = []
Dir.entries("#{RAILS_ROOT}/db/migrate").each do |entry|
migration_number = entry.scan(/^\d+/).first
next if migration_number.nil?
if existing_migration_numbers.include? migration_number
raise ArgumentError, "Migration #{migration_number} is already checked in on the server. Verify your migration numbers."
end
existing_migration_numbers << migration_number
end
end
end
end

2 comments:

  1. Anonymous7:31 PM

    Cool, but why an ArgumentError?

    ReplyDelete
  2. Anonymous9:25 PM

    Good point. A simple raise "reason" is probably good enough.

    ReplyDelete

Note: Only a member of this blog may post a comment.