FIX: Handle BBCode in `migrate_to_s3` task as well.

FIX: Handle BBCode in migrate_to_s3 task as well.

diff --git a/lib/db_helper.rb b/lib/db_helper.rb
index 19cdfe5..ae29875 100644
--- a/lib/db_helper.rb
+++ b/lib/db_helper.rb
@@ -56,8 +56,6 @@ class DbHelper
         "#{column} IS NOT NULL AND #{column} #{match} :pattern"
       end.join(" OR ")
 
-      puts pattern, replacement, flags, match
-
       DB.exec(<<~SQL, pattern: pattern, replacement: replacement, flags: flags, match: match)
         UPDATE #{table}
            SET #{set}
diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake
index 2a384b8..9a263ef 100644
--- a/lib/tasks/uploads.rake
+++ b/lib/tasks/uploads.rake
@@ -367,6 +367,13 @@ def migrate_to_s3
 
     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\.]*))/
 
diff --git a/spec/lib/db_helper_spec.rb b/spec/lib/db_helper_spec.rb
index ef9105c..d9573bb 100644
--- a/spec/lib/db_helper_spec.rb
+++ b/spec/lib/db_helper_spec.rb
@@ -41,4 +41,14 @@ RSpec.describe DbHelper do
       expect(post.reload.cooked).to eq('test')
     end
   end
+
+  describe ".regexp_replace" do
+    it "should remap columns correctly" do
+      post = Fabricate(:post, raw: "this is a [img]test[/img] post")
+
+      DbHelper.regexp_replace("\\[img\\]test\\[/img\\]", "[img]something[/img]")
+
+      expect(post.reload.raw).to include("[img]something[/img]")
+    end
+  end
 end

GitHub sha: 19c3c25d

This should not do a replace when the dry_run flag is enabled :wink:

1 Like

This has been followed up.

2 Likes