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.

:nocov:

Public Class Methods

new() click to toggle source
# File lib/sequel/extensions/migration.rb, line 180
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 187
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

Private Instance Methods

add_column(*args) click to toggle source
# File lib/sequel/extensions/migration.rb, line 209
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 213
def add_index(*args)
  @actions << [:drop_index, *args]
end
alter_table(table, &block) click to toggle source
# File lib/sequel/extensions/migration.rb, line 217
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 187
def create_enum(name, _)
  @actions << [:drop_enum, name]
end
create_join_table(*args) click to toggle source
# File lib/sequel/extensions/migration.rb, line 221
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 225
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 229
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 233
def rename_column(table, name, new_name)
  @actions << [:rename_column, table, new_name, name]
end
rename_enum(old_name, new_name) click to toggle source
# File lib/sequel/extensions/pg_enum.rb, line 191
def rename_enum(old_name, new_name)
  @actions << [:rename_enum, new_name, old_name]
end
rename_table(table, new_name) click to toggle source
# File lib/sequel/extensions/migration.rb, line 237
def rename_table(table, new_name)
  @actions << [:rename_table, new_name, table]
end