-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
139 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
namespace :cleanup_orphaned_users do | ||
desc 'Remove users that do not have a role' | ||
task find_orphaned_users: [:environment] do | ||
admin_orphaned_users = User.joins("LEFT JOIN admin_roles ON users.role_id = admin_roles.id AND users.role_type = 'AdminRole'") | ||
.where(admin_roles: { id: nil }, role_type: 'AdminRole') | ||
|
||
greeter_orphaned_users = User.joins("LEFT JOIN greeter_roles ON users.role_id = greeter_roles.id AND users.role_type = 'GreeterRole'") | ||
.where(greeter_roles: { id: nil }, role_type: 'GreeterRole') | ||
|
||
site_coordinator_orphaned_users = User.joins("LEFT JOIN site_coordinator_roles ON users.role_id = site_coordinator_roles.id AND users.role_type = 'SiteCoordinatorRole'") | ||
.where(site_coordinator_roles: { id: nil }, role_type: 'SiteCoordinatorRole') | ||
|
||
client_success_orphaned_users = User.joins("LEFT JOIN client_success_roles ON users.role_id = client_success_roles.id AND users.role_type = 'ClientSuccessRole'") | ||
.where(client_success_roles: { id: nil }, role_type: 'ClientSuccessRole') | ||
|
||
coalition_lead_orphaned_users = User.joins("LEFT JOIN coalition_lead_roles ON users.role_id = coalition_lead_roles.id AND users.role_type = 'CoalitionLeadRole'") | ||
.where(coalition_lead_roles: { id: nil }, role_type: 'CoalitionLeadRole') | ||
|
||
organization_lead_orphaned_users = User.joins("LEFT JOIN organization_lead_roles ON users.role_id = organization_lead_roles.id AND users.role_type = 'OrganizationLeadRole'") | ||
.where(organization_lead_roles: { id: nil }, role_type: 'OrganizationLeadRole') | ||
|
||
team_member_orphaned_users = User.joins("LEFT JOIN team_member_roles ON users.role_id = team_member_roles.id AND users.role_type = 'TeamMemberRole'") | ||
.where(team_member_roles: { id: nil }, role_type: 'TeamMemberRole') | ||
|
||
orphaned_users = admin_orphaned_users + greeter_orphaned_users + site_coordinator_orphaned_users + client_success_orphaned_users + coalition_lead_orphaned_users + organization_lead_orphaned_users + team_member_orphaned_users | ||
|
||
puts "These users have no roles: #{orphaned_users.pluck(:id)}" | ||
end | ||
|
||
desc "Replace user_id calls in other models and delete old user" | ||
task :replace_user_associations_and_delete_old_user, [:old_user_id, :new_user_id] => :environment do |t, args| | ||
old_user_id = args[:old_user_id] | ||
new_user_id = args[:new_user_id] | ||
if old_user_id.nil? || new_user_id.nil? | ||
puts "Please provide a user ID." | ||
next | ||
end | ||
|
||
old_user = User.find_by(id: old_user_id) | ||
new_user = User.find_by(id: new_user_id) | ||
|
||
if old_user.nil? || new_user.nil? | ||
puts "Please provide users that exist" | ||
else | ||
if old_user.role.present? | ||
puts "Role for old user ##{old_user_id} is present, are you sure you want to delete?" | ||
next | ||
end | ||
|
||
logs = AccessLog.where(user: old_user) | ||
logs.update(user: new_user) | ||
|
||
notes = Note.where(user_id: old_user) | ||
notes.update(user: new_user) | ||
|
||
notifications = UserNotification.where(user_id: old_user) | ||
notifications.update(user: new_user) | ||
|
||
system_notes = SystemNote.where(user_id: old_user) | ||
system_notes.update(user: new_user) | ||
|
||
puts "User with ID #{old_user_id} and associated records have been replaced with ID #{new_user_id}." | ||
|
||
old_user.destroy! | ||
|
||
puts "User with ID #{old_user_id} has been deleted" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'rails_helper' | ||
|
||
describe "cleanup_orphaned_users:find_orphaned_users" do | ||
include_context "rake" | ||
|
||
around { |example| capture_output { example.run } } | ||
|
||
before(:all) do | ||
Rails.application.load_tasks | ||
end | ||
|
||
let!(:team_member_user) { create :team_member_user } | ||
let!(:admin_user) { create :admin_user } | ||
|
||
context "when there are users without roles" do | ||
|
||
before do | ||
team_member_user.role.destroy | ||
admin_user.role.destroy | ||
end | ||
|
||
it "it finds users without roles" do | ||
expect { | ||
task.invoke | ||
}.to output(/These users have no roles: \[#{admin_user.id}, #{team_member_user.id}]/).to_stdout | ||
end | ||
end | ||
end | ||
|
||
describe "cleanup_orphaned_users:replace_user_associations_and_delete_old_user" do | ||
include_context "rake" | ||
|
||
around { |example| capture_output { example.run } } | ||
|
||
before(:all) do | ||
Rails.application.load_tasks | ||
end | ||
|
||
let!(:team_member_user) { create :team_member_user } | ||
let!(:admin_user) { create :admin_user } | ||
|
||
context "when old and new user exists" do | ||
let!(:new_user){ create :team_member_user} | ||
let!(:old_user){ create :team_member_user} | ||
let!(:access_log) { create :access_log, user: old_user } | ||
let!(:note) { create :note, user: old_user } | ||
|
||
before do | ||
old_user.role.destroy | ||
end | ||
|
||
it "it finds users without roles" do | ||
task.invoke(old_user.id, new_user.id) | ||
expect(access_log.reload.user).to eq new_user | ||
expect(note.reload.user).to eq new_user | ||
end | ||
end | ||
end |