Skip to content

Commit

Permalink
Stringify variables names for mysql connections
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pftg committed Apr 30, 2014
1 parent fd92437 commit 7e8b062
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
9 changes: 9 additions & 0 deletions activerecord/CHANGELOG.md
@@ -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.
Expand Down
Expand Up @@ -765,22 +765,22 @@ def column_for(table_name, column_name)
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
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/adapters/mysql/connection_test.rb
Expand Up @@ -151,6 +151,14 @@ def test_mysql_set_session_variable
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}}))
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/adapters/mysql2/connection_test.rb
Expand Up @@ -77,6 +77,14 @@ def test_mysql_set_session_variable
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}}))
Expand Down

0 comments on commit 7e8b062

Please sign in to comment.