FIX: Dry run broken for `uploads:migrate_to_s3`.

This commit is contained in:
Guo Xiang Tan 2019-03-25 22:38:24 +08:00
parent 4452332077
commit cc2bac86e9
1 changed files with 40 additions and 30 deletions

View File

@ -357,32 +357,40 @@ def migrate_to_s3
DbHelper.remap(from, to, anchor_left: true)
end
from = "src=\"/uploads/#{db}/original/(\\dX/(?:[a-f0-9]/)*[a-f0-9]{40}[a-z0-9\\.]*)"
to = "src=\"#{SiteSetting.Upload.s3_base_url}/#{prefix}\\1"
[
[
"src=\"/uploads/#{db}/original/(\\dX/(?:[a-f0-9]/)*[a-f0-9]{40}[a-z0-9\\.]*)",
"src=\"#{SiteSetting.Upload.s3_base_url}/#{prefix}\\1"
],
[
"src='/uploads/#{db}/original/(\\dX/(?:[a-f0-9]/)*[a-f0-9]{40}[a-z0-9\\.]*)",
"src='#{SiteSetting.Upload.s3_base_url}/#{prefix}\\1"
],
[
"\\[img\\]/uploads/#{db}/original/(\\dX/(?:[a-f0-9]/)*[a-f0-9]{40}[a-z0-9\\.]*)\\[/img\\]",
"[img]#{SiteSetting.Upload.s3_base_url}/#{prefix}\\1[/img]"
]
].each do |from, to|
DbHelper.regexp_replace(from, to)
from = "src='/uploads/#{db}/original/(\\dX/(?:[a-f0-9]/)*[a-f0-9]{40}[a-z0-9\\.]*)"
to = "src='#{SiteSetting.Upload.s3_base_url}/#{prefix}\\1"
DbHelper.regexp_replace(from, to)
# BBCode images
from = "\\[img\\]/uploads/#{db}/original/(\\dX/(?:[a-f0-9]/)*[a-f0-9]{40}[a-z0-9\\.]*)\\[/img\\]"
to = "[img]#{SiteSetting.Upload.s3_base_url}/#{prefix}\\1[/img]"
DbHelper.regexp_replace(from, to)
# Legacy inline image format
Post.where("raw LIKE '%![](/uploads/default/original/%)%'").each do |post|
regexp = /!\[\](\/uploads\/#{db}\/original\/(\dX\/(?:[a-f0-9]\/)*[a-f0-9]{40}[a-z0-9\.]*))/
post.raw.scan(regexp).each do |upload_url, _|
upload = Upload.get_from_url(upload_url)
post.raw = post.raw.gsub("![](#{upload_url})", "![](#{upload.short_url})")
if true
puts "REPLACING '#{from}' WITH '#{to}'"
else
DbHelper.regexp_replace(from, to)
end
end
post.save!(validate: false)
unless dry_run
# Legacy inline image format
Post.where("raw LIKE '%![](/uploads/default/original/%)%'").each do |post|
regexp = /!\[\](\/uploads\/#{db}\/original\/(\dX\/(?:[a-f0-9]\/)*[a-f0-9]{40}[a-z0-9\.]*))/
post.raw.scan(regexp).each do |upload_url, _|
upload = Upload.get_from_url(upload_url)
post.raw = post.raw.gsub("![](#{upload_url})", "![](#{upload.short_url})")
end
post.save!(validate: false)
end
end
if Discourse.asset_host.present?
@ -407,16 +415,18 @@ def migrate_to_s3
DbHelper.remap(from, to)
end
puts "Removing old optimized images..."
unless dry_run
puts "Removing old optimized images..."
OptimizedImage
.joins("LEFT JOIN uploads u ON optimized_images.upload_id = u.id")
.where("u.id IS NOT NULL AND u.url LIKE '//%' AND optimized_images.url NOT LIKE '//%'")
.destroy_all
OptimizedImage
.joins("LEFT JOIN uploads u ON optimized_images.upload_id = u.id")
.where("u.id IS NOT NULL AND u.url LIKE '//%' AND optimized_images.url NOT LIKE '//%'")
.destroy_all
puts "Rebaking posts with lightboxes..."
puts "Rebaking posts with lightboxes..."
Post.where("cooked LIKE '%class=\"lightbox\"%'").find_each(&:rebake!)
Post.where("cooked LIKE '%class=\"lightbox\"%'").find_each(&:rebake!)
end
end
puts "Done!"