Improve restore from tombstone script by not having to loop through all posts.

This commit is contained in:
Guo Xiang Tan 2016-09-21 18:01:59 +08:00
parent b62b6ba12c
commit 1d4451db5d

View file

@ -409,7 +409,10 @@ def recover_from_tombstone
public_path = Rails.root.join("public") public_path = Rails.root.join("public")
Post.find_each do |post| Dir.glob("#{File.join(public_path, 'uploads', 'tombstone', '**', '*.*')}").each do |path|
filename = File.basename(path)
Post.where("raw LIKE ?", "%#{filename}%").each do |post|
doc = Nokogiri::HTML::fragment(post.raw) doc = Nokogiri::HTML::fragment(post.raw)
doc.css("img[src]").each do |img| doc.css("img[src]").each do |img|
@ -420,6 +423,8 @@ def recover_from_tombstone
upload = Upload.find_by(url: url) upload = Upload.find_by(url: url)
if !upload && url if !upload && url
printf "Restoring #{url}..."
tombstone_path = File.join(public_path, 'uploads', 'tombstone', url.gsub(/^\/uploads\//, "")) tombstone_path = File.join(public_path, 'uploads', 'tombstone', url.gsub(/^\/uploads\//, ""))
if File.exists?(tombstone_path) if File.exists?(tombstone_path)
@ -427,7 +432,7 @@ def recover_from_tombstone
new_upload = Upload.create_for(Discourse::SYSTEM_USER_ID, file, File.basename(url), File.size(file)) new_upload = Upload.create_for(Discourse::SYSTEM_USER_ID, file, File.basename(url), File.size(file))
if new_upload.persisted? if new_upload.persisted?
putc "." printf "Restored into #{new_upload.url}\n"
DbHelper.remap(url, new_upload.url) DbHelper.remap(url, new_upload.url)
else else
puts "Failed to create upload for #{url}: #{new_upload.errors.full_messages}." puts "Failed to create upload for #{url}: #{new_upload.errors.full_messages}."
@ -439,6 +444,7 @@ def recover_from_tombstone
end end
end end
end end
end
ensure ensure
SiteSetting.max_image_size_kb = original_setting SiteSetting.max_image_size_kb = original_setting
end end