Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinzui/88969 to your computer and use it in GitHub Desktop.
Save shinzui/88969 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