Sha256: c751876e7e5028e8c1b76e1c68db553f2b4a85047288842554ce537cf1411b05

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

class AddUserOption < ActiveRecord::Migration
  def self.up
    add_column :backlogs, :enable_users, :boolean
    add_column :estimates, :user_id, :integer, :references => :users
    add_column :works, :user_id, :integer, :references => :users
    change_column_null :works, :completed_at, true
    started_tasks = Task.find(:all, :conditions => 'work_started_at IS NOT NULL')
    started_tasks.each do |task|
      started_work = Work.new(:task_id => task.id, :started_at => task.work_started_at)
      started_work.save!
    end
    remove_column :tasks, :work_started_at
  end
  
  def self.down
    add_column :tasks, :work_started_at, :datetime
    started_works = Work.find(:all, :conditions => 'completed_at IS NULL')
    started_works.each do |work|
      task = Task.find(work.task_id)
      task.work_started_at = work.started_at
      task.save!
      work.destroy
    end
    change_column_null :works, :completed_at, false
    remove_column :works, :user_id
    remove_column :estimates, :user_id
    remove_column :backlogs, :enable_users
  end
  
  def self.change_column_null(table_name, column_name, null, default = nil) #:nodoc: 
    unless null || default.nil? 
      execute("UPDATE #{table_name} SET #{column_name}=#{quote(default)} WHERE #{column_name} IS NULL") 
    end 
    execute("ALTER TABLE #{table_name} ALTER #{column_name} #{null ? 'DROP' : 'SET'} NOT NULL") 
  end
  
end

class Task < ActiveRecord::Base
end

class Work < ActiveRecord::Base
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
backlog-0.0.0 db/migrate/015_add_user_option.rb
backlog-0.0.1 db/migrate/015_add_user_option.rb
backlog-0.0.2 db/migrate/015_add_user_option.rb
backlog-0.0.4 db/migrate/015_add_user_option.rb