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

Add config option, rdoc, tests for mysql(2) STRICT_ALL_TABLES mode.

This commit is contained in:
Michael Pearson 2012-05-05 17:11:33 +10:00
parent 4b905606e3
commit 7c4d331133
4 changed files with 38 additions and 4 deletions

View file

@ -256,8 +256,10 @@ module ActiveRecord
# 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
variable_assignments << "SQL_MODE='STRICT_ALL_TABLES'"
# http://dev.mysql.com/doc/refman/5.5/en/server-sql-mode.html#sqlmode_strict_all_tables
if @config.fetch(:strict, true)
variable_assignments << "SQL_MODE='STRICT_ALL_TABLES'"
end
encoding = @config[:encoding]

View file

@ -53,6 +53,7 @@ module ActiveRecord
# * <tt>:database</tt> - The name of the database. No default, must be provided.
# * <tt>:encoding</tt> - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
# * <tt>:reconnect</tt> - Defaults to false (See MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html).
# * <tt>:strict</tt> - Defaults to true. Enable STRICT_ALL_TABLES. (See MySQL documentation: http://dev.mysql.com/doc/refman/5.5/en/server-sql-mode.html)
# * <tt>:sslca</tt> - Necessary to use MySQL with an SSL connection.
# * <tt>:sslkey</tt> - Necessary to use MySQL with an SSL connection.
# * <tt>:sslcert</tt> - Necessary to use MySQL with an SSL connection.
@ -407,8 +408,10 @@ module ActiveRecord
# 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
execute("SET SQL_MODE='STRICT_ALL_TABLES'", :skip_logging)
# http://dev.mysql.com/doc/refman/5.5/en/server-sql-mode.html#sqlmode_strict_all_tables
if @config.fetch(:strict, true)
execute("SET SQL_MODE='STRICT_ALL_TABLES'", :skip_logging)
end
end
def select(sql, name = nil, binds = [])

View file

@ -120,6 +120,19 @@ class MysqlConnectionTest < ActiveRecord::TestCase
end
end
def test_mysql_default_in_strict_mode
result = @connection.exec_query "SELECT @@SESSION.sql_mode"
assert_equal [["STRICT_ALL_TABLES"]], result.rows
end
def test_mysql_strict_mode_disabled
run_without_connection do |orig_connection|
ActiveRecord::Model.establish_connection(orig_connection.merge({:strict => false}))
result = ActiveRecord::Model.connection.exec_query "SELECT @@SESSION.sql_mode"
assert_equal [['']], result.rows
end
end
private
def run_without_connection

View file

@ -29,6 +29,22 @@ class MysqlConnectionTest < ActiveRecord::TestCase
assert @connection.active?
end
# TODO: Below is a straight up copy/paste from mysql/connection_test.rb
# I'm not sure what the correct way is to share these tests between
# adapters in minitest.
def test_mysql_default_in_strict_mode
result = @connection.exec_query "SELECT @@SESSION.sql_mode"
assert_equal [["STRICT_ALL_TABLES"]], result.rows
end
def test_mysql_strict_mode_disabled
run_without_connection do |orig_connection|
ActiveRecord::Model.establish_connection(orig_connection.merge({:strict => false}))
result = ActiveRecord::Model.connection.exec_query "SELECT @@SESSION.sql_mode"
assert_equal [['']], result.rows
end
end
private
def run_without_connection