ActiveRecord Migrations

Flashcard 3 of 6

We want to add a new column to mark users as admins. Since we want to avoid having null values for this boolean field, we need to backfill the value for all existing users.

Are there any potential issues with the following migration?

class AddAdminFlagToUsers < ActiveRecord::Migration
  def up
    add_column :users, :admin, :boolean, default: false
    User.update_all(admin: false)
    change_column_null :users, :admin, false
  end

  def down
    remove_column :users, :admin
  end
end

Answer:

Reveal Answer