FIX: Create readonly functions during backup

FIX: Create readonly functions during backup

Temporarily recreate already dropped functions in the discourse_functions schema in order to allow restoring of backups which still reference dropped functions.

diff --git a/db/post_migrate/20180917024729_remove_superfluous_columns.rb b/db/post_migrate/20180917024729_remove_superfluous_columns.rb
index 128c3e8..a230734 100644
--- a/db/post_migrate/20180917024729_remove_superfluous_columns.rb
+++ b/db/post_migrate/20180917024729_remove_superfluous_columns.rb
@@ -4,24 +4,23 @@ require 'migration/column_dropper'
 require 'badge_posts_view_manager'
 
 class RemoveSuperfluousColumns < ActiveRecord::Migration[5.2]
-  def up
-    {
-      user_profiles: %i{
+  DROPPED_COLUMNS ||= {
+    user_profiles: %i{
         card_image_badge_id
       },
-      categories: %i{
+    categories: %i{
         logo_url
         background_url
         suppress_from_homepage
       },
-      groups: %i{
+    groups: %i{
         visible
         public
         alias_level
       },
-      theme_fields: %i{target},
-      user_stats: %i{first_topic_unread_at},
-      topics: %i{
+    theme_fields: %i{target},
+    user_stats: %i{first_topic_unread_at},
+    topics: %i{
         auto_close_at
         auto_close_user_id
         auto_close_started_at
@@ -35,7 +34,7 @@ class RemoveSuperfluousColumns < ActiveRecord::Migration[5.2]
         last_unread_at
         vote_count
       },
-      users: %i{
+    users: %i{
         email
         email_always
         mailing_list_mode
@@ -58,23 +57,27 @@ class RemoveSuperfluousColumns < ActiveRecord::Migration[5.2]
         silenced
         trust_level_locked
       },
-      user_auth_tokens: %i{legacy},
-      user_options: %i{theme_key},
-      themes: %i{key},
-      email_logs: %i{
+    user_auth_tokens: %i{legacy},
+    user_options: %i{theme_key},
+    themes: %i{key},
+    email_logs: %i{
         topic_id
         reply_key
         skipped
         skipped_reason
       },
-    }.each do |table, columns|
+    posts: %i{vote_count}
+  }
+
+  def up
+    BadgePostsViewManager.drop!
+
+    DROPPED_COLUMNS.each do |table, columns|
       Migration::ColumnDropper.execute_drop(table, columns)
     end
 
     DB.exec "DROP FUNCTION IF EXISTS first_unread_topic_for(int)"
 
-    BadgePostsViewManager.drop!
-    Migration::ColumnDropper.execute_drop(:posts, %i{vote_count})
     BadgePostsViewManager.create!
   end
 
diff --git a/db/post_migrate/20180917034056_remove_superfluous_tables.rb b/db/post_migrate/20180917034056_remove_superfluous_tables.rb
index 21cba12..f652452 100644
--- a/db/post_migrate/20180917034056_remove_superfluous_tables.rb
+++ b/db/post_migrate/20180917034056_remove_superfluous_tables.rb
@@ -3,12 +3,14 @@
 require 'migration/table_dropper'
 
 class RemoveSuperfluousTables < ActiveRecord::Migration[5.2]
-  def up
-    %i{
+  DROPPED_TABLES ||= %i{
       category_featured_users
       versions
       topic_status_updates
-    }.each do |table|
+    }
+
+  def up
+    DROPPED_TABLES.each do |table|
       Migration::TableDropper.execute_drop(table)
     end
   end
diff --git a/db/post_migrate/20181012123001_drop_group_locked_trust_level_from_user.rb b/db/post_migrate/20181012123001_drop_group_locked_trust_level_from_user.rb
index b1ec42c..3b3bd6e 100644
--- a/db/post_migrate/20181012123001_drop_group_locked_trust_level_from_user.rb
+++ b/db/post_migrate/20181012123001_drop_group_locked_trust_level_from_user.rb
@@ -3,8 +3,14 @@
 require 'migration/column_dropper'
 
 class DropGroupLockedTrustLevelFromUser < ActiveRecord::Migration[5.2]
+  DROPPED_COLUMNS ||= {
+    posts: %i{group_locked_trust_level}
+  }
+
   def up
-    Migration::ColumnDropper.execute_drop(:posts, %i{group_locked_trust_level})
+    DROPPED_COLUMNS.each do |table, columns|
+      Migration::ColumnDropper.execute_drop(table, columns)
+    end
   end
 
   def down
diff --git a/db/post_migrate/20190103065652_remove_uploaded_meta_id_from_category.rb b/db/post_migrate/20190103065652_remove_uploaded_meta_id_from_category.rb
index 484a31c..9aba22f 100644
--- a/db/post_migrate/20190103065652_remove_uploaded_meta_id_from_category.rb
+++ b/db/post_migrate/20190103065652_remove_uploaded_meta_id_from_category.rb
@@ -3,8 +3,14 @@
 require 'migration/column_dropper'
 
 class RemoveUploadedMetaIdFromCategory < ActiveRecord::Migration[5.2]
+  DROPPED_COLUMNS ||= {
+    categories: %i{uploaded_meta_id}
+  }
+
   def up
-    Migration::ColumnDropper.execute_drop(:categories, %i{uploaded_meta_id})
+    DROPPED_COLUMNS.each do |table, columns|
+      Migration::ColumnDropper.execute_drop(table, columns)
+    end
   end
 
   def down
diff --git a/db/post_migrate/20190208144706_drop_unused_auth_tables_again.rb b/db/post_migrate/20190208144706_drop_unused_auth_tables_again.rb
index c82137c..6fb208c 100644
--- a/db/post_migrate/20190208144706_drop_unused_auth_tables_again.rb
+++ b/db/post_migrate/20190208144706_drop_unused_auth_tables_again.rb
@@ -3,11 +3,13 @@
 require 'migration/table_dropper'
 
 class DropUnusedAuthTablesAgain < ActiveRecord::Migration[5.2]
-  def up
-    %i{
+  DROPPED_TABLES ||= %i{
         facebook_user_infos
         twitter_user_infos
-      }.each do |table|
+      }
+
+  def up
+    DROPPED_TABLES.each do |table|
       Migration::TableDropper.execute_drop(table)
     end
   end
diff --git a/db/post_migrate/20190312194528_drop_email_user_options_columns.rb b/db/post_migrate/20190312194528_drop_email_user_options_columns.rb
index bb9ec95..1b4203b 100644
--- a/db/post_migrate/20190312194528_drop_email_user_options_columns.rb
+++ b/db/post_migrate/20190312194528_drop_email_user_options_columns.rb
@@ -3,14 +3,16 @@
 require 'migration/column_dropper'
 
 class DropEmailUserOptionsColumns < ActiveRecord::Migration[5.2]
-  def up
-    {
-      user_options: %i{
+  DROPPED_COLUMNS ||= {
+    user_options: %i{
         email_direct
         email_private_messages
         email_always
       },
-    }.each do |table, columns|
+  }
+
+  def up
+    DROPPED_COLUMNS.each do |table, columns|
       Migration::ColumnDropper.execute_drop(table, columns)
     end
   end
diff --git a/db/post_migrate/20190716124050_remove_via_email_from_invite.rb b/db/post_migrate/20190716124050_remove_via_email_from_invite.rb
index 676ea11..29e91f3 100644
--- a/db/post_migrate/20190716124050_remove_via_email_from_invite.rb
+++ b/db/post_migrate/20190716124050_remove_via_email_from_invite.rb
@@ -3,8 +3,14 @@
 require 'migration/column_dropper'
 
 class RemoveViaEmailFromInvite < ActiveRecord::Migration[5.2]
+  DROPPED_COLUMNS ||= {
+    invites: %i{via_email}
+  }
+
   def up
-    Migration::ColumnDropper.execute_drop(:invites, %i{via_email})
+    DROPPED_COLUMNS.each do |table, columns|
+      Migration::ColumnDropper.execute_drop(table, columns)
+    end
   end
 
   def down
diff --git a/lib/backup_restore/restorer.rb b/lib/backup_restore/restorer.rb
index 701c72e..1b038a5 100644
--- a/lib/backup_restore/restorer.rb
+++ b/lib/backup_restore/restorer.rb
@@ -63,6 +63,7 @@ module BackupRestore
       validate_metadata
 
       extract_dump
+      create_missing_discourse_functions
 
       if !can_restore_into_different_schema?
         log "Cannot restore into different schema, restoring in-place"
@@ -144,6 +145,7 @@ module BackupRestore
 
       @logs = []
       @readonly_mode_was_enabled = Discourse.readonly_mode?
+      @created_functions_for_table_columns = []
     end
 
     def listen_for_shutdown_signal
@@ -561,8 +563,46 @@ module BackupRestore
       log "Something went wrong while notifying user.", ex
     end
 
+    def create_missing_discourse_functions
+      log "Creating missing functions in the discourse_functions schema"
+
+      all_readonly_table_columns = []
+
+      Dir[Rails.root.join(Discourse::DB_POST_MIGRATE_PATH, "*.rb")].each do |path|
+        require path
+        class_name = File.basename(path, ".rb").sub(/^\d+_/, "").camelize
+        migration_class = class_name.constantize
+
+        if migration_class.const_defined?(:DROPPED_TABLES)
+          migration_class::DROPPED_TABLES.each do |table_name|
+            all_readonly_table_columns << [table_name]
+          end
+        end
+

[... diff too long, it was truncated ...]

GitHub sha: 7cb51d0e

1 Like