From 844d0859353c2e4d6f08308a8e6a50f0898cf36a Mon Sep 17 00:00:00 2001
From: Israel Barreto Sant'Anna <ibsantanna@inf.ufpr.br>
Date: Fri, 21 Feb 2020 11:17:57 -0300
Subject: [PATCH] Fixed update_dspace_ids task and added the languages
 migration task and user role task

---
 lib/tasks/import/learning_objects.rake | 40 +++++++++++----
 lib/tasks/language.rake                | 67 +++++++++++++++++++++++++-
 lib/tasks/user.rake                    | 10 ++++
 3 files changed, 107 insertions(+), 10 deletions(-)
 create mode 100644 lib/tasks/user.rake

diff --git a/lib/tasks/import/learning_objects.rake b/lib/tasks/import/learning_objects.rake
index 3c9a82482..39f2ae190 100644
--- a/lib/tasks/import/learning_objects.rake
+++ b/lib/tasks/import/learning_objects.rake
@@ -134,14 +134,37 @@ namespace :import do
   task :update_dspace_ids => :environment do
     items_not_found = 0
     bitstreams_not_found = 0
-    LearningObject.where("id_dspace ~ '^[0-9]+$'").each do |lo|
-      items_not_found += 1 if !update_id_dspace("items", lo)
-      lo.attachments.each do |att|
-        bitstreams_not_found += 1 if !update_id_dspace("bitstreams", att)
+    loop do
+      begin
+        n_los = LearningObject.with_deleted.where("id_dspace ~ '^[0-9]+$'").count
+        n_atts = LearningObject::Attachment.where("id_dspace ~ '^[0-9]+$'").count
+        p "LearningObjects with legacy id_dspace = #{n_los}"
+        p "Items not found = #{items_not_found}"
+        p "Attachments with legacy id_dspace = #{n_atts}"
+        p "Bitstreams not found = #{bitstreams_not_found}"
+        if n_los > items_not_found
+          LearningObject.with_deleted.where("id_dspace ~ '^[0-9]+$'").find_each do |lo|
+            items_not_found += 1 if !update_id_dspace("items", lo)
+          end
+        elsif n_atts > bitstreams_not_found
+          LearningObject::Attachment.where("id_dspace ~ '^[0-9]+$'").find_each do |att|
+            bitstreams_not_found += 1 if !update_id_dspace("bitstreams", att)
+          end
+        else
+          break
+        end
+      rescue PG::ConnectionBad, Faraday::TimeoutError, Net::HTTP::Persistent::Error, Net::ReadTimeout => e
+        p "Database error, going to sleep"
+        p e
+        p e.class
+        p e.message
+        ActiveRecord::Base.clear_active_connections!
+        # Sleeps for a while to wait database's recovery
+        sleep(60.seconds)
+        # Goes to next iteration to retry
+        next
       end
     end
-    p "Items not found: #{items_not_found}"
-    p "Bitstreams not found: #{bitstreams_not_found}"
   end
 
   private
@@ -166,9 +189,8 @@ namespace :import do
     item = dspace_client.send(obj_type).find(
       id: obj.id_dspace
     )
-    obj.legacy_dspace_id = obj.id_dspace
-    obj.id_dspace = item.id
-    obj.save!
+    old_dspace_id = obj.id_dspace
+    obj.update_attributes(legacy_dspace_id: old_dspace_id, id_dspace: item.id)
     return true
   rescue Dspace::NotFoundError
     p "#{obj_type} id: #{obj.id_dspace} not found"
diff --git a/lib/tasks/language.rake b/lib/tasks/language.rake
index db681d0af..09bb7e045 100644
--- a/lib/tasks/language.rake
+++ b/lib/tasks/language.rake
@@ -49,6 +49,71 @@ namespace :language do
     end
   end
 
+  desc 'Update LearningObjects associations with Languages from 1:N to N:N'
+  task :migration_up => :environment do
+    start = 1
+    finish = 999
+    batch_size = 1000
+    last_id = LearningObject.count > 0 ? LearningObject.with_deleted.last.id : 0
+
+    loop do
+      begin
+        p "start: #{start}, finish: #{finish}"
+        LearningObject.with_deleted.find_each(start: start, finish: finish) do |lo|
+          if (lo.language_id)
+            lo.update_attributes(language_ids: lo.language_id)
+          end
+        end
+      #rescue PG::ConnectionBad => e
+      rescue Exception => e
+        #logger.warn 'Database error, going to sleep'
+        p 'Database error, going to sleep'
+        #logger.error e
+        p e
+        p e.class
+        p e.message
+        ActiveRecord::Base.clear_active_connections!
+        # Sleeps for a while to wait database's recovery
+        sleep(60.seconds)
+        # Goes to next iteration to retry
+        next
+      else
+        start += batch_size - 1
+        finish += batch_size
+        break if start >= last_id
+      end
+    end
+  end
+
+  desc 'Update LearningObjects associations with Languages from N:N to 1:N'
+  task :migration_down => :environment do
+    start = 1
+    finish = 999
+    batch_size = 1000
+    last_id = LearningObject.count > 0 ? LearningObject.with_deleted.last.id : 0
+
+    loop do
+      begin
+        LearningObject.with_deleted.find_each(start: start, finish: start) do |lo|
+          if (lo.language_id)
+            lo.update_attributes(language_id: lo.language_ids.first)
+          end
+        end
+      rescue PG::ConnectionBad => e
+        logger.warn 'Database error, going to sleep'
+        logger.error e
+        # Sleeps for a while to wait database's recovery
+        sleep(10.seconds)
+        # Goes to next iteration to retry
+        next
+      else
+        start += batch_size - 1
+        finish += batch_size
+        break if start >= last_id
+      end
+    end
+  end
+
   def is_valid?(language)
     if default_languages.has_key? language.name and default_languages[language.name] == language.code
       return true
@@ -71,4 +136,4 @@ namespace :language do
         'Outro' => 'Outro'
     }
   end
-end
\ No newline at end of file
+end
diff --git a/lib/tasks/user.rake b/lib/tasks/user.rake
new file mode 100644
index 000000000..5cf6adbe9
--- /dev/null
+++ b/lib/tasks/user.rake
@@ -0,0 +1,10 @@
+namespace :user do
+  desc 'Assign submitter role to users that had the teacher role'
+  task set_submitters: :environment do
+    submitter = Role.submitter
+    Role.teacher.users.each do |user|
+      user.roles << submitter
+      user.save!
+    end
+  end
+end
-- 
GitLab