Sha256: 0678dcad27e3616c93ff4b598779cc1cf665b2f9f4aa9fa47c87a1ea5144da7c

Contents?: true

Size: 1.35 KB

Versions: 177

Compression:

Stored size: 1.35 KB

Contents

class AddCreatedByIdAndUpdatedByIdToProblems < ActiveRecord::Migration[4.2]

  def up
    add_reference :problem_problems, :created_by, references: :users, index: true, null: true
    add_foreign_key :problem_problems, :users, column: :created_by_id

    add_reference :problem_problems, :updated_by, references: :users, index: true, null: true
    add_foreign_key :problem_problems, :users, column: :updated_by_id

    update_problem_user_column_with_a_system_user_so_we_can_set_null_false(:created_by_id)
    change_column_null :problem_problems, :created_by_id, false
  end

  def down
    remove_foreign_key :problem_problems, column: :created_by_id
    remove_reference :problem_problems, :created_by

    remove_foreign_key :problem_problems, column: :updated_by_id
    remove_reference :problem_problems, :updated_by
  end

  private

  class Problem < ActiveRecord::Base
    self.table_name = :problem_problems
  end

  class User < ActiveRecord::Base
    def self.system_user
      User.find_by!(username: "systemuser")
    end
  end

  def update_problem_user_column_with_a_system_user_so_we_can_set_null_false(column_name)
    if Problem.count > 0
      system_user_id = User.system_user.id
      Problem.all.each do |problem|
        next if problem.send(column_name).present?
        problem.update_attributes(column_name => system_user_id)
      end
    end
  end
end

Version data entries

177 entries across 177 versions & 1 rubygems

Version Path
renalware-core-2.1.1 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.1.0 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.167 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.166 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.165 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.164 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.163 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.162 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.161 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.160 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.159 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.158 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.157 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.156 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.155 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.153 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.152 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.151 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.149 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb
renalware-core-2.0.148 db/migrate/20161216090417_add_created_by_id_and_updated_by_id_to_problems.rb