Method: ActiveRecord::Migration#up_only

Defined in:
lib/active_record/migration.rb

#up_only(&block) ⇒ Object

Used to specify an operation that is only run when migrating up (for example, populating a new column with its initial values).

In the following example, the new column published will be given the value true for all existing records.

class AddPublishedToPosts < ActiveRecord::Migration[8.1]
  def change
    add_column :posts, :published, :boolean, default: false
    up_only do
      execute "update posts set published = 'true'"
    end
  end
end


933
934
935
# File 'lib/active_record/migration.rb', line 933

def up_only(&block)
  execute_block(&block) unless reverting?
end