module ActiveRecord::PGEnum::CommandRecorder

ActiveRecord::Migration::CommandRecorder is a class used by reversible migrations. It captures the forward migration commands and translates them into their inverse by way of some simple metaprogramming.

The Migrator class uses CommandRecorder during the reverse migration instead of the connection object. Forward migration calls are translated to their inverse where possible, and then forwarded to the connetion. Irreversible migrations raise an exception.

Known schema statement methods are metaprogrammed into an inverse method like so:

create_table => invert_create_table

which returns:

[:drop_table, args.first]

Public Instance Methods

add_enum_value(*args, &block) click to toggle source
# File lib/active_record/pg_enum/command_recorder.rb, line 33
def add_enum_value(*args, &block)
  record(:add_enum_value, args, &block)
end
create_enum(*args, &block) click to toggle source
# File lib/active_record/pg_enum/command_recorder.rb, line 25
def create_enum(*args, &block)
  record(:create_enum, args, &block)
end
drop_enum(*args, &block) click to toggle source
# File lib/active_record/pg_enum/command_recorder.rb, line 29
def drop_enum(*args, &block)
  record(:drop_enum, args, &block)
end
rename_enum(name, options = {}) click to toggle source
# File lib/active_record/pg_enum/command_recorder.rb, line 37
def rename_enum(name, options = {})
  record(:rename_enum, [name, options])
end
rename_enum_value(type, options = {}) click to toggle source
# File lib/active_record/pg_enum/command_recorder.rb, line 41
def rename_enum_value(type, options = {})
  record(:rename_enum_value, [type, options])
end

Private Instance Methods

invert_add_enum_value(args) click to toggle source
# File lib/active_record/pg_enum/command_recorder.rb, line 67
def invert_add_enum_value(args)
  raise ActiveRecord::IrreversibleMigration, "ENUM values cannot be removed once added. Drop and Replace it instead at your own risk."
end
invert_create_enum(args) click to toggle source
# File lib/active_record/pg_enum/command_recorder.rb, line 47
def invert_create_enum(args)
  [:drop_enum, args]
end
invert_drop_enum(args) click to toggle source
# File lib/active_record/pg_enum/command_recorder.rb, line 62
def invert_drop_enum(args)
  raise ActiveRecord::IrreversibleMigration, "drop_enum is only reversible if given a list of values" unless args.length > 1
  [:create_enum, args]
end
invert_rename_enum(args) click to toggle source
# File lib/active_record/pg_enum/command_recorder.rb, line 58
def invert_rename_enum(args)
  [:rename_enum, [args.last[:to], to: args.first]]
end
invert_rename_enum_value(args) click to toggle source
# File lib/active_record/pg_enum/command_recorder.rb, line 51
def invert_rename_enum_value(args)
  type, args = args
  reversed   = %i[from to].zip(args.values_at(:to, :from))

  [:rename_enum_value, [type, Hash[reversed]]]
end