aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2012-08-06 22:25:01 -0400
committerMarc-Andre Lafortune <github@marc-andre.ca>2012-12-21 13:54:51 -0500
commit65e154f33b54acf40b51082fc5b681ba605015d9 (patch)
tree2db947cc5aea5cc1815a1e13122a23ed68271e78 /activerecord/lib
parent24653c945ad3fdce4cb5890a9cc7565753decda0 (diff)
downloadrails-65e154f33b54acf40b51082fc5b681ba605015d9.tar.gz
rails-65e154f33b54acf40b51082fc5b681ba605015d9.tar.bz2
rails-65e154f33b54acf40b51082fc5b681ba605015d9.zip
Allow revert of whole migration [#8267]
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/migration.rb42
1 files changed, 39 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb
index 14c1fb9ae2..c605c2da7f 100644
--- a/activerecord/lib/active_record/migration.rb
+++ b/activerecord/lib/active_record/migration.rb
@@ -379,7 +379,8 @@ module ActiveRecord
self.verbose = true
self.delegate = new
- # Reverses the migration commands for the given block.
+ # Reverses the migration commands for the given block and
+ # the given migrations.
#
# The following migration will remove the table 'horses'
# and create the table 'apples' on the way up, and the reverse
@@ -399,9 +400,25 @@ module ActiveRecord
# end
# end
#
- # This command can be nested.
+ # Or equivalently, if +TenderloveMigration+ is defined as in the
+ # documentation for Migration:
+ #
+ # require_relative '2012121212_tenderlove_migration'
+ #
+ # class FixupTLMigration < ActiveRecord::Migration
+ # def change
+ # revert TenderloveMigration
+ #
+ # create_table(:apples) do |t|
+ # t.string :variety
+ # end
+ # end
+ # end
#
- def revert
+ # This command can be nested.
+ def revert(*migration_classes)
+ run(*migration_classes.reverse, revert: true) unless migration_classes.empty?
+ if block_given?
if @connection.respond_to? :revert
@connection.revert { yield }
else
@@ -415,12 +432,31 @@ module ActiveRecord
send(cmd, *args, &block)
end
end
+ end
end
def reverting?
@connection.respond_to?(:reverting) && @connection.reverting
end
+ # Runs the given migration classes.
+ # Last argument can specify options:
+ # - :direction (default is :up)
+ # - :revert (default is false)
+ def run(*migration_classes)
+ opts = migration_classes.extract_options!
+ dir = opts[:direction] || :up
+ dir = (dir == :down ? :up : :down) if opts[:revert]
+ if reverting?
+ # If in revert and going :up, say, we want to execute :down without reverting, so
+ revert { run(*migration_classes, direction: dir, revert: true) }
+ else
+ migration_classes.each do |migration_class|
+ migration_class.new.exec_migration(@connection, dir)
+ end
+ end
+ end
+
def up
self.class.delegate = self
return unless self.class.respond_to?(:up)