-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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