instance method revert

Ruby on Rails 4.0.13

Since v3.2.22.5

Available in: v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

revert(*migration_classes)

Reverses the migration commands for the given block and the given migrations.

The following migration will remove the table ‘horses’ and create the table ‘apples’ on the way up, and the reverse on the way down.

class FixTLMigration < ActiveRecord::Migration
  def change
    revert do
      create_table(:horses) do |t|
        t.text :content
        t.datetime :remind_at
      end
    end
    create_table(:apples) do |t|
      t.string :variety
    end
  end
end

Or equivalently, if TenderloveMigration is defined as in the documentation for Migration:

require_relative '2012121212_tenderlove_migration'

class FixupTLMigration < ActiveRecord::Migration
  def change
    revert TenderloveMigration

    create_table(:apples) do |t|
      t.string :variety
    end
  end
end

This command can be nested.

Parameters

migration_classes rest
Source
# File activerecord/lib/active_record/migration.rb, line 453
    def revert(*migration_classes)
      run(*migration_classes.reverse, revert: true) unless migration_classes.empty?
      if block_given?
        if @connection.respond_to? :revert
          @connection.revert { yield }
        else
          recorder = CommandRecorder.new(@connection)
          @connection = recorder
          suppress_messages do
            @connection.revert { yield }
          end
          @connection = recorder.delegate
          recorder.commands.each do |cmd, args, block|
            send(cmd, *args, &block)
          end
        end
      end
    end

Defined in activerecord/lib/active_record/migration.rb line 453 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Migration

Type at least 2 characters to search.

↑↓ navigate · open · esc close