From 315ad6be47119302ede60b44617989da9d729049 Mon Sep 17 00:00:00 2001 From: moveson Date: Mon, 11 Dec 2023 23:43:45 -0700 Subject: [PATCH] Rake task to merge organizations --- lib/tasks/merge_organizations.rake | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/tasks/merge_organizations.rake diff --git a/lib/tasks/merge_organizations.rake b/lib/tasks/merge_organizations.rake new file mode 100644 index 000000000..b60126b99 --- /dev/null +++ b/lib/tasks/merge_organizations.rake @@ -0,0 +1,30 @@ +# require "active_record" + +desc "merges one organization into another" +task :merge_organizations, [:disappearing_organization_id, :surviving_organization_id] => :environment do |_, args| + disappearing_organization = Organization.find_by(id: args.disappearing_organization_id) + surviving_organization = Organization.find_by(id: args.surviving_organization_id) + + abort if disappearing_organization.nil? || surviving_organization.nil? + + puts "This will merge #{disappearing_organization.name} into #{surviving_organization.name}" + puts "Are you sure you want to do this? (y/n)" + answer = STDIN.gets.chomp + abort unless answer.downcase == "y" + + relevant_models = [ + Course, + CourseGroup, + EventGroup, + EventSeries, + Lottery, + ResultsCategory, + ResultsTemplate, + Stewardship, + ] + + relevant_models.each do |model| + puts "Merging #{model.table_name.titleize.pluralize}" + model.where(organization: disappearing_organization).update_all(organization_id: surviving_organization.id) + end +end