From b798c5b796f58a9286e1a037db4453750cd26f95 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sat, 23 Sep 2017 05:55:48 +0900 Subject: Prevent extra `column_for` for `change_column_{default,null,comment}` `change_column_{default,null,comment}` in mysql2 adapter are passing `column.sql_type` as `type` to `change_column` to intend keeping previous type. But `column_for` requires extra query, so use passing `nil` to `type` explicitly in the internal for the purpose. --- .../connection_adapters/abstract_mysql_adapter.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 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 46ab8f3edd..ae991d3d79 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -356,23 +356,19 @@ module ActiveRecord def change_column_default(table_name, column_name, default_or_changes) #:nodoc: default = extract_new_default_value(default_or_changes) - column = column_for(table_name, column_name) - change_column table_name, column_name, column.sql_type, default: default + change_column table_name, column_name, nil, default: default end def change_column_null(table_name, column_name, null, default = nil) #:nodoc: - column = column_for(table_name, column_name) - unless null || default.nil? execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL") end - change_column table_name, column_name, column.sql_type, null: null + change_column table_name, column_name, nil, null: null end def change_column_comment(table_name, column_name, comment) #:nodoc: - column = column_for(table_name, column_name) - change_column table_name, column_name, column.sql_type, comment: comment + change_column table_name, column_name, nil, comment: comment end def change_column(table_name, column_name, type, options = {}) #:nodoc: @@ -678,6 +674,7 @@ module ActiveRecord def change_column_sql(table_name, column_name, type, options = {}) column = column_for(table_name, column_name) + type ||= column.sql_type unless options.key?(:default) options[:default] = column.default -- cgit v1.2.3