migration_warning.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # frozen_string_literal: true
  2. module Mastodon
  3. module MigrationWarning
  4. WARNING_SECONDS = 10
  5. DEFAULT_WARNING = <<~WARNING_MESSAGE
  6. WARNING: This migration may take a *long* time for large instances.
  7. It will *not* lock tables for any significant time, but it may run
  8. for a very long time. We will pause for #{WARNING_SECONDS} seconds to allow you to
  9. interrupt this migration if you are not ready.
  10. WARNING_MESSAGE
  11. def migration_duration_warning(explanation = nil)
  12. return unless valid_environment?
  13. announce_warning(explanation)
  14. announce_countdown
  15. end
  16. private
  17. def announce_countdown
  18. WARNING_SECONDS.downto(1) do |i|
  19. say "Continuing in #{i} second#{i == 1 ? '' : 's'}...", true
  20. sleep 1
  21. end
  22. end
  23. def valid_environment?
  24. $stdout.isatty && Rails.env.production?
  25. end
  26. def announce_warning(explanation)
  27. announce_message prepare_message(explanation)
  28. end
  29. def announce_message(text)
  30. say ''
  31. text.each_line do |line|
  32. say(line)
  33. end
  34. say ''
  35. end
  36. def prepare_message(explanation)
  37. if explanation.blank?
  38. DEFAULT_WARNING
  39. else
  40. DEFAULT_WARNING + "\n#{explanation}"
  41. end
  42. end
  43. end
  44. end