Sha256: 31644157d174cd4804f6dcb8284c6790d09cfc0fb4dc116d443cc85ccd6fbfdb

Contents?: true

Size: 1.41 KB

Versions: 51

Compression:

Stored size: 1.41 KB

Contents

class AddUserId < ActiveRecord::Migration[5.0]
  OWNER_LOCK_NAME = :task_owner

  def up
    add_reference :foreman_tasks_tasks, :user, :foreign_key => true

    return if User.unscoped.find_by(:login => User::ANONYMOUS_ADMIN).nil?
    User.as_anonymous_admin do
      user_locks.select(:resource_id).distinct.pluck(:resource_id).each do |owner_id|
        if User.exists?(:id => owner_id)
          tasks = ForemanTasks::Task.joins(:locks).where(:locks => user_locks.where(:resource_id => owner_id))
          tasks.update_all(:user_id => owner_id)
        end
        user_locks.where(:resource_id => owner_id).delete_all
      end
    end
  end

  def down
    User.as_anonymous_admin do
      ForemanTasks::Task.select(:user_id).distinct.pluck(:user_id).compact.each do |user_id|
        ForemanTasks::Task.where(:user_id => user_id).select(:id).find_in_batches do |group|
          group.each { |task| create_lock user_id, task }
        end
      end
    end

    remove_column :foreman_tasks_tasks, :user_id
  end

  private

  def create_lock(user_id, task)
    ForemanTasks::Lock.new(:name          => OWNER_LOCK_NAME,
                           :resource_type => User.name,
                           :resource_id   => user_id,
                           :task_id       => task.id,
                           :exclusive     => false).save!
  end

  def user_locks
    ForemanTasks::Lock.where(:name => OWNER_LOCK_NAME)
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
foreman-tasks-10.0.1 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-10.0.0 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-9.2.3 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-9.2.2 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-9.2.1 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-9.2.0 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-9.1.1 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-9.0.4 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-9.1.0 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-9.0.2 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-9.0.1 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-8.3.3 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-9.0.0 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-8.2.1 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-8.1.4 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-8.3.2 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-8.3.1 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-8.1.3 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-8.3.0 db/migrate/20180927120509_add_user_id.foreman_tasks.rb
foreman-tasks-8.2.0 db/migrate/20180927120509_add_user_id.foreman_tasks.rb