Skip to content

Instantly share code, notes, and snippets.

@Dounx
Created November 16, 2020 08:16
Show Gist options
  • Save Dounx/e6ee5abfcc91d2577914f0e3797ce790 to your computer and use it in GitHub Desktop.
Save Dounx/e6ee5abfcc91d2577914f0e3797ce790 to your computer and use it in GitHub Desktop.
def rollback(migrations_path)
migrations_paths_was = ActiveRecord::Migrator.migrations_paths
ActiveRecord::Migrator.migrations_paths = [migrations_path]
ActiveRecord::Tasks::DatabaseTasks.check_target_version
scope = ENV["SCOPE"]
verbose_was, ActiveRecord::Migration.verbose = ActiveRecord::Migration.verbose, ActiveRecord::Tasks::DatabaseTasks.send(:verbose?)
ActiveRecord::Base.connection.migration_context.down(ActiveRecord::Tasks::DatabaseTasks.target_version) do |migration|
scope.blank? || scope == migration.scope
end
ActiveRecord::Base.clear_cache!
ensure
ActiveRecord::Migrator.migrations_paths = migrations_paths_was - [migrations_path]
ActiveRecord::Migration.verbose = verbose_was
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment