about summary refs log tree commit diff
path: root/lib/tasks/db.rake
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2020-11-19 17:37:49 +0100
committerGitHub <noreply@github.com>2020-11-19 17:37:49 +0100
commit1242e57c270e9ff356e8c175670d5dc3a10ad273 (patch)
treefb149a2e4e95f510fa1343983b76609934995173 /lib/tasks/db.rake
parent29e76f994e3abf0146da1be3ddda9e0893f7efa5 (diff)
Deal with collation-related index corruption (#14860)
* Add tootctl maintenance fix-duplicates

This tool goes through the database to detect and fix duplicates.
This operation is very slow and may cause data loss (of data that would be
inaccessible without intervention because of the existing index corruptions).
It tries its best to make sensible decisions, and asks the user in some cases.

* Add warning message in db:migrate hook

* Clear Rails cache after being done with database deduplication

Avoids followers hash cache being incorrect, among other things
Diffstat (limited to 'lib/tasks/db.rake')
-rw-r--r--lib/tasks/db.rake11
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake
index b76e90131..199155107 100644
--- a/lib/tasks/db.rake
+++ b/lib/tasks/db.rake
@@ -48,6 +48,17 @@ namespace :db do
     end
   end
 
+  task :post_migration_hook do
+    at_exit do
+      unless %w(C POSIX).include?(ActiveRecord::Base.connection.execute('SELECT datcollate FROM pg_database WHERE datname = current_database();').first['datcollate'])
+        Rails.logger.warn 'WARNING: Your database is using an unsafe collation setting, which might result in index corruption.'
+        Rails.logger.warn 'WARNING: See https://docs.joinmastodon.org/admin/troubleshooting/index-corruption/#am-i-affected'
+      end
+    end
+  end
+
+  Rake::Task['db:migrate'].enhance(['db:post_migration_hook'])
+
   # Before we load the schema, define the timestamp_id function.
   # Idiomatically, we might do this in a migration, but then it
   # wouldn't end up in schema.rb, so we'd need to figure out a way to