Merge pull request #5071 from LeoMcA/remove_duplicated_without_posts

FIX: removed the duplicated staged users without posts
This commit is contained in:
Guo Xiang Tan 2017-08-24 09:23:36 +09:00 committed by GitHub
commit 027b999828
2 changed files with 15 additions and 5 deletions

View File

@ -2,18 +2,22 @@ module Jobs
class FixPrimaryEmailsForStagedUsers < Jobs::Onceoff class FixPrimaryEmailsForStagedUsers < Jobs::Onceoff
def execute_onceoff(args) def execute_onceoff(args)
users = User.where(active: false, staged: true).joins(:email_tokens) users = User.where(active: false, staged: true).joins(:email_tokens)
destroyer = UserDestroyer.new(Discourse.system_user) acting_user = Discourse.system_user
destroyer = UserDestroyer.new(acting_user)
users.group("email_tokens.email") users.group("email_tokens.email")
.having("COUNT(email_tokens.email) > 1") .having("COUNT(email_tokens.email) > 1")
.count .count
.each_key do |email| .each_key do |email|
users.where("email_tokens.email = ?", email) query = users.where("email_tokens.email = ?", email).order(id: :asc)
.order(id: :asc)
.offset(1)
.each do |user|
original_user = query.first
query.offset(1).each do |user|
user.posts.each do |post|
post.set_owner(original_user, acting_user)
end
destroyer.destroy(user) destroyer.destroy(user)
end end
end end

View File

@ -8,6 +8,10 @@ RSpec.describe Jobs::FixPrimaryEmailsForStagedUsers do
staged_user2 = Fabricate(:user, staged: true, active: false) staged_user2 = Fabricate(:user, staged: true, active: false)
staged_user3 = Fabricate(:user, staged: true, active: false) staged_user3 = Fabricate(:user, staged: true, active: false)
post1 = Fabricate(:post, user: staged_user2)
post2 = Fabricate(:post, user: staged_user2)
post3 = Fabricate(:post, user: staged_user3)
[staged_user, staged_user2, staged_user3].each do |user| [staged_user, staged_user2, staged_user3].each do |user|
user.email_tokens = [Fabricate.create(:email_token, email: common_email, user: user)] user.email_tokens = [Fabricate.create(:email_token, email: common_email, user: user)]
end end
@ -18,8 +22,10 @@ RSpec.describe Jobs::FixPrimaryEmailsForStagedUsers do
expect { described_class.new.execute_onceoff({}) } expect { described_class.new.execute_onceoff({}) }
.to change { User.count }.by(-2) .to change { User.count }.by(-2)
.and change { staged_user.posts.count }.by(3)
expect(User.all).to contain_exactly(Discourse.system_user, staged_user, active_user) expect(User.all).to contain_exactly(Discourse.system_user, staged_user, active_user)
expect(staged_user.posts.all).to contain_exactly(post1, post2, post3)
expect(staged_user.reload.email).to eq(common_email) expect(staged_user.reload.email).to eq(common_email)
end end
end end