db.rake 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # frozen_string_literal: true
  2. namespace :db do
  3. namespace :migrate do
  4. desc 'Setup the db or migrate depending on state of db'
  5. task setup: :environment do
  6. begin
  7. if ActiveRecord::Migrator.current_version.zero?
  8. Rake::Task['db:migrate'].invoke
  9. Rake::Task['db:seed'].invoke
  10. end
  11. rescue ActiveRecord::NoDatabaseError
  12. Rake::Task['db:setup'].invoke
  13. else
  14. Rake::Task['db:migrate'].invoke
  15. end
  16. end
  17. end
  18. task :post_migration_hook do
  19. at_exit do
  20. unless %w(C POSIX).include?(ActiveRecord::Base.connection.select_one('SELECT datcollate FROM pg_database WHERE datname = current_database();')['datcollate'])
  21. warn <<~WARNING
  22. Your database collation is susceptible to index corruption.
  23. (This warning does not indicate that index corruption has occurred and can be ignored)
  24. (To learn more, visit: https://docs.joinmastodon.org/admin/troubleshooting/index-corruption/)
  25. WARNING
  26. end
  27. end
  28. end
  29. task :pre_migration_check do
  30. version = ActiveRecord::Base.connection.select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i
  31. abort 'ERROR: This version of Mastodon requires PostgreSQL 9.5 or newer. Please update PostgreSQL before updating Mastodon.' if version < 90_500
  32. end
  33. Rake::Task['db:migrate'].enhance(['db:pre_migration_check'])
  34. Rake::Task['db:migrate'].enhance(['db:post_migration_hook'])
  35. end