FIX: Prevent failed remaps during restores

FIX: Prevent failed remaps during restores

Additional changes:

  • Verbose logging of remaps during restores
  • Exclude the backup_metadata table from restores
diff --git a/app/models/backup_metadata.rb b/app/models/backup_metadata.rb
index 6c27399..6cddb82 100644
--- a/app/models/backup_metadata.rb
+++ b/app/models/backup_metadata.rb
@@ -2,7 +2,7 @@
 
 class BackupMetadata < ActiveRecord::Base
   def self.value_for(name)
-    where(name: name).pluck(:value).first
+    where(name: name).pluck(:value).first.presence
   end
 end
 
diff --git a/lib/backup_restore/restorer.rb b/lib/backup_restore/restorer.rb
index 1b038a5..cf5aa29 100644
--- a/lib/backup_restore/restorer.rb
+++ b/lib/backup_restore/restorer.rb
@@ -469,41 +469,46 @@ module BackupRestore
       uploads_folder = was_multisite ? "/" : "/uploads/#{current_db_name}/"
 
       if (old_base_url = BackupMetadata.value_for("base_url")) && old_base_url != Discourse.base_url
-        DbHelper.remap(old_base_url, Discourse.base_url)
+        remap(old_base_url, Discourse.base_url)
       end
 
       current_s3_base_url = SiteSetting.Upload.enable_s3_uploads ? SiteSetting.Upload.s3_base_url : nil
       if (old_s3_base_url = BackupMetadata.value_for("s3_base_url")) && old_base_url != current_s3_base_url
-        DbHelper.remap("#{old_s3_base_url}/", uploads_folder)
+        remap("#{old_s3_base_url}/", uploads_folder)
       end
 
       current_s3_cdn_url = SiteSetting.Upload.enable_s3_uploads ? SiteSetting.Upload.s3_cdn_url : nil
       if (old_s3_cdn_url = BackupMetadata.value_for("s3_cdn_url")) && old_s3_cdn_url != current_s3_cdn_url
         base_url = SiteSetting.Upload.enable_s3_uploads ? SiteSetting.Upload.s3_cdn_url : Discourse.base_url
-        DbHelper.remap("#{old_s3_cdn_url}/", UrlHelper.schemaless("#{base_url}#{uploads_folder}"))
+        remap("#{old_s3_cdn_url}/", UrlHelper.schemaless("#{base_url}#{uploads_folder}"))
 
         old_host = URI.parse(old_s3_cdn_url).host
         new_host = URI.parse(base_url).host
-        DbHelper.remap(old_host, new_host)
+        remap(old_host, new_host)
       end
 
       if (old_cdn_url = BackupMetadata.value_for("cdn_url")) && old_cdn_url != Discourse.asset_host
         base_url = Discourse.asset_host || Discourse.base_url
-        DbHelper.remap("#{old_cdn_url}/", UrlHelper.schemaless("#{base_url}/"))
+        remap("#{old_cdn_url}/", UrlHelper.schemaless("#{base_url}/"))
 
         old_host = URI.parse(old_cdn_url).host
         new_host = URI.parse(base_url).host
-        DbHelper.remap(old_host, new_host)
+        remap(old_host, new_host)
       end
 
       if previous_db_name != current_db_name
-        DbHelper.remap("uploads/#{previous_db_name}", "uploads/#{current_db_name}")
+        remap("uploads/#{previous_db_name}", "uploads/#{current_db_name}")
       end
 
     rescue => ex
       log "Something went wrong while remapping uploads.", ex
     end
 
+    def remap(from, to)
+      puts "Remapping '#{from}' to '#{to}'"
+      DbHelper.remap(from, to, verbose: true, excluded_tables: ["backup_metadata"])
+    end
+
     def migrate_to_s3
       log "Migrating uploads to S3..."
       ENV["SKIP_FAILED"] = "1"

GitHub sha: d6863181

2 Likes