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 charlieok/90538 to your computer and use it in GitHub Desktop.
Save charlieok/90538 to your computer and use it in GitHub Desktop.
I basically just added an if-else block here so it won't throw an error if there are no migration differences
#
# 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
first_difference_index = migration_files.index(first_difference)
if first_difference_index == nil
puts "No migration difference between this branch and #{ENV['branch']}"
else
common_version_index = first_difference_index - 1
version = migration_files[common_version_index].match(/^db\/migrate\/(\d{14})/)[1]
puts `rake db:migrate VERSION=#{version}`
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment