1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Stringify variables names for mysql connections

For mysql2/mysql adapters, `sql_mode` variable name set in `database.yml`
as string, was ignored and `sql_mode` was set to use strict mode.

Fixes #14895
This commit is contained in:
Paul Nikitochkin 2014-04-29 01:39:47 +03:00
parent fd92437112
commit 7e8b062823
4 changed files with 30 additions and 5 deletions

View file

@ -1,3 +1,12 @@
* Stringify all variables keys of mysql connection configuration.
When `sql_mode` variable for mysql adapters set in configuration as `String`
was ignored and overwritten by strict mode option.
Fixes #14895
*Paul Nikitochkin*
* When using a custom `join_table` name on a `habtm`, rails was not saving it
on Reflections. This causes a problem when rails loads fixtures, because it
uses the reflections to set database with fixtures.

View file

@ -765,22 +765,22 @@ module ActiveRecord
end
def configure_connection
variables = @config[:variables] || {}
variables = @config.fetch(:variables, {}).stringify_keys
# By default, MySQL 'where id is null' selects the last inserted id.
# Turn this off. http://dev.rubyonrails.org/ticket/6778
variables[:sql_auto_is_null] = 0
variables['sql_auto_is_null'] = 0
# Increase timeout so the server doesn't disconnect us.
wait_timeout = @config[:wait_timeout]
wait_timeout = 2147483 unless wait_timeout.is_a?(Fixnum)
variables[:wait_timeout] = self.class.type_cast_config_to_integer(wait_timeout)
variables['wait_timeout'] = self.class.type_cast_config_to_integer(wait_timeout)
# Make MySQL reject illegal values rather than truncating or blanking them, see
# http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_all_tables
# If the user has provided another value for sql_mode, don't replace it.
if strict_mode? && !variables.has_key?(:sql_mode)
variables[:sql_mode] = 'STRICT_ALL_TABLES'
if strict_mode? && !variables.has_key?('sql_mode')
variables['sql_mode'] = 'STRICT_ALL_TABLES'
end
# NAMES does not have an equals sign, see

View file

@ -151,6 +151,14 @@ class MysqlConnectionTest < ActiveRecord::TestCase
end
end
def test_mysql_sql_mode_variable_overides_strict_mode
run_without_connection do |orig_connection|
ActiveRecord::Base.establish_connection(orig_connection.deep_merge(variables: { 'sql_mode' => 'ansi' }))
result = ActiveRecord::Base.connection.exec_query 'SELECT @@SESSION.sql_mode'
assert_not_equal [['STRICT_ALL_TABLES']], result.rows
end
end
def test_mysql_set_session_variable_to_default
run_without_connection do |orig_connection|
ActiveRecord::Base.establish_connection(orig_connection.deep_merge({:variables => {:default_week_format => :default}}))

View file

@ -77,6 +77,14 @@ class MysqlConnectionTest < ActiveRecord::TestCase
end
end
def test_mysql_sql_mode_variable_overides_strict_mode
run_without_connection do |orig_connection|
ActiveRecord::Base.establish_connection(orig_connection.deep_merge(variables: { 'sql_mode' => 'ansi' }))
result = ActiveRecord::Base.connection.exec_query 'SELECT @@SESSION.sql_mode'
assert_not_equal [['STRICT_ALL_TABLES']], result.rows
end
end
def test_mysql_set_session_variable_to_default
run_without_connection do |orig_connection|
ActiveRecord::Base.establish_connection(orig_connection.deep_merge({:variables => {:default_week_format => :default}}))