Module: ActiveRecord::Migration::Ext::CommandRecorder

Included in:
ActiveRecord::Migration
Defined in:
lib/active_record/migration/ext/command_recorder.rb

Instance Method Summary collapse

Instance Method Details

#recorder_record(&block) ⇒ Object

Records commands returns the recorder.

recorder = recorder_record do
  change_table 'users' do |t|
    t.string 'name'
    t.integer 'number'
  end
end
pp recorder.commands
# => [[:add_column, ["users", "name", :string], nil], [:add_column, ["users", "number", :integer], nil]]
pp recorder.inverse.commands
# =>  [[:remove_column, ["users", "number", :integer], nil], [:remove_column, ["users", "name", :string], nil]]

Based on recorder_revert but without the revert



48
49
50
51
52
53
54
55
# File 'lib/active_record/migration/ext/command_recorder.rb', line 48

def recorder_record(&block)
  command_recorder.tap do |recorder|
    @connection = recorder
    suppress_messages(&block)
    @connection = recorder.delegate
    # recorder.replay(self)
  end
end

#recorder_revert(*_migration_classes, &block) ⇒ Object

ActiveRecord::Migration#revert automatically calls recorder.replay(self) and doesn't even return the recorder; this returns the recorder, letting you to do whatever you want with it.

AKA revert_without_replay

recorder = recorder_revert do
  change_table 'users' do |t|
    t.string 'name'
    t.integer 'number'
  end
end
pp recorder.commands
# =>  [[:remove_column, ["users", "number", :integer], nil], [:remove_column, ["users", "name", :string], nil]]
pp recorder.inverse.commands
# => [[:add_column, ["users", "name", :string], nil], [:add_column, ["users", "number", :integer], nil]]


23
24
25
26
27
28
29
30
31
32
# File 'lib/active_record/migration/ext/command_recorder.rb', line 23

def recorder_revert(*_migration_classes, &block)
  command_recorder.tap do |recorder|
    @connection = recorder
    suppress_messages do
      connection.revert(&block)
    end
    @connection = recorder.delegate
    # recorder.replay(self)
  end
end