about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2022-03-12 08:12:57 +0100
committerGitHub <noreply@github.com>2022-03-12 08:12:57 +0100
commit75e33fd08fb4f926897490b6fa42890b393c0989 (patch)
tree3836227041ada2f5b6e389f40e1d955db19bbfa4 /lib
parentbc320d6cec5900fa2fd9d3a46480e242111caaca (diff)
Fix null values being included in some indexes (#17711)
* Fix null values being included in some indexes

* Update lib/mastodon/migration_helpers.rb

Co-authored-by: Claire <claire.github-309c@sitedethib.com>

* Add documentation link to corruption error message

Co-authored-by: Claire <claire.github-309c@sitedethib.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/mastodon/migration_helpers.rb28
-rw-r--r--lib/tasks/db.rake15
2 files changed, 27 insertions, 16 deletions
diff --git a/lib/mastodon/migration_helpers.rb b/lib/mastodon/migration_helpers.rb
index 5bc903349..e920ff88f 100644
--- a/lib/mastodon/migration_helpers.rb
+++ b/lib/mastodon/migration_helpers.rb
@@ -42,8 +42,14 @@
 module Mastodon
   module MigrationHelpers
     class CorruptionError < StandardError
-      def initialize(message = nil)
-        super(message.presence || 'Migration failed because of index corruption, see https://docs.joinmastodon.org/admin/troubleshooting/index-corruption/#fixing')
+      attr_reader :index_name
+
+      def initialize(index_name)
+        @index_name = index_name
+
+        super "The index `#{index_name}` seems to be corrupted, it contains duplicate rows. " \
+          'For information on how to fix this, see our documentation: ' \
+          'https://docs.joinmastodon.org/admin/troubleshooting/index-corruption/'
       end
 
       def cause
@@ -802,6 +808,24 @@ module Mastodon
       columns(table).find { |column| column.name == name }
     end
 
+    # Update the configuration of an index by creating a new one and then
+    # removing the old one
+    def update_index(table_name, index_name, columns, **index_options)
+      if index_name_exists?(table_name, "#{index_name}_new") && index_name_exists?(table_name, index_name)
+        remove_index table_name, "#{index_name}_new"
+      end
+
+      begin
+        add_index table_name, columns, **index_options.merge(name: "#{index_name}_new", algorithm: :concurrently)
+      rescue ActiveRecord::RecordNotUnique
+        remove_index table_name, name: "#{index_name}_new"
+        raise CorruptionError.new(index_name)
+      end
+
+      remove_index table_name, name: index_name if index_name_exists?(table_name, index_name)
+      rename_index table_name, "#{index_name}_new", index_name
+    end
+
     # This will replace the first occurrence of a string in a column with
     # the replacement
     # On postgresql we can use `regexp_replace` for that.
diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake
index a6b8c74cd..ca939fd1f 100644
--- a/lib/tasks/db.rake
+++ b/lib/tasks/db.rake
@@ -17,23 +17,10 @@ namespace :db do
     end
   end
 
-  task :post_migration_hook do
-    at_exit do
-      unless %w(C POSIX).include?(ActiveRecord::Base.connection.select_one('SELECT datcollate FROM pg_database WHERE datname = current_database();')['datcollate'])
-        warn <<~WARNING
-          Your database collation may be susceptible to index corruption.
-            (This warning does not indicate that index corruption has occurred, and it can be ignored if you've previously checked for index corruption)
-            (To learn more, visit: https://docs.joinmastodon.org/admin/troubleshooting/index-corruption/)
-        WARNING
-      end
-    end
-  end
-
   task :pre_migration_check do
     version = ActiveRecord::Base.connection.select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i
-    abort 'ERROR: This version of Mastodon requires PostgreSQL 9.5 or newer. Please update PostgreSQL before updating Mastodon.' if version < 90_500
+    abort 'This version of Mastodon requires PostgreSQL 9.5 or newer. Please update PostgreSQL before updating Mastodon' if version < 90_500
   end
 
   Rake::Task['db:migrate'].enhance(['db:pre_migration_check'])
-  Rake::Task['db:migrate'].enhance(['db:post_migration_hook'])
 end