Skip to content

Instantly share code, notes, and snippets.

@outoftime
Created March 26, 2009 14:23
Show Gist options
  • Save outoftime/86128 to your computer and use it in GitHub Desktop.
Save outoftime/86128 to your computer and use it in GitHub Desktop.
#
# Usage:
# rake db:rollback_to_common branch=production
# git checkout -m production
# rake db:migrate
#
namespace :db do
desc 'Rolls back database to common migration state between current branch and another'
task :rollback_to_common do
FileUtils.cd(RAILS_ROOT) do
diff = `git-diff --name-only HEAD #{ENV['branch']} db/migrate`
first_difference = diff.split("\n").sort.first
migration_files = Dir.glob(File.join('db', 'migrate', '*.rb')).sort
common_version_index = migration_files.index(first_difference) - 1
version = migration_files[common_version_index].match(/^db\/migrate\/(\d{14})/)[1]
puts `rake db:migrate VERSION=#{version}`
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment