From d7115a6be5a0c0eabc9e82ed50032533a6e85f0e Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 20 Oct 2016 23:18:26 +0200 Subject: [PATCH] Fixed issue#237 - added database version check at app boot sequence - thanks to @zotto and @qpwr --- config/initializers/db_preferences.rb | 11 ------ config/initializers/db_preferences_mysql.rb | 39 +++++++++++++++++++ .../initializers/db_preferences_postgresql.rb | 22 +++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 config/initializers/db_preferences_mysql.rb create mode 100644 config/initializers/db_preferences_postgresql.rb diff --git a/config/initializers/db_preferences.rb b/config/initializers/db_preferences.rb index 2a8dce1d6..66cf8856c 100644 --- a/config/initializers/db_preferences.rb +++ b/config/initializers/db_preferences.rb @@ -4,14 +4,3 @@ Rails.application.config.db_case_sensitive = false Rails.application.config.db_like = 'LIKE' Rails.application.config.db_4bytes_utf8 = true - -# postgresql -if ActiveRecord::Base.connection_config[:adapter] == 'postgresql' - Rails.application.config.db_case_sensitive = true - Rails.application.config.db_like = 'ILIKE' -end - -# mysql -if ActiveRecord::Base.connection_config[:adapter] == 'mysql2' - Rails.application.config.db_4bytes_utf8 = false -end diff --git a/config/initializers/db_preferences_mysql.rb b/config/initializers/db_preferences_mysql.rb new file mode 100644 index 000000000..b5374e142 --- /dev/null +++ b/config/initializers/db_preferences_mysql.rb @@ -0,0 +1,39 @@ +# mysql +if ActiveRecord::Base.connection_config[:adapter] == 'mysql2' + Rails.application.config.db_4bytes_utf8 = false + + # mysql version check + # mysql example: "5.7.3" + # mariadb example: "10.1.17-MariaDB" + server_version = ActiveRecord::Base.connection.select_rows('SHOW VARIABLES LIKE \'version\'')[0][1] + raise 'Unable to retrive database version' if !server_version + (server_version, server_vendor) = server_version.split('-') + if !server_vendor + server_vendor = 'MySQL' + end + (major, minor) = server_version.split('.') + if server_vendor == 'MySQL' + if major.to_i < 5 || (major.to_i == 5 && minor.to_i < 6) + + # rubocop:disable Rails/Output + # rubocop:disable Rails/Exit + p '+++++++++++++++++++++++++++++++++++++++++++++++++++++' + p '+ I\'m sorry, MySQL 5.6+ is required +' + p '+++++++++++++++++++++++++++++++++++++++++++++++++++++' + exit 1 + # rubocop:enable Rails/Exit + # rubocop:enable Rails/Output + end + elsif major.to_i < 10 + + # rubocop:disable Rails/Output + # rubocop:disable Rails/Exit + p '+++++++++++++++++++++++++++++++++++++++++++++++++++++' + p '+ I\'m sorry, MariaDB 10.0+ is required +' + p '+++++++++++++++++++++++++++++++++++++++++++++++++++++' + exit 1 + # rubocop:enable Rails/Exit + # rubocop:enable Rails/Output + + end +end diff --git a/config/initializers/db_preferences_postgresql.rb b/config/initializers/db_preferences_postgresql.rb new file mode 100644 index 000000000..8f72504c4 --- /dev/null +++ b/config/initializers/db_preferences_postgresql.rb @@ -0,0 +1,22 @@ +# postgresql +if ActiveRecord::Base.connection_config[:adapter] == 'postgresql' + Rails.application.config.db_case_sensitive = true + Rails.application.config.db_like = 'ILIKE' + + # postgresql version check + # example output: "9.5.0" + server_version = ActiveRecord::Base.connection.select_rows('SHOW server_version;')[0][0] + (major, minor) = server_version.split('.') + if major.to_i < 9 || (major.to_i == 9 && minor.to_i < 1) + + # rubocop:disable Rails/Output + # rubocop:disable Rails/Exit + p '+++++++++++++++++++++++++++++++++++++++++++++++++++++' + p '+ I\'m sorry, PostgreSQL 9.1+ is required +' + p '+++++++++++++++++++++++++++++++++++++++++++++++++++++' + exit 1 + # rubocop:enable Rails/Exit + # rubocop:enable Rails/Output + + end +end