Extract add_sql_comment! method

Refactor of #22911.

Signed-off-by: Jeremy Daer <jeremydaer@gmail.com>
This commit is contained in:
Ryuta Kamizono 2016-04-27 09:11:06 +09:00 committed by Jeremy Daer
parent 2db8514d21
commit 0eac27904e
No known key found for this signature in database
GPG Key ID: AB8F6399D5C60664
3 changed files with 13 additions and 20 deletions

@ -502,8 +502,12 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
def add_index(table_name, column_name, options = {}) #:nodoc:
index_name, index_type, index_columns, _, index_algorithm, index_using, comment = add_index_options(table_name, column_name, options)
sql = "CREATE #{index_type} INDEX #{quote_column_name(index_name)} #{index_using} ON #{quote_table_name(table_name)} (#{index_columns}) #{index_algorithm}"
execute add_sql_comment!(sql, comment)
end
def add_sql_comment!(sql, comment) # :nodoc:
sql << " COMMENT #{quote(comment)}" if comment
execute sql
sql
end
def foreign_keys(table_name)

@ -2,8 +2,8 @@ module ActiveRecord
module ConnectionAdapters
module MySQL
class SchemaCreation < AbstractAdapter::SchemaCreation
delegate :quote, to: :@conn
private :quote
delegate :add_sql_comment!, to: :@conn
private :add_sql_comment!
private
@ -26,11 +26,7 @@ def visit_ChangeColumnDefinition(o)
end
def add_table_options!(create_sql, options)
super
if comment = options[:comment]
create_sql << " COMMENT #{quote(comment)}"
end
add_sql_comment!(super, options[:comment])
end
def column_options(o)
@ -48,13 +44,7 @@ def add_column_options!(sql, options)
sql << " COLLATE #{collation}"
end
super
if comment = options[:comment]
sql << " COMMENT #{quote(comment)}"
end
sql
add_sql_comment!(super, options[:comment])
end
def add_column_position!(sql, options)
@ -69,8 +59,7 @@ def add_column_position!(sql, options)
def index_in_create(table_name, column_name, options)
index_name, index_type, index_columns, _, _, index_using, comment = @conn.add_index_options(table_name, column_name, options)
index_option = " COMMENT #{quote(comment)}" if comment
"#{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns})#{index_option} "
add_sql_comment!("#{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns})", comment)
end
end
end

@ -63,14 +63,14 @@ def test_index_in_create
def (ActiveRecord::Base.connection).data_source_exists?(*); false; end
%w(SPATIAL FULLTEXT UNIQUE).each do |type|
expected = "CREATE TABLE `people` (#{type} INDEX `index_people_on_last_name` (`last_name`) ) ENGINE=InnoDB"
expected = "CREATE TABLE `people` (#{type} INDEX `index_people_on_last_name` (`last_name`)) ENGINE=InnoDB"
actual = ActiveRecord::Base.connection.create_table(:people, id: false) do |t|
t.index :last_name, type: type
end
assert_equal expected, actual
end
expected = "CREATE TABLE `people` ( INDEX `index_people_on_last_name` USING btree (`last_name`(10)) ) ENGINE=InnoDB"
expected = "CREATE TABLE `people` ( INDEX `index_people_on_last_name` USING btree (`last_name`(10))) ENGINE=InnoDB"
actual = ActiveRecord::Base.connection.create_table(:people, id: false) do |t|
t.index :last_name, length: 10, using: :btree
end
@ -155,7 +155,7 @@ def test_indexes_in_create
ActiveRecord::Base.connection.stubs(:data_source_exists?).with(:temp).returns(false)
ActiveRecord::Base.connection.stubs(:index_name_exists?).with(:index_temp_on_zip).returns(false)
expected = "CREATE TEMPORARY TABLE `temp` ( INDEX `index_temp_on_zip` (`zip`) ) ENGINE=InnoDB AS SELECT id, name, zip FROM a_really_complicated_query"
expected = "CREATE TEMPORARY TABLE `temp` ( INDEX `index_temp_on_zip` (`zip`)) ENGINE=InnoDB AS SELECT id, name, zip FROM a_really_complicated_query"
actual = ActiveRecord::Base.connection.create_table(:temp, temporary: true, as: "SELECT id, name, zip FROM a_really_complicated_query") do |t|
t.index :zip
end