forked from DMPRoadmap/roadmap
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupgrade.rake
More file actions
1346 lines (1177 loc) · 55.2 KB
/
upgrade.rake
File metadata and controls
1346 lines (1177 loc) · 55.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Upgrade tasks for versions < 3.0. See https://github.com/DMPRoadmap/roadmap/releases for information
# on how and when to run each task.
require 'set'
namespace :upgrade do
desc "upgrade to Rails 5, rename task after naming release"
task v2_3_0: :environment do
Rake::Task["upgrade:column_defaults"].execute
end
desc "Upgrade to v2.2.0 Part 1"
task v2_2_0_part1: :environment do
p "Upgrading to v2.2.0 (part 1) ... A summary report will be generated when complete"
p "------------------------------------------------------------------------"
Rake::Task["upgrade:upgrade_2_2_0_identifier_schemes"].execute
Rake::Task["upgrade:upgrade_2_2_0_identifiers"].execute
Rake::Task["upgrade:upgrade_2_2_0_orgs"].execute
Rake::Task["upgrade:results_2_2_0_part1"].execute
end
desc "Upgrade to v2.2.0 Part 2"
task v2_2_0_part2: :environment do
p "Upgrading to v2.2.0 (part 2) ... A summary report will be generated when complete"
p "------------------------------------------------------------------------"
Rake::Task["upgrade:migrate_other_organisation_to_org"].execute
Rake::Task["upgrade:migrate_contributors"].execute
Rake::Task["upgrade:migrate_plan_org_and_funder"].execute
Rake::Task["upgrade:migrate_plan_grants"].execute
Rake::Task["upgrade:results_2_2_0_part2"].execute
end
desc "Upgrade to v2.1.6"
task v2_1_6: :environment do
Rake::Task['upgrade:add_versionable_id_to_question_options'].execute
end
desc "Upgrade to v2.1.3"
task v2_1_3: :environment do
Rake::Task['upgrade:fill_blank_plan_identifiers'].execute
Rake::Task["upgrade:add_reviewer_perm"].execute
Rake::Task["upgrade:add_reviewer_to_existing_admin_perms"].execute
Rake::Task["upgrade:migrate_reviewer_roles"].execute
end
desc "Upgrade to v2.1.2:"
task v2_1_2: :environment do
Rake::Task["upgrade:add_date_question_format"].execute
end
desc "Upgrade to v2.1.0:"
task v2_1_0: :environment do
Rake::Task["data_cleanup:deactivate_orphaned_plans"].execute
end
desc "Upgrade to v2.0.0: Part 1"
task v2_0_0_part_1: :environment do
Rake::Task['upgrade:add_default_values_v2_0_0'].execute
Rake::Task['db:migrate'].execute
Rake::Task['data_cleanup:find_known_invalidations'].execute
puts "If any invalid records were reported above you will need to correct them before running part 2."
end
desc "Upgrade to v2.0.0: Part 2"
task v2_0_0_part_2: :environment do
Rake::Task['data_cleanup:clean_invalid_records'].execute
Rake::Task['upgrade:add_versioning_id_to_templates'].execute
Rake::Task['upgrade:normalize_language_formats'].execute
Rake::Task['stat:build'].execute
end
desc "Upgrade to v1.1.2"
task v1_1_2: :environment do
Rake::Task['upgrade:check_org_contact_emails'].execute
Rake::Task['upgrade:check_for_guidance_multiple_themes'].execute
Rake::Task['upgrade:remove_admin_preferences'].execute
Rake::Task['upgrade:add_other_org'].execute
end
desc "Upgrade to 1.0"
task v1_0_0: :environment do
Rake::Task['upgrade:set_template_visibility'].execute
Rake::Task['upgrade:set_org_links_defaults'].execute
Rake::Task['upgrade:set_template_links_defaults'].execute
Rake::Task['upgrade:set_plan_complete'].execute
Rake::Task['upgrade:stats_api_org_admin'].execute
end
desc "Bug fixes for version v0.3.3"
task v0_3_3: :environment do
Rake::Task['upgrade:fix_question_formats'].execute
Rake::Task['upgrade:add_missing_token_permission_types'].execute
end
desc "Add the missing formattype to the question_formats table"
task fix_question_formats: :environment do
QuestionFormat.all.each do |qf|
case qf.title.downcase
when 'text area'
qf.formattype = :textarea
when 'text field'
qf.formattype = :textfield
when 'radio buttons'
qf.formattype = :radiobuttons
when 'check box'
qf.formattype = :checkbox
when 'dropdown'
qf.formattype = :dropdown
when 'multi select box'
qf.formattype = :multiselectbox
when 'date'
qf.formattype = :date
end
qf.save!
end
if QuestionFormat.find_by(formattype: QuestionFormat.formattypes[:date]).nil?
QuestionFormat.create!({ title: "Date", option_based: false, formattype: QuestionFormat.formattypes[:date] })
end
end
desc "Add the missing token_permission_types"
task add_missing_token_permission_types: :environment do
if TokenPermissionType.find_by(token_type: 'templates').nil?
TokenPermissionType.create!({token_type: 'templates',
text_description: 'allows a user access to the templates api endpoint'})
end
if TokenPermissionType.find_by(token_type: 'statistics').nil?
TokenPermissionType.create!({token_type: 'statistics',
text_description: 'allows a user access to the statistics api endpoint'})
end
end
desc "Set all funder templates (and the default template) to 'public' visibility and all others to 'organisational'"
task set_template_visibility: :environment do
funders = Org.funder.pluck(:id)
Template.update_all(visibility: Template.visibilities[:organisationally_visible])
Template.where(org_id: funders).update_all(visibility: Template.visibilities[:publicly_visible])
Template.default.update(visibility: Template.visibilities[:publicly_visible])
end
desc "Set all orgs.links defaults"
task set_org_links_defaults: :environment do
Org.update_all(links: { 'org': [] })
end
desc "Set all template.links defaults"
task set_template_links_defaults: :environment do
Template.update_all(links: {'funder':[],'sample_plan':[]})
end
desc "Sets completed for plans whose no. questions matches no. valid answers"
task set_plan_complete: :environment do
Plan.all.each do |p|
if p.no_questions_matches_no_answers?
p.update_column(:complete, true) # Avoids updating the column updated_at
end
end
end
desc "Allow Statistics API Usage for Org Admin Users"
task stats_api_org_admin: :environment do
Rake::Task['upgrade:add_missing_token_permission_types'].execute
orgs = Org.where(is_other: false).select(:id)
orgs.each do |org|
unless org.token_permission_types.include?(TokenPermissionType::STATISTICS)
org.grant_api!(TokenPermissionType.where(token_type: 'statistics'))
end
unless org.token_permission_types.include?(TokenPermissionType::PLANS)
org.grant_api!(TokenPermissionType.where(token_type: 'plans'))
end
end
users = User.joins(:perms).where(org_id: orgs).where(api_token: [nil, ''])
users.each do |user|
if user.can_org_admin? && user.api_token.blank?
# Generate the tokens directly instead of via the User.keep_or_generate_token! method so that we do not spam users!!
user.api_token = loop do
random_token = SecureRandom.urlsafe_base64(nil, false)
break random_token unless User.exists?(api_token: random_token)
end
user.save!
end
end
end
desc "Remove Duplicate Answers"
task remove_duplicate_answers: :environment do
## Concat Duplicate Answers
ActiveRecord::Base.transaction do
plan_ids = ActiveRecord::Base.connection.select_all("SELECT a1.plan_id as plan_id FROM Answers a1 INNER JOIN Answers a2 ON a1.plan_id = a2.plan_id AND a1.question_id = a2.question_id WHERE a1.id > a2.id" ).to_a.map{|h| h["plan_id"]}.uniq
plans = Plan.where(id: plan_ids)
plans.each do |plan|
plan.answers.pluck(:question_id).uniq.each do |question_id|
answers = Answer.where(plan_id: plan.id, question_id: question_id).order(:updated_at)
if answers.length > 1 # Duplicates found
puts "found duplicate for plan:#{plan.id}\tquestion:#{question_id} \n\tanswers:[#{answers.map{|answer| answer.id}}]"
new_answer = Answer.new
new_answer.user_id = answers.last.user_id
new_answer.plan_id = plan.id
new_answer.question_id = question_id
new_answer.created_at = answers.last.created_at
num_text = 0
qf = answers.last.question.question_format
puts "\tquestion format #{qf.title}"
if qf.dropdown?
new_answer.question_options << answers.last.question_options.first
puts "\t adding option answers.last.question_options.first.text" unless answers.last.question_options.first.blank?
end
answers.reverse.each do |answer|
if num_text == 0 && answer.text.present? # case first present text
new_answer.text = answer.text
num_text += 1
end
if num_text == 1 && answer.text.present?
text = "<p><strong>ANSWER SAVED TWICE - REQUIRES MERGING</strong></p>"
text += new_answer.text
new_answer.text = text + "<p><strong>-------------</strong></p>" + answer.text
end
new_answer.save
new_answer.reload
answer.notes.each do |note|
note.answer_id = new_answer.id
note.save
end
answer.question_options.each do |op|
unless qf.dropdown?
new_answer.question_options << op unless new_answer.question_options.any? {|aop| aop.id == op.id}
puts "\t adding option #{op.text}"
end
end
answer.destroy
end
new_answer.save
puts "\tsaved new answer with text:\n#{new_answer.text}"
end
end
end
end
end
desc "Remove deprecated themes"
task theme_delete_deprecated: :environment do
if t = Theme.find_by(title:'Project Description') then t.destroy end
if t = Theme.find_by(title:'Project Name') then t.destroy end
if t = Theme.find_by(title:'ID') then t.destroy end
if t = Theme.find_by(title:'PI / Researcher') then t.destroy end
end
desc "Create new Theme list"
task theme_new_themes: :environment do
["Data description", "Data collection", "Metadata & documentation", "Storage & security",
"Preservation", "Data sharing", "Related policies", "Data format", "Data volume",
"Ethics & privacy", "Intellectual Property Rights", "Data repository", "Roles & responsibilities",
"Budget"].each do |t|
Theme.create(title: t)
end
end
desc "Transform existing themes and their associations into new theme list"
task theme_transform: :environment do
ActiveRecord::Base.transaction do
[
{'Budget':'Resourcing'},
{'Data collection':'Data Capture Methods'},
{'Data collection':'Data Quality'},
{'Data description':'Data Description'},
{'Data description':'Data Type'},
{'Data description':'Existing Data'},
{'Data description':'Relationship to Existing Data'},
{'Data format':'Data Format'},
{'Data repository':'Data Repository'},
{'Data sharing':'Expected Reuse'},
{'Data sharing':'Managed Access Procedures'},
{'Data sharing':'Method For Data Sharing'},
{'Data sharing':'Restrictions on Sharing'},
{'Data sharing':'Timeframe For Data Sharing'},
{'Data volume':'Data Volumes'},
{'Ethics & privacy':'Ethical Issues'},
{'Intellectual Property Rights':'IPR Ownership and Licencing'},
{'Metadata & documentation':'Discovery by Users'},
{'Metadata & documentation':'Documentation'},
{'Metadata & documentation':'Metadata '}, # there may be a whitespace here!
{'Preservation':'Data Selection'},
{'Preservation':'Period of Preservation'},
{'Preservation':'Preservation Plan'},
{'Related policies':'Related Policies'},
{'Roles & responsibilities':'Responsibilities'},
{'Storage & security':'Data Security'},
{'Storage & security':'Storage and Backup'},
].each do |pair|
themeto = Theme.find_by(title: pair.keys[0].to_s)
themefrom = Theme.find_by(title: pair.values[0])
Guidance.joins(:themes).where('themes.title' => themefrom.title).each do |gui|
gui.themes.delete(themefrom)
gui.themes << themeto
end
Question.joins(:themes).where('themes.title' => themefrom.title).each do |q|
q.themes.delete(themefrom)
q.themes << themeto
end
end
end
end
desc "Delete migrated themes and their associations"
task theme_remove_migrated: :environment do
ActiveRecord::Base.transaction do
["Data Type", "Existing Data", "Relationship to Existing Data", "Data Quality", "Documentation",
"Discovery by Users", "Data Security", "Data Selection", "Period of Preservation",
"Expected Reuse", "Timeframe For Data Sharing", "Restrictions on Sharing",
"Managed Access Procedures", "Related Policies", "Data Description", "Data Volumes",
"Data Format", "Data Capture Methods", "Metadata ", "Ethical Issues",
"IPR Ownership and Licencing", "Storage and Backup", "Preservation Plan", "Data Repository",
"Method For Data Sharing", "Responsibilities", "Resourcing"].each do |t|
if deltheme = Theme.find_by(title: t) then deltheme.destroy end
end
end
end
desc "Deduplicate multiple associations resulting from Theme merges"
task theme_deduplicate_questions: :environment do
ActiveRecord::Base.transaction do
Question.all.each do |q|
themelist = []
if q.themes.present?
q.themes.each do |qt|
q.themes.delete(qt)
q.themes << qt
end
end
end
end
end
############# Make sure there are no guidances with multiple themes before this step!! #############
desc "Concatenate Guidance which refers to the same Theme as a result of merges"
task single_guidance_for_theme: :environment do
ActiveRecord::Base.transaction do
allthemes = Theme.all
GuidanceGroup.all.each do |group|
if group.guidances.present?
allthemes.each do |theme|
themeguidances = group.guidances.joins(:themes).where('themes.id = ?', theme.id)
if themeguidances.present? && themeguidances.length >= 2
themeguidances.drop(1).each do |guidance|
themeguidances.first.text += '<p>——</p>' + guidance.text
guidance.destroy
end #themeguidances loop
themeguidances.first.save
end
end #allthemes loop
end
end #GuidanceGroup loop
end
end
desc "Remove duplicated non customised template versions"
task remove_duplicated_non_customised_template_versions: :environment do
templates = Template
.select(:id, :family_id, :version, :updated_at)
.group(:family_id, :version, :id)
.order(family_id: :asc, version: :asc, updated_at: :desc)
current_family_id = nil
unique_versions = Set.new
duplicates = []
templates.each do |template|
if current_family_id != template.family_id
current_family_id = template.family_id
unique_versions = Set.new
end
if unique_versions.add?(template.version).nil?
duplicates << template
end
end
current_family_id = nil
version_counter = nil
duplicates.each do |template|
if current_family_id != template.family_id
current_family_id = template.family_id
version_counter = nil
end
num_plans = Plan.where(template_id: template.id).count
if num_plans > 0
version_counter = version_counter.nil? ? -1 : version_counter - 1
unsaved_template = Template.find(template.id)
unsaved_template.version = version_counter
if Template.exists?(customization_of: template.family_id)
puts "template with id: #{template.id} has NOT been ARCHIVED since it had customised templates"
else
puts "template with id: #{template.id} has been ARCHIVED since it had plans associated but no customised templates"
unsaved_template.archived = true
end
unsaved_template.save!
else
Template.destroy(template.id)
puts "template with id: #{template.id} has been REMOVED since it had no plans associated"
end
end
puts "remove_duplicated_non_customised_template_versions DONE"
end
desc "Remove duplicated customised template versions"
task remove_duplicated_customised_template_versions: :environment do
templates = Template
.select(:id, :customization_of, :version, :org_id, :updated_at)
.where('customization_of IS NOT NULL')
.group(:customization_of, :org_id, :version, :id)
.order(customization_of: :asc, org_id: :asc, version: :asc, updated_at: :desc)
generate_compound_key = lambda{ |customization_of, org_id| return "#{customization_of}_#{org_id}" }
current = nil
unique_versions = Set.new
duplicates = []
templates.each do |template|
key = generate_compound_key.call(template.customization_of, template.org_id)
if current != key
current = key
unique_versions = Set.new
end
if unique_versions.add?(template.version).nil?
duplicates << template
end
end
current = nil
version_counter = nil
duplicates.each do |template|
key = generate_compound_key.call(template.customization_of, template.org_id)
if current != key
current = key
version_counter = nil
end
num_plans = Plan.where(template_id: template.id).count
if num_plans > 0
version_counter = version_counter.nil? ? -1 : version_counter - 1
unsaved_template = Template.find(template.id)
unsaved_template.version = version_counter
unsaved_template.archived = true
unsaved_template.save!
puts "template with id: #{template.id} has been ARCHIVED since it had plans associated"
else
Template.destroy(template.id)
puts "template with id: #{template.id} has been REMOVED since it has no plans associated"
end
end
puts "remove_duplicated_customised_template_versions DONE"
end
desc "Remove duplicated template versions"
task remove_duplicated_template_versions: :environment do
Rake::Task['upgrade:remove_duplicated_non_customised_template_versions'].execute
Rake::Task['upgrade:remove_duplicated_customised_template_versions'].execute
end
desc "Org.contact_email is now required, sets any nil values to the helpdesk email defined in dmproadmap.rb initializer"
task check_org_contact_emails: :environment do
email = Rails.configuration.x.organisation.helpdesk_email
name = Rails.configuration.x.organisation.name
if email.present? && name.present?
puts "Searching for Orgs with an undefined contact_email ..."
Org.where("contact_email IS NULL OR contact_email = ''").each do |org|
puts " Setting contact_email to #{email} for #{org.name}"
org.update_attributes(contact_email: email, contact_name: name)
end
else
puts "No helpdesk_email and/or name found in your config/initializers/dmproadmap.rb. Please add them!"
puts "For example:"
puts "config.x.organisation.name = \"Curation Centre\""
puts "config.x.organisation.helpdesk_email = \"help@example.org\""
end
puts "Search complete"
puts ""
end
desc "The system now only allows for one theme selection per guidance, so check for violations"
task check_for_guidance_multiple_themes: :environment do
puts "Searching for guidance with multiple theme selections (you will need to manually reconcile these records) ..."
ids = Guidance.select('guidances.id, count(themes.id) theme_count').
joins(:themes).group('guidances.id').
having('count(themes.id) > 1').pluck('guidances.id')
GuidanceGroup.joins(:guidances).includes(:org).where('guidances.id IN (?)', ids).
distinct.order('orgs.name, guidance_groups.name').each do |grp|
puts " #{grp.org.name} - Guidance group, '#{grp.name}', has guidance with multiple themes"
end
puts "Search complete"
puts ""
end
desc "Remove admin preferences"
task remove_admin_preferences: :environment do
Pref.all.each do |p|
if p.settings.present?
if p.settings['email'].present?
if p.settings['email']['admin'].present?
p.settings['email'].delete('admin')
p.save!
end
end
end
end
end
desc "Add the 'other' org if it is not present."
task add_other_org: :environment do
puts "Checking for existence of an 'Other' org. Unaffiliated users should be affiliated with this org"
# Get the helpdesk email from the dmproadmap.rb initializer
email = Rails.configuration.x.organisation.helpdesk_email
name = Rails.configuration.x.organisation.name
email = 'other.organisation@example.org' unless email.present?
name = 'Helpdesk' unless name.present?
other_org = Org.find_by(is_other: true)
if other_org.present?
puts "Found the 'Other' org (is_other == true)"
else
puts "Could not find the 'Other' org (is_other == true), adding 'Other' org"
other_org = Org.create!({
name: 'Other Organisation',
abbreviation: 'OTHER',
org_type: Org.org_type_values_for(:organisation).min,
contact_email: email,
contact_name: name,
links: {"org": []},
is_other: true,
})
end
unaffiliated = User.where(org_id: nil)
unless unaffiliated.empty?
puts "The following users are not associated with an org. Assigning them to the 'Other' org."
puts unaffiliated.collect(&:email).join(', ')
unaffiliated.update_all(org_id: other_org.id)
end
end
desc "Apply default column values for v2.0.0"
task :add_default_values_v2_0_0 => :environment do
results = GuidanceGroup.where(optional_subset: nil)
puts "Found #{results.length} GuidanceGroups with a null optional_subset ... set values to false"
results.update_all(optional_subset: false)
results = GuidanceGroup.where(published: nil)
puts "Found #{results.length} GuidanceGroups with a null published ... set values to false"
results.update_all(published: false)
results = Note.where(archived: nil)
puts "Found #{results.length} Notes with a null archived ... set values to false"
results.update_all(archived: false)
results = Org.where(is_other: nil)
puts "Found #{results.length} Orgs with a null is_other ... set values to false"
results.update_all(is_other: false)
end
desc "Add verisoning_id to published Templates"
task :add_versioning_id_to_templates => :environment do
safe_require 'text'
safe_require 'progress_bar'
template_count = Template.latest_version.where(customization_of: nil)
.includes(phases: { sections: { questions: :annotations }})
.count
bar = ProgressBar.new(template_count)
# Remove attr_readonly restrictions form these models
Phase.attr_readonly.delete('versionable_id')
Section.attr_readonly.delete('versionable_id')
Question.attr_readonly.delete('versionable_id')
Annotation.attr_readonly.delete('versionable_id')
# Get each of the funder templates...
Template.latest_version.where(customization_of: nil)
.includes(phases: { sections: { questions: :annotations }})
.each do |funder_template|
bar.increment!(1)
Rails.logger.info "Updating versionable_id for Template: #{funder_template.id}"
funder_template.phases.each do |funder_phase|
Rails.logger.info "Updating versionable_id for Phase: #{funder_phase.id}"
funder_phase.update! versionable_id: SecureRandom.uuid
Phase.joins(:template)
.where(templates: { customization_of: funder_template.family_id })
.where(number: funder_phase.number).each do |phase|
if fuzzy_match?(phase.title, funder_phase.title)
phase.update! versionable_id: funder_phase.versionable_id
end
end
funder_phase.sections.each do |funder_section|
Rails.logger.info "Updating versionable_id for Section: #{funder_section.id}"
funder_section.update! versionable_id: SecureRandom.uuid
Section.joins(:template).where(templates: {
customization_of: funder_template.family_id
}).each do |section|
# Prefix the match text with the number. This will make it easier to match
# Sections where the number hasn't changed
text_a = "#{section.number} - #{section.description}"
text_b = "#{funder_section.number} - #{funder_section.description}"
if fuzzy_match?(text_a, text_b)
section.update! versionable_id: funder_section.versionable_id
end
end
funder_section.questions.each do |funder_question|
Rails.logger.info "Updating versionable_id for Question: #{funder_question.id}"
funder_question.update! versionable_id: SecureRandom.uuid
Question.joins(:template).where(templates: {
customization_of: funder_template.family_id
}).each do |question|
# Prefix the match text with the number. This will make it easier to match
# Questions where the number hasn't changed
text_a = "#{question.number} - #{question.text}"
text_b = "#{funder_question.number} - #{funder_question.text}"
if fuzzy_match?(text_a, text_b)
question.update! versionable_id: funder_question.versionable_id
end
end
funder_question.annotations.each do |funder_annotation|
Rails.logger.info "Updating versionable_id for Annotation: #{funder_annotation.id}"
funder_annotation.update! versionable_id: SecureRandom.uuid
Annotation.joins(:template).where(templates: {
customization_of: funder_template.family_id,
}).where(type: funder_annotation.type).each do |ann|
if fuzzy_match?(ann.text, funder_annotation.text)
ann.update! versionable_id: funder_annotation.versionable_id
end
end
end
end
end
end
end
# Add versionable_id to any customized Sections...
Section.joins(:template)
.includes(questions: :annotations)
.where(templates: { id: Template.latest_version.ids })
.where(versionable_id: nil, modifiable: true).each do |section|
section.update! versionable_id: SecureRandom.uuid
section.questions.each do |question|
question.update! versionable_id: SecureRandom.uuid
question.annotations.each do |annotation|
annotation.update! versionable_id: SecureRandom.uuid
end
end
end
end
desc "Update Language abbreviations to use ISO format"
task :normalize_language_formats => :environment do
Language.all.each do |language|
val = LocaleService.to_i18n(string: language.abbreviation).to_s
language.update(abbreviation: val)
end
Template.all.each do |template|
next if template.locale.blank?
template.update(locale: LocaleService.to_i18n(string: template.locale).to_s)
end
Theme.all.each do |theme|
next if theme.locale.blank?
theme.update(locale: LocaleService.to_i18n(string: theme.locale).to_s)
end
end
desc "Adds the Date question format"
task :add_date_question_format => :environment do
unless QuestionFormat.id_for(QuestionFormat.formattypes[:date]).present?
QuestionFormat.create(
title: "Date field",
description: "Date field format",
option_based: false,
formattype: QuestionFormat.formattypes[:date]
)
end
end
desc "Fill blank or nil plan identifiers with plan_id"
task fill_blank_plan_identifiers: :environment do
Plan.where(identifier: ["",nil]).update_all('identifier = id')
end
desc "Adds a new permission for plan reviewers"
task add_reviewer_perm: :environment do
perm_name = 'review_org_plans'
unless Perm.find_by(name: perm_name).present?
Perm.create(name: perm_name)
end
end
desc "adds the new reviewer perm to all existing admin perms"
task add_reviewer_to_existing_admin_perms: :environment do
Perm.change_org_details.users.each do |u|
u.perms << Perm.review_plans
end
end
desc "remove the old reviewer roles and ensure these are marked feedback-enabled"
task migrate_reviewer_roles: :environment do
# remove all roles with nil plan_id
Role.reviewer.where(plan_id: nil).destroy_all
# Pluck all other plan_ids
review_plan_ids = Role.reviewer.pluck(:plan_id).uniq
Plan.where(id: review_plan_ids).update_all(feedback_requested: true)
Role.reviewer.destroy_all
end
desc "generate versionable_ids for "
task add_versionable_id_to_question_options: :environment do
QuestionOption.attr_readonly.delete('versionable_id')
Template.latest_version.where(customization_of: nil)
.includes(phases: { sections: { questions: :question_options }})
.each do |uncustomized|
# update the versionable_id for the canonical and all customized templates
uncustomized.question_options.each do |qo|
vers_id = loop do
rand = SecureRandom.uuid
break rand unless QuestionOption.exists?(versionable_id: rand)
end
qo.update! versionable_id: vers_id
text_a = "#{qo.number} - #{qo.text}"
Question.joins(:question_options)
.where(questions: {versionable_id: qo.question.versionable_id})
.where.not(questions: {id: qo.question_id}) # ensure we exclude the current question
.includes(:question_options)
.each do |q_cust|
q_cust.question_options.each do |qo_cust|
text_b = "#{qo_cust.number} - #{qo_cust.text}"
if fuzzy_match?(text_a, text_b)
qo_cust.update! versionable_id: qo.versionable_id
break
end
end
end
end
end
end
# -------------------------------------------------
# TASKS FOR 2.2.0
desc "run all of the identifier_scheme changes"
task upgrade_2_2_0_identifier_schemes: :environment do
Rake::Task["upgrade:add_new_identifier_schemes"].execute
Rake::Task["upgrade:update_shibboleth_description"].execute
Rake::Task["upgrade:contextualize_identifier_schemes"].execute
end
desc "run all of the identifier changes"
task upgrade_2_2_0_identifiers: :environment do
Rake::Task["upgrade:convert_org_identifiers"].execute
p "--------------------------"
Rake::Task["upgrade:convert_user_identifiers"].execute
end
desc "run all of the org changes"
task upgrade_2_2_0_orgs: :environment do
Rake::Task["upgrade:default_orgs_to_managed"].execute
p "--------------------------"
Rake::Task["upgrade:retrieve_ror_fundref_ids"].execute
end
desc "add the ROR and Fundref identifier schemes"
task add_new_identifier_schemes: :environment do
unless IdentifierScheme.where(name: "fundref").any?
IdentifierScheme.create(
name: "fundref",
description: "Crossref Funder Registry (FundRef)",
active: true
)
end
unless IdentifierScheme.where(name: "ror").any?
IdentifierScheme.create(
name: "ror",
description: "Research Organization Registry (ROR)",
active: true
)
end
end
desc "update the Shibboleth scheme description"
task update_shibboleth_description: :environment do
scheme = IdentifierScheme.where(name: "shibboleth")
if scheme.any?
scheme.first.update(description: "Institutional Sign In (Shibboleth)")
end
end
desc "Contextualize the Identifier Schemes (e.g. which ones are for orgs, etc."
task contextualize_identifier_schemes: :environment do
# Identifier schemes for multiple uses
shib = IdentifierScheme.find_or_initialize_by(name: "shibboleth")
shib.for_users = true
shib.for_orgs = true
shib.for_authentication = true
shib.save
orcid = IdentifierScheme.find_or_initialize_by(name: "orcid")
orcid.for_users = true
orcid.for_contributors = true
orcid.for_authentication = true
orcid.identifier_prefix = "https://orcid.org/"
orcid.save
# Org identifier schemes
ror = IdentifierScheme.find_or_initialize_by(name: "ror")
ror.for_orgs = true
ror.identifier_prefix = "https://ror.org/"
ror.save
fundref = IdentifierScheme.find_or_initialize_by(name: "fundref")
fundref.for_orgs = true
fundref.identifier_prefix = "https://doi.org/10.13039/"
fundref.save
end
desc "migrate the old user_identifiers over to the polymorphic identifiers table"
task convert_user_identifiers: :environment do
p "Transferring existing user_identifiers over to the identifiers table"
p "this may take in excess of 10 minutes depending on the size of your users table ..."
identifiers = UserIdentifier.joins(:user, :identifier_scheme)
.includes(:user, :identifier_scheme)
.where.not(identifier: nil)
.where.not(identifier: '')
Parallel.map(identifiers, in_threads: 8) do |ui|
# Parallel has trouble with ActiveRecord lazy loading
require "org" unless Object.const_defined?("Org")
require "identifier" unless Object.const_defined?("Identifier")
require "identifier_scheme" unless Object.const_defined?("IdentifierScheme")
@reconnected ||= Identifier.connection.reconnect! || true
lookup = Identifier.where(identifiable_id: ui.user_id,
identifiable_type: "User",
identifier_scheme: ui.identifier_scheme)
next if lookup.present?
Identifier.create(identifier_scheme: ui.identifier_scheme, attrs: {}.to_json,
identifiable: ui.user, value: ui.identifier)
end
count = Identifier.where(identifiable_type: "User").length
p "Transfer complete. Orginal user_identifier count #{identifiers.length}, new identifiers count #{count}"
if identifiers.length > count
p ""
p "#{identifiers.length - count} records could not be transferred."
p "This is typically due to the fact that the new identifiers table will automatically"
p "prepend the identifier_scheme.identifier_prefix to the value For example: "
p " '0000-0000-0000-0001' would become 'https://orcid.org/0000-0000-0000-0001'"
p "and your old user_identifiers table may have an entry for both versions"
end
end
desc "migrate the old org_identifiers over to the polymorphic identifiers table"
task convert_org_identifiers: :environment do
p "Transferring existing org_identifiers over to the identifiers table"
p "please wait ..."
identifiers = OrgIdentifier.joins(:org, :identifier_scheme)
.includes(:org, :identifier_scheme)
.where.not(identifier: nil)
.where.not(identifier: '')
.order(id: :desc)
Parallel.map(identifiers, in_threads: 8) do |oi|
# Parallel has trouble with ActiveRecord lazy loading
require "org" unless Object.const_defined?("Org")
require "identifier" unless Object.const_defined?("Identifier")
require "identifier_scheme" unless Object.const_defined?("IdentifierScheme")
@reconnected ||= Identifier.connection.reconnect! || true
lookup = Identifier.where(identifiable_id: oi.org_id,
identifiable_type: "Org",
identifier_scheme: oi.identifier_scheme)
next if lookup.present?
Identifier.create(identifier_scheme: oi.identifier_scheme, attrs: oi.attrs,
identifiable: oi.org, value: oi.identifier)
end
count = Identifier.where(identifiable_type: "Org").length
p "Transfer complete. Orginal org_identifier count #{identifiers.length}, new identifiers count #{count}"
if identifiers.length > count
p ""
p "#{identifiers.length - count} records could not be transferred. Run the following query manually to identify them:"
p " SELECT * FROM org_identifiers WHERE org_id NOT IN ("
p " SELECT identifiers.identifiable_id FROM identifiers "
p " WHERE identifiers.identifier_scheme_id = org_identifiers.identifier_scheme_id AND identifiable_type = 'Org'"
p " );"
p "Then transfer them manually."
end
end
desc "Sets the new managed flag for all existing Orgs to managed = true"
task default_orgs_to_managed: :environment do
Org.all.update_all(managed: true)
end
desc "retrieves ROR ids for each of the Orgs defined in the database"
task retrieve_ror_fundref_ids: :environment do
ror = IdentifierScheme.find_by(name: "ror")
fundref = IdentifierScheme.find_by(name: "fundref")
out = CSV.generate do |csv|
csv << %w[org_id org_name ror_name ror_id fundref_id]
if ExternalApis::RorService.ping
p "Scanning ROR for each of your existing Orgs"
p "The results will be written to tmp/ror_fundref_ids.csv to facilitate review and any corrections that may need to be made."
p "The CSV file contains the Org name stored in your DB next to the ROR org name that was matched. Use these 2 values to determine if the match was valid."
p "You can use the ROR search page to find the correct match for any organizations that need to be corrected: https://ror.org/search"
p ""
orgs = Org.includes(identifiers: :identifier_scheme)
.where(is_other: false).order(:name)
orgs.each do |org|
# If the Org already has a ROR identifier skip it
next if org.identifiers.select { |id| id.identifier_scheme_id == ror.id }.any?
# The abbreviation sometimes causes weird results so strip it off
# in this instance
org_name = org.name.gsub(" (#{org.abbreviation})", "")
rslts = OrgSelection::SearchService.search_externally(search_term: org_name)
next unless rslts.any?
# Just use the first match that contains the search term
rslt = rslts.select { |rslt| rslt[:weight] <= 1 }.first
next unless rslt.present?
ror_id = rslt[:ror]
fundref_id = rslt[:fundref]
if ror_id.present?
ror_ident = Identifier.find_or_initialize_by(identifiable: org,
identifier_scheme: ror)
ror_ident.value = "#{ror.identifier_prefix}#{ror_id}"
ror_ident.save
p " #{org.name} -> ROR: #{ror_ident.value}, #{rslt[:name]}"
end
if fundref_id.present?
fr_ident = Identifier.find_or_initialize_by(identifiable: org,
identifier_scheme: fundref)
fr_ident.value = "#{fundref.identifier_prefix}#{fundref_id}"
fr_ident.save
p " #{org.name} -> FUNDRF: #{fr_ident.value}, #{rslt[:name]}"
end
if ror_id.present? || fundref_id.present?
csv << [org.id, org.name, rslt[:name], ror_ident&.value, fr_ident&.value]
end
end
else
p "ROR appears to be offline or your configuration is invalid. Heartbeat check failed. Refer to the log for more information."
end
end
if out.present?
file = File.open("tmp/ror_fundref_ids.csv", "w")
file.puts out
file.close
end
end
desc "Attempts to migrate other_organisation entries to Orgs"
task migrate_other_organisation_to_org: :environment do
is_other = Org.find_by(is_other: true)
users = is_other.present? ? User.where(org: is_other) : []
if is_other.present?
p "Processing #{users.length} users attached to '#{is_other.name}' #{is_other.id}"
p "this may take more than 15 minutes depending on how many users are in your database"
else
p "No is_other Org defined, so no orgs need to be created!"
end
# Unfortunately can't use the Parallel gem here because we can have collisions
# when creating Orgs