Sha256: 71ab3f30056012f34e6fadd61c9d1d1aae5a24f7cadd08989e7f5f1301fc2b19

Contents?: true

Size: 1.07 KB

Versions: 52

Compression:

Stored size: 1.07 KB

Contents

class IntegerColumnsToBoolean < ActiveRecord::Migration
  
  class User < ActiveRecord::Base; end
  
  def self.up
    change_column "users", "admin",     :boolean, :default => false, :null => false
    change_column "users", "developer", :boolean, :default => false, :null => false
  end
  
  def self.down
    change_column "users", "admin",     :integer, :limit => 1, :default => 0, :null => false
    change_column "users", "developer", :integer, :limit => 1, :default => 0, :null => false
  end
  
  def self.change_column(table, column, type, options={})
    model_class = table.singularize.camelize.constantize
    
    announce "saving #{model_class} data"
    old_values = model_class.find(:all).map do |model|
      [model.id, model.send("#{column}?")]
    end
    
    remove_column table, column
    add_column table, column, type, options
    
    model_class.reset_column_information
    
    announce "restoring #{model_class} data"
    old_values.each do |(id, value)|
      model = model_class.find(id)
      model.send "#{column}=", value
      model.save
    end
  end
  
end

Version data entries

52 entries across 52 versions & 6 rubygems

Version Path
trusty-cms-1.0.0 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.1.4 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.1.3 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.1.2 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.1.1 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.1.0 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.1.0.rc1 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.1.0.beta db/migrate/006_integer_columns_to_boolean.rb
radiant-1.0.1 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.1.0.alpha db/migrate/006_integer_columns_to_boolean.rb
radiant-1.0.0 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.0.0.rc5 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.0.0.rc4 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.0.0.rc3 db/migrate/006_integer_columns_to_boolean.rb
kajam-1.0.3.rc2 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.0.0.rc2 db/migrate/006_integer_columns_to_boolean.rb
radiant-1.0.0.rc1 db/migrate/006_integer_columns_to_boolean.rb
radiant-rails3-0.1 db/migrate/006_integer_columns_to_boolean.rb
radiantcms-couchrest_model-0.2.4 db/migrate/006_integer_columns_to_boolean.rb
radiantcms-couchrest_model-0.2.2 db/migrate/006_integer_columns_to_boolean.rb