1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

partial implementation of the command recorder

This commit is contained in:
Aaron Patterson 2010-11-18 14:59:25 -08:00
parent 24174d1b3a
commit 843e319f78
3 changed files with 122 additions and 0 deletions

View file

@ -284,6 +284,8 @@ module ActiveRecord
# In application.rb.
#
class Migration
autoload :CommandRecorder, 'active_record/migration/command_recorder'
class << self
attr_accessor :delegate # :nodoc:
end

View file

@ -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

View file

@ -0,0 +1,72 @@
require "cases/helper"
module ActiveRecord
class Migration
class CommandRecorderTest < ActiveRecord::TestCase
def setup
@recorder = CommandRecorder.new
end
def test_record
@recorder.record :create_table, [:system_settings]
assert_equal 1, @recorder.commands.length
end
def test_inverse
@recorder.record :create_table, [:system_settings]
assert_equal 1, @recorder.inverse.length
@recorder.record :rename_table, [:old, :new]
assert_equal 2, @recorder.inverse.length
end
def test_invert_create_table
@recorder.record :create_table, [:system_settings]
drop_table = @recorder.inverse.first
assert_equal [:drop_table, [:system_settings]], drop_table
end
def test_invert_rename_table
@recorder.record :rename_table, [:old, :new]
rename = @recorder.inverse.first
assert_equal [:rename_table, [:new, :old]], rename
end
def test_invert_add_column
@recorder.record :add_column, [:table, :column, :type, {}]
remove = @recorder.inverse.first
assert_equal [:remove_column, [:table, :column]], remove
end
def test_invert_rename_column
@recorder.record :rename_column, [:table, :old, :new]
rename = @recorder.inverse.first
assert_equal [:rename_column, [:table, :new, :old]], rename
end
def test_invert_add_index
@recorder.record :add_index, [:table, [:one, :two], {:options => true}]
remove = @recorder.inverse.first
assert_equal [:remove_index, [:table, {:column => [:one, :two]}]], remove
end
def test_invert_rename_index
@recorder.record :rename_index, [:old, :new]
rename = @recorder.inverse.first
assert_equal [:rename_index, [:new, :old]], rename
end
def test_invert_add_timestamps
@recorder.record :add_timestamps, [:table]
remove = @recorder.inverse.first
assert_equal [:remove_timestamps, [:table]], remove
end
def test_invert_remove_timestamps
@recorder.record :remove_timestamps, [:table]
add = @recorder.inverse.first
assert_equal [:add_timestamps, [:table]], add
end
end
end
end