aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/migration
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-11-18 14:59:25 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2010-11-19 10:24:15 -0800
commit843e319f78631d056e5018a690d0e16fc4dee619 (patch)
tree6cbb620f6729c20056579564ad0229b12b9d1a0a /activerecord/lib/active_record/migration
parent24174d1b3aa1b8ac4fec95f82f6204e2d095805d (diff)
downloadrails-843e319f78631d056e5018a690d0e16fc4dee619.tar.gz
rails-843e319f78631d056e5018a690d0e16fc4dee619.tar.bz2
rails-843e319f78631d056e5018a690d0e16fc4dee619.zip
partial implementation of the command recorder
Diffstat (limited to 'activerecord/lib/active_record/migration')
-rw-r--r--activerecord/lib/active_record/migration/command_recorder.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb
new file mode 100644
index 0000000000..25b8649350
--- /dev/null
+++ b/activerecord/lib/active_record/migration/command_recorder.rb
@@ -0,0 +1,48 @@
+module ActiveRecord
+ class Migration
+ # ActiveRecord::Migration::CommandRecorder records commands done during
+ # a migration and knows how to reverse those commands.
+ class CommandRecorder
+ attr_reader :commands
+
+ def initialize
+ @commands = []
+ end
+
+ # record +command+. +command+ should be a method name and arguments.
+ # For example:
+ #
+ # recorder.record(:method_name, [:arg1, arg2])
+ def record(*command)
+ @commands << command
+ end
+
+ # Returns a list that represents commands that are the inverse of the
+ # commands stored in +commands+.
+ def inverse
+ @commands.map { |name, args| send(:"invert_#{name}", args) }
+ end
+
+ private
+ def invert_create_table(args)
+ [:drop_table, args]
+ end
+
+ def invert_rename_table(args)
+ [:rename_table, args.reverse]
+ end
+
+ def invert_add_column(args)
+ [:remove_column, args.first(2)]
+ end
+
+ def invert_rename_index(args)
+ [:rename_index, args.reverse]
+ end
+
+ def invert_rename_column(args)
+ [:rename_column, [args.first] + args.last(2).reverse]
+ end
+ end
+ end
+end