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 165 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 172 def reverse(&block) begin instance_exec(&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| 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 194 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 198 def add_index(*args) @actions << [:drop_index, *args] end
alter_table(table, &block)
click to toggle source
# File lib/sequel/extensions/migration.rb, line 202 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 173 def create_enum(name, _) @actions << [:drop_enum, name] end
create_join_table(*args)
click to toggle source
# File lib/sequel/extensions/migration.rb, line 206 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 210 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 214 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 218 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 177 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 222 def rename_table(table, new_name) @actions << [:rename_table, new_name, table] end