Skip to content

Instantly share code, notes, and snippets.

@jswanner
Last active August 15, 2024 15:15
Show Gist options
  • Save jswanner/5293719 to your computer and use it in GitHub Desktop.
Save jswanner/5293719 to your computer and use it in GitHub Desktop.
Rolls back migrations in current branch not present in specified branch.
desc 'rolls back migrations in current branch not present in other'
task :rollback_branch_migrations, [:other_branch] do |t, args|
load "#{Dir.pwd}/Rakefile"
branch_migrations = BranchMigrations.new(args.other_branch)
puts ['Rollback the following migrations', branch_migrations, 'y,n? ']
next if %w[no n NO N].include?(STDIN.gets.chomp)
Rake::Task['environment'].invoke
migrate_task = Rake::Task['db:migrate:down']
branch_migrations.each_version do |version|
ENV['VERSION'] = version
migrate_task.execute
end
puts 'Will probably need to discard changes to db/schema.rb'
end
class BranchMigrations
def initialize other_branch
@other_branch = other_branch
end
attr_reader :other_branch
def each_version
filenames.each do |filename|
yield filename.split('_')[0]
end
end
def filenames
list.map { |migration_path| migration_path.match(%r{/(\d+.*)\z})[1] }
end
def list
@list ||= begin
list = %x{git diff #{other_branch} --name-only --diff-filter=A db/migrate/}
list.split.reverse
end
end
def to_ary; filenames end
end
@jswanner
Copy link
Author

Hmm, I haven't seen that error when using this Rake task before. I don't use bundle exec, have you tried running it without that, or using a binstub instead?

@jswanner
Copy link
Author

@olivierlacan I've found the ActiveRecord::ConnectionNotEstablished problem you were running into and it's been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment