Sha256: 689d17c32ccdc35360d31dce5f35fb647ba66b302353afcb82c69ec2329fb574

Contents?: true

Size: 386 Bytes

Versions: 2

Compression:

Stored size: 386 Bytes

Contents

class AddApiKeyToUsers < ActiveRecord::Migration
  def up
    add_column :users, :api_key, :string, limit: 32
    
    User.where('api_key IS NULL').find_each do |user|
      begin
        user.api_key = SecureRandom.uuid.tr('-', '')
      end while User.where(api_key: user.api_key).any?
      
      user.save!
    end
  end
  
  def down
    remove_column :users, :api_key
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
voluntary-0.7.1 db/migrate/20150808155719_add_api_key_to_users.rb
voluntary-0.7.0 db/migrate/20150808155719_add_api_key_to_users.rb