class Sequel::MigrationReverser

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

Public Class Methods

new() click to toggle source
# File lib/sequel/extensions/migration.rb, line 164
def initialize
  @actions = []
end

Public Instance Methods

reverse(&block) click to toggle source

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.

# File lib/sequel/extensions/migration.rb, line 171
def reverse(&block)
  begin
    instance_eval(&block)
  rescue
    just_raise = true
  end
  if just_raise
    Proc.new{raise Sequel::Error, 'irreversible migration method used, you may need to write your own down method'}
  else
    actions = @actions.reverse
    Proc.new do
      actions.each do |a|
        if a.last.is_a?(Proc)
          pr = a.pop
          send(*a, &pr)
        else
          send(*a)
        end
      end
    end
  end
end

Private Instance Methods

add_column(*args) click to toggle source
# File lib/sequel/extensions/migration.rb, line 196
def add_column(*args)
  @actions << [:drop_column, args[0], args[1]]
end
add_index(*args) click to toggle source
# File lib/sequel/extensions/migration.rb, line 200
def add_index(*args)
  @actions << [:drop_index, *args]
end
alter_table(table, &block) click to toggle source
# File lib/sequel/extensions/migration.rb, line 204
def alter_table(table, &block)
  @actions << [:alter_table, table, MigrationAlterTableReverser.new.reverse(&block)]
end
create_enum(name, _) click to toggle source
# File lib/sequel/extensions/pg_enum.rb, line 154
def create_enum(name, _)
  @actions << [:drop_enum, name]
end
create_join_table(*args) click to toggle source
# File lib/sequel/extensions/migration.rb, line 208
def create_join_table(*args)
  @actions << [:drop_join_table, *args]
end
create_table(name, opts=OPTS) click to toggle source
# File lib/sequel/extensions/migration.rb, line 212
def create_table(name, opts=OPTS)
  @actions << [:drop_table, name, opts]
end
create_view(name, _, opts=OPTS) click to toggle source
# File lib/sequel/extensions/migration.rb, line 216
def create_view(name, _, opts=OPTS)
  @actions << [:drop_view, name, opts]
end
rename_column(table, name, new_name) click to toggle source
# File lib/sequel/extensions/migration.rb, line 220
def rename_column(table, name, new_name)
  @actions << [:rename_column, table, new_name, name]
end
rename_table(table, new_name) click to toggle source
# File lib/sequel/extensions/migration.rb, line 224
def rename_table(table, new_name)
  @actions << [:rename_table, new_name, table]
end