Add data update script for backfilling usernames (#12728)

This commit is contained in:
Michael Kohl 2021-02-19 08:16:29 +07:00 committed by GitHub
parent 1b47f16291
commit 283b008e97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View file

@ -0,0 +1,9 @@
module DataUpdateScripts
class BackfillUsernames
def run
User.where(username: nil).find_each do |user|
user.update(username: "user#{user.id}")
end
end
end
end

View file

@ -0,0 +1,14 @@
require "rails_helper"
require Rails.root.join(
"lib/data_update_scripts/20210218041143_backfill_usernames.rb",
)
describe DataUpdateScripts::BackfillUsernames do
it " " do
user = create(:user)
user.update_column(:username, nil)
expect do
described_class.new.run
end.to change { user.reload.username }.from(nil).to("user#{user.id}")
end
end