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

FIX: Dry run broken for uploads:migrate_to_s3.

diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake
index 9a263ef..b626051 100644
--- a/lib/tasks/uploads.rake
+++ b/lib/tasks/uploads.rake
@@ -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"
-
-    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]"
+    [
+      [
+        "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|
+
+      if true
+        puts "REPLACING '#{from}' WITH '#{to}'"
+      else
+        DbHelper.regexp_replace(from, to)
+      end
+    end
 
-    DbHelper.regexp_replace(from, to)
+    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\.]*))/
 
-    # 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.raw.scan(regexp).each do |upload_url, _|
-        upload = Upload.get_from_url(upload_url)
-        post.raw = post.raw.gsub("![](#{upload_url})", "![](#{upload.short_url})")
+        post.save!(validate: false)
       end
-
-      post.save!(validate: false)
     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!"

GitHub sha: cc2bac86

1 Like

Should this be if dry_run?

1 Like

Oops I was testing something. I’ll fix it tml :slight_smile:

1 Like

DEV: Fix hardcoded value introduced in cc2bac86e9ba41a948c7c6fdee8c184615c68b98.