diff options
author | Aaron Stone <aaron@serendipity.cx> | 2012-11-27 00:57:45 -0800 |
---|---|---|
committer | Aaron Stone <aaron@serendipity.cx> | 2012-12-08 10:11:10 -0800 |
commit | 97d06e8c4bd44154abda41a3f7e4bc49929a919f (patch) | |
tree | 33bad0ef6d7bd8566e0e397d9db7e3d6cfb6a208 /activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb | |
parent | 0a33fcd68bb2c56b8c2eba066f343484a44fa588 (diff) | |
download | rails-97d06e8c4bd44154abda41a3f7e4bc49929a919f.tar.gz rails-97d06e8c4bd44154abda41a3f7e4bc49929a919f.tar.bz2 rails-97d06e8c4bd44154abda41a3f7e4bc49929a919f.zip |
Session variables for mysql, mysql2, and postgresql adapters can be set
in the new 'variables:' hash in each database config section in database.yml.
The key-value pairs of this hash will be sent in a 'SET key = value, ...'
query on new database connections.
The configure_connection methods from mysql and mysql2 into are
consolidated into the abstract_mysql base class.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 84e73e6f0f..d37e489f5c 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -704,6 +704,45 @@ module ActiveRecord end column end + + def configure_connection + variables = @config[:variables] || {} + + # 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 + + # 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] = 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' + end + + # NAMES does not have an equals sign, see + # http://dev.mysql.com/doc/refman/5.0/en/set-statement.html#id944430 + # (trailing comma because variable_assignments will always have content) + encoding = "NAMES #{@config[:encoding]}, " if @config[:encoding] + + # Gather up all of the SET variables... + variable_assignments = variables.map do |k, v| + if v == ':default' || v == :default + "@@SESSION.#{k.to_s} = DEFAULT" # Sets the value to the global or compile default + elsif !v.nil? + "@@SESSION.#{k.to_s} = #{quote(v)}" + end + # or else nil; compact to clear nils out + end.compact.join(', ') + + # ...and send them all in one query + execute("SET #{encoding} #{variable_assignments}", :skip_logging) + end + end end end |