Class: Sequel::MigrationReverser

Inherits:
BasicObject
Defined in:
lib/sequel/extensions/pg_enum.rb,
lib/sequel/extensions/migration.rb

Overview

Handles the reversing of reversible migrations. Basically records supported methods calls, translates them to reversed calls, and returns them in reverse order.

Instance Method Summary collapse

Methods inherited from BasicObject

const_missing

Constructor Details

#initializeMigrationReverser

Returns a new instance of MigrationReverser.



168
169
170
# File 'lib/sequel/extensions/migration.rb', line 168

def initialize
  @actions = []
end

Instance Method Details

#reverse(&block) ⇒ Object

Reverse the actions for the given block. Takes the block given and returns a new block that reverses the actions taken by the given block.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/sequel/extensions/migration.rb', line 175

def reverse(&block)
  begin
    instance_exec(&block)
  rescue
    just_raise = true
  end
  if just_raise
    Proc.new{raise Sequel::Error, "irreversible migration method used in #{block.source_location.first}, you may need to write your own down method"}
  else
    actions = @actions.reverse
    Proc.new do
      actions.each do |a|
        pr = a.last.is_a?(Proc) ? a.pop : nil
        # Allow calling private methods as the reversing methods are private
        send(*a, &pr)
      end
    end
  end
end