From 1ec356a0b5b6594ef0df3fb764cb57c0859ca86f Mon Sep 17 00:00:00 2001 From: Jan Provaznik Date: Sat, 9 Jun 2018 20:13:50 +0200 Subject: [PATCH] Mysql fixes for Rails 5 * `MysqlDateTimeWithTimeZone` inherits from `ActiveRecord::Type::DateTime` (`MysqlDateTime` is not present in Rails 5) * explicitly set `NULL` default value for `merge_request_diff_files`'s `diff` column (otherwise empty string is used in a migration) and empty string is not allowed for text/blob fields in Mysql * disable NO_ZERO_DATE mode for all Mysql DB connections, otherwise SQL queries fail on inserting `0` value for `created_at` column --- config/application.rb | 17 +++++++++++------ .../initializers/active_record_data_types.rb | 2 +- .../merge_request_diff_file_limits_to_mysql.rb | 2 +- lib/mysql_zero_date.rb | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 lib/mysql_zero_date.rb diff --git a/config/application.rb b/config/application.rb index 202e5d5e327..d9483cd806d 100644 --- a/config/application.rb +++ b/config/application.rb @@ -5,6 +5,12 @@ require 'rails/all' Bundler.require(:default, Rails.env) module Gitlab + # This method is used for smooth upgrading from the current Rails 4.x to Rails 5.0. + # https://gitlab.com/gitlab-org/gitlab-ce/issues/14286 + def self.rails5? + ENV["RAILS5"].in?(%w[1 true]) + end + class Application < Rails::Application require_dependency Rails.root.join('lib/gitlab/redis/wrapper') require_dependency Rails.root.join('lib/gitlab/redis/cache') @@ -14,6 +20,11 @@ module Gitlab require_dependency Rails.root.join('lib/gitlab/current_settings') require_dependency Rails.root.join('lib/gitlab/middleware/read_only') + # This needs to be loaded before DB connection is made + # to make sure that all connections have NO_ZERO_DATE + # setting disabled + require_dependency Rails.root.join('lib/mysql_zero_date') + # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. @@ -211,10 +222,4 @@ module Gitlab Gitlab::Routing.add_helpers(MilestonesRoutingHelper) end end - - # This method is used for smooth upgrading from the current Rails 4.x to Rails 5.0. - # https://gitlab.com/gitlab-org/gitlab-ce/issues/14286 - def self.rails5? - ENV["RAILS5"].in?(%w[1 true]) - end end diff --git a/config/initializers/active_record_data_types.rb b/config/initializers/active_record_data_types.rb index fda13d0c4cb..717e30b5b7e 100644 --- a/config/initializers/active_record_data_types.rb +++ b/config/initializers/active_record_data_types.rb @@ -65,7 +65,7 @@ elsif Gitlab::Database.mysql? prepend RegisterDateTimeWithTimeZone # Add the class `DateTimeWithTimeZone` so we can map `timestamp` to it. - class MysqlDateTimeWithTimeZone < MysqlDateTime + class MysqlDateTimeWithTimeZone < (Gitlab.rails5? ? ActiveRecord::Type::DateTime : MysqlDateTime) def type :datetime_with_timezone end diff --git a/db/migrate/merge_request_diff_file_limits_to_mysql.rb b/db/migrate/merge_request_diff_file_limits_to_mysql.rb index 3958380e4b9..ca3bc7d6be9 100644 --- a/db/migrate/merge_request_diff_file_limits_to_mysql.rb +++ b/db/migrate/merge_request_diff_file_limits_to_mysql.rb @@ -4,7 +4,7 @@ class MergeRequestDiffFileLimitsToMysql < ActiveRecord::Migration def up return unless Gitlab::Database.mysql? - change_column :merge_request_diff_files, :diff, :text, limit: 2147483647 + change_column :merge_request_diff_files, :diff, :text, limit: 2147483647, default: nil end def down diff --git a/lib/mysql_zero_date.rb b/lib/mysql_zero_date.rb new file mode 100644 index 00000000000..64634f789da --- /dev/null +++ b/lib/mysql_zero_date.rb @@ -0,0 +1,18 @@ +# Disable NO_ZERO_DATE mode for mysql in rails 5. +# We use zero date as a default value +# (config/initializers/active_record_mysql_timestamp.rb), in +# Rails 5 using zero date fails by default (https://gitlab.com/gitlab-org/gitlab-ce/-/jobs/75450216) +# and NO_ZERO_DATE has to be explicitly disabled. Disabling strict mode +# is not sufficient. + +require 'active_record/connection_adapters/abstract_mysql_adapter' + +module MysqlZeroDate + def configure_connection + super + + @connection.query "SET @@SESSION.sql_mode = REPLACE(@@SESSION.sql_mode, 'NO_ZERO_DATE', '');" # rubocop:disable Gitlab/ModuleWithInstanceVariables + end +end + +ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.prepend(MysqlZeroDate) if Gitlab.rails5?