From 8bc1c4ad228c77610f11387d6282ef2c3a663e91 Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Sat, 11 Sep 2021 17:08:22 -0700 Subject: [PATCH] add warning to mysql2 default prepared_statements Co-authored-by: Thiago Araujo --- activerecord/CHANGELOG.md | 5 +++ .../connection_adapters/mysql2_adapter.rb | 9 ++++++ .../adapters/mysql2/mysql2_adapter_test.rb | 32 +++++++++++++++++++ activerecord/test/config.example.yml | 4 +++ 4 files changed, 50 insertions(+) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 10c2004177..988041ac79 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,8 @@ +* Add a deprecation warning when `prepared_statements` configuration is not + set for the mysql2 adapter. + + *Thiago Araujo and Stefanni Brasil* + * Fix `QueryMethods#in_order_of` to handle empty order list. ```ruby diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb index 9b915f4412..d7b3e29a70 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb @@ -56,6 +56,7 @@ module ActiveRecord end def initialize(connection, logger, connection_options, config) + check_prepared_statements_deprecation(config) superclass_config = config.reverse_merge(prepared_statements: false) super(connection, logger, connection_options, superclass_config) configure_connection @@ -140,6 +141,14 @@ module ActiveRecord end private + def check_prepared_statements_deprecation(config) + if !config.key?(:prepared_statements) + ActiveSupport::Deprecation.warn(<<-MSG.squish) + The default value of `prepared_statements` for the mysql2 adapter will be changed from +false+ to +true+ in Rails 7.2. + MSG + end + end + def connect @connection = self.class.new_client(@config) configure_connection diff --git a/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb b/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb index 18e37838aa..2cf7961934 100644 --- a/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb +++ b/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb @@ -40,6 +40,38 @@ class Mysql2AdapterTest < ActiveRecord::Mysql2TestCase end end + def test_mysql2_prepared_statements_default_deprecation_warning + fake_connection = Class.new do + def query_options + {} + end + + def query(*) + end + + def close + end + end.new + + assert_deprecated do + ActiveRecord::ConnectionAdapters::Mysql2Adapter.new( + fake_connection, + ActiveRecord::Base.logger, + nil, + { socket: File::NULL } + ) + end + + assert_not_deprecated do + ActiveRecord::ConnectionAdapters::Mysql2Adapter.new( + fake_connection, + ActiveRecord::Base.logger, + nil, + { socket: File::NULL, prepared_statements: false } + ) + end + end + def test_exec_query_nothing_raises_with_no_result_queries assert_nothing_raised do with_example_table do diff --git a/activerecord/test/config.example.yml b/activerecord/test/config.example.yml index 5d9882e2d1..9c356a3760 100644 --- a/activerecord/test/config.example.yml +++ b/activerecord/test/config.example.yml @@ -42,6 +42,8 @@ connections: collation: utf8mb4_unicode_ci <% if ENV['MYSQL_PREPARED_STATEMENTS'] %> prepared_statements: true +<% else %> + prepared_statements: false <% end %> <% if ENV['MYSQL_HOST'] %> host: <%= ENV['MYSQL_HOST'] %> @@ -55,6 +57,8 @@ connections: collation: utf8mb4_general_ci <% if ENV['MYSQL_PREPARED_STATEMENTS'] %> prepared_statements: true +<% else %> + prepared_statements: false <% end %> <% if ENV['MYSQL_HOST'] %> host: <%= ENV['MYSQL_HOST'] %>