db_preferences_mysql.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. return if ActiveRecord::Base.connection_config[:adapter] != 'mysql2'
  2. Rails.application.config.db_4bytes_utf8 = false
  3. Rails.application.config.db_null_byte = true
  4. connection = ActiveRecord::Base.connection
  5. # rubocop:disable Rails/Output
  6. # rubocop:disable Rails/Exit
  7. # rubocop:disable Layout/IndentHeredoc
  8. # Version check --------------------------------------------------------------
  9. # mysql example: "5.7.3"
  10. # mariadb example: "10.1.17-MariaDB"
  11. server_version = connection.execute('SELECT @@version;').first.first
  12. raise 'Unable to retrive database version' if server_version.blank?
  13. version_number = Gem::Version.new(server_version.split('-').first)
  14. vendor = server_version.split('-').second || 'MySQL'
  15. case vendor
  16. when 'MySQL'
  17. if version_number < Gem::Version.new('5.6')
  18. printf "\e[31m" # ANSI red
  19. puts <<~MSG
  20. Error: Incompatible database backend version
  21. (MySQL 5.6+ required; #{version_number} found)
  22. MSG
  23. printf "\e[0m" # ANSI normal
  24. exit 1
  25. end
  26. when 'MariaDB'
  27. if version_number < Gem::Version.new('10.0')
  28. printf "\e[31m" # ANSI red
  29. puts <<~MSG
  30. Error: Incompatible database backend version
  31. (MariaDB 10.0+ required; #{version_number} found)
  32. MSG
  33. printf "\e[0m" # ANSI normal
  34. exit 1
  35. end
  36. end
  37. # Configuration check --------------------------------------------------------
  38. # Before MySQL 8.0, the default value of max_allowed_packet was 1MB - 4MB,
  39. # which is impractically small and can lead to failures processing emails.
  40. #
  41. # See https://github.com/zammad/zammad/issues/1759
  42. # https://github.com/zammad/zammad/issues/1970
  43. # https://github.com/zammad/zammad/issues/2034
  44. max_allowed_packet = connection.execute('SELECT @@max_allowed_packet;').first.first
  45. max_allowed_packet_mb = max_allowed_packet / 1024 / 1024
  46. if max_allowed_packet_mb <= 4
  47. printf "\e[31m" # ANSI red
  48. puts <<~MSG
  49. Error: Database config value 'max_allowed_packet' too small (#{max_allowed_packet_mb}MB)
  50. Please increase this value in your #{vendor} configuration (64MB+ recommended).
  51. MSG
  52. printf "\e[0m" # ANSI normal
  53. exit 1
  54. end
  55. if connection.execute("SHOW tables LIKE 'settings';").any? &&
  56. Setting.get('postmaster_max_size').present? &&
  57. Setting.get('postmaster_max_size').to_i > max_allowed_packet_mb
  58. printf "\e[33m" # ANSI yellow
  59. puts <<~MSG
  60. Warning: Database config value 'max_allowed_packet' less than Zammad setting 'Maximum Email Size'
  61. Zammad will fail to process emails (both incoming and outgoing)
  62. larger than the value of 'max_allowed_packet' (#{max_allowed_packet_mb}MB).
  63. Please increase this value in your #{vendor} configuration accordingly.
  64. MSG
  65. printf "\e[0m" # ANSI normal
  66. end
  67. # rubocop:enable Rails/Exit
  68. # rubocop:enable Rails/Output
  69. # rubocop:enable Layout/IndentHeredoc