# desc “Explaining what the task does” # task :klastera do # # Task goes here # end

namespace :klastera do

namespace :seed do

  desc "Move cluster_id to cluster_entities"
  task :cluster_entities, [:entity] => :environment do |task,args|
    begin
      klass = args.entity.constantize
      ActiveRecord::Base.transaction do
        klass.where.not(cluster_id: nil).each do |entity|
          if entity.cluster.blank?
            puts "Cluster ID #{entity.cluster_id} was not found!"
            puts "skip..."
            next
          end
          Klastera::ClusterEntity.create(entity: entity, cluster: entity.cluster)
        end
      end
    rescue NameError => ne
      puts "NameError Exception: #{ne}"
    rescue ActiveRecord::StatementInvalid => asi
      puts "ActiveRecord::StatementInvalid Exception #{asi}"
    end
  end

end

end