🐛 Preload the authors_mod on AuthorsUsersSeparatorUpdater
I really dodged a bullet by importing the production database and testing that this worked manually, phew!master
parent
8dad840867
commit
1b7504fabe
|
@ -1,16 +1,27 @@
|
|||
class AuthorsUsersSeparationUpdater
|
||||
def run
|
||||
User.all.each do |user|
|
||||
authors_mods = AuthorsMod.where(author_id: user.id)
|
||||
all_users = User.all
|
||||
all_authors_mods = all_users.map{|u| AuthorsMod.where(author_id: u.id).load }
|
||||
all_users.each_with_index do |user, i|
|
||||
#LN "============ Updating user: #{user.name} #{user.id}"
|
||||
authors_mods = all_authors_mods[i]
|
||||
unless authors_mods.empty?
|
||||
author = Author.create! name: user.name, forum_name: user.name
|
||||
authors_mods.update_all(author_id: author.id)
|
||||
#LN "Has mods! Author created! #{author.id}"
|
||||
authors_mods.each do |am|
|
||||
#LN "Updating authors_mod #{am.mod.name}---#{user.name} #{user.id} -> #{author.id}"
|
||||
am.update! author_id: author.id
|
||||
end
|
||||
Author.reset_counters(author.id, :mods)
|
||||
if user.autogenerated?
|
||||
#LN "User was autogenerated, destroying!"
|
||||
user.destroy!
|
||||
elsif author.mods.where(owner: user).count > 0
|
||||
#LN "User was legit and marked as owner! Associating them!"
|
||||
author.user = user
|
||||
author.save!
|
||||
else
|
||||
#LN "User was legit! But didn't have ownership of any mods!"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -168,4 +168,27 @@ describe AuthorsUsersSeparationUpdater do
|
|||
expect(a2.mods).to match_array [m1]
|
||||
expect(a3.mods).to match_array [m3]
|
||||
end
|
||||
|
||||
it 'should not mess up with newly created AuthorsMod' do
|
||||
# This will ensure us that IDs start at 1
|
||||
DatabaseCleaner.strategy = :truncation
|
||||
DatabaseCleaner.clean
|
||||
|
||||
u1 = create :user, name: 'Potato'
|
||||
u2 = create :user, name: 'Galaxy'
|
||||
|
||||
# We create and destroy a new author so the next one starts with ID=2
|
||||
# and we can break this thing!
|
||||
Author.create!(name: 'Whatever').destroy!
|
||||
|
||||
m1 = create :mod, owner: nil, authors: []
|
||||
|
||||
create :authors_mod, mod_id: m1.id, author_id: u1.id
|
||||
|
||||
updater.run
|
||||
|
||||
expect(Author.count).to eq 1
|
||||
a1 = Author.last
|
||||
expect(a1.mods).to match_array [m1]
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue