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
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
app/jobs/scheduled/fix_user_usernames_and_groups_names_clash.rb
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,29 @@ | ||
module Jobs | ||
class FixUserUsernamesAndGroupNamesClash < Jobs::Scheduled | ||
every 1.week | ||
|
||
def execute(args) | ||
User.joins("LEFT JOIN groups ON lower(groups.name) = users.username_lower") | ||
.where("groups.id IS NOT NULL") | ||
.find_each do |user| | ||
|
||
suffix = 1 | ||
old_username = user.username | ||
|
||
loop do | ||
user.username = "#{old_username}#{suffix}" | ||
suffix += 1 | ||
break if user.valid? | ||
end | ||
|
||
new_username = user.username | ||
user.username = old_username | ||
|
||
UsernameChanger.new( | ||
user, | ||
new_username | ||
).change(asynchronous: false) | ||
end | ||
end | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
spec/jobs/fix_user_usernames_and_group_names_clash_spec.rb
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,15 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Jobs::FixUserUsernamesAndGroupNamesClash do | ||
it 'update usernames of users that clashes with a group name' do | ||
user = Fabricate(:user) | ||
Fabricate(:user, username: 'test1') | ||
group = Fabricate(:group, name: 'test') | ||
user.update_columns(username: 'test', username_lower: 'test') | ||
|
||
Jobs::FixUserUsernamesAndGroupNamesClash.new.execute({}) | ||
|
||
expect(user.reload.username).to eq('test2') | ||
expect(group.reload.name).to eq('test') | ||
end | ||
end |
4172e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit has been mentioned on Discourse Meta. There might be relevant details there:
https://meta.discourse.org/t/recent-changes-to-api-requests/91967/5