class DeleteJournalsActivity < ActiveRecord::Migration
  def up
    UserActivity.all.each do |activity|
      if activity.act_type == 'JournalsForMessage'
        if activity.act
          unless activity.act.m_parent_id.nil?
            parent_act = UserActivity.where("act_id = #{activity.act.m_parent_id} and act_type='JournalsForMessage' and container_type='Principal'").first
            if parent_act
              parent_act.created_at = activity.act.parent.children.maximum("created_on")
              parent_act.save
              activity.destroy
            end
          end
        else
          activity.destroy
        end
      end
    end

    PrincipalActivity.all.each do |activity|
      if activity.principal_act_type == 'JournalsForMessage'
        if activity.principal_act
          unless activity.principal_act.m_parent_id.nil?
            parent_act = PrincipalActivity.where("principal_act_id = #{activity.principal_act.m_parent_id} and principal_act_type='JournalsForMessage'").first
            if parent_act
              parent_act.created_at = activity.principal_act.parent.children.maximum("created_on")
              parent_act.save
              activity.destroy
            end
          end
        else
          activity.destroy
        end
      end
    end
  end

  def down
  end
end
