Sha256: 75cb87857021e48c4fbcf51e2eeaf2d562a14d6560b40e435e951051bfa00728

Contents?: true

Size: 1.69 KB

Versions: 6

Compression:

Stored size: 1.69 KB

Contents

module ActiveRecord
  module PGEnum
    register :command_recorder do
      require "active_record/migration/command_recorder"
      ActiveRecord::Migration::CommandRecorder.include CommandRecorder
    end

    # 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]
    module CommandRecorder
      def create_enum(*args, &block)
        record(:create_enum, args, &block)
      end

      def drop_enum(*args, &block)
        record(:drop_enum, args, &block)
      end

      def add_enum_value(*args, &block)
        record(:add_enum_value, args, &block)
      end

      private

      def invert_create_enum(args)
        [:drop_enum, args]
      end

      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

      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
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
activerecord-pg_enum-1.0.5 lib/active_record/pg_enum/command_recorder.rb
activerecord-pg_enum-1.0.4 lib/active_record/pg_enum/command_recorder.rb
activerecord-pg_enum-1.0.3 lib/active_record/pg_enum/command_recorder.rb
activerecord-pg_enum-1.0.2 lib/active_record/pg_enum/command_recorder.rb
activerecord-pg_enum-1.0.1 lib/active_record/pg_enum/command_recorder.rb
activerecord-pg_enum-1.0.0 lib/active_record/pg_enum/command_recorder.rb