1
0

db.rake 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # frozen_string_literal: true
  2. # We are providing our own task with our own format
  3. Rake::Task['db:encryption:init'].clear
  4. namespace :db do
  5. namespace :encryption do
  6. desc 'Generate a set of keys for configuring Active Record encryption in a given environment'
  7. task :init do # rubocop:disable Rails/RakeEnvironment
  8. if %w(
  9. ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY
  10. ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT
  11. ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY
  12. ).any? { |key| ENV.key?(key) }
  13. pastel = Pastel.new
  14. puts pastel.red(<<~MSG)
  15. WARNING: It looks like encryption secrets have already been set. Please ensure you are not changing secrets for a Mastodon installation that already uses them, as this will cause data loss and other issues that are difficult to recover from.
  16. MSG
  17. end
  18. puts <<~MSG
  19. Add the following secret environment variables to your Mastodon environment (e.g. .env.production), ensure they are shared across all your nodes and do not change them after they are set:#{' '}
  20. ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY=#{SecureRandom.alphanumeric(32)}
  21. ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT=#{SecureRandom.alphanumeric(32)}
  22. ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY=#{SecureRandom.alphanumeric(32)}
  23. MSG
  24. end
  25. end
  26. namespace :migrate do
  27. desc 'Setup the db or migrate depending on state of db'
  28. task setup: :environment do
  29. if ActiveRecord::Migrator.current_version.zero?
  30. Rake::Task['db:migrate'].invoke
  31. Rake::Task['db:seed'].invoke
  32. end
  33. rescue ActiveRecord::NoDatabaseError
  34. Rake::Task['db:setup'].invoke
  35. else
  36. Rake::Task['db:migrate'].invoke
  37. end
  38. end
  39. task pre_migration_check: :environment do
  40. version = ActiveRecord::Base.connection.database_version
  41. abort 'This version of Mastodon requires PostgreSQL 12.0 or newer. Please update PostgreSQL before updating Mastodon.' if version < 120_000
  42. end
  43. Rake::Task['db:migrate'].enhance(['db:pre_migration_check'])
  44. end