@@ -768,7 +768,7 @@ def index_name(table_name, options) #:nodoc:
768768 raise ArgumentError , "You must specify the index name"
769769 end
770770 else
771- index_name ( table_name , column : options )
771+ index_name ( table_name , index_name_options ( options ) )
772772 end
773773 end
774774
@@ -1123,18 +1123,14 @@ def update_table_definition(table_name, base) #:nodoc:
11231123 end
11241124
11251125 def add_index_options ( table_name , column_name , comment : nil , **options ) # :nodoc:
1126- if column_name . is_a? ( String ) && /\W / . match? ( column_name )
1127- column_names = column_name
1128- else
1129- column_names = Array ( column_name )
1130- end
1126+ column_names = index_column_names ( column_name )
11311127
11321128 options . assert_valid_keys ( :unique , :order , :name , :where , :length , :internal , :using , :algorithm , :type )
11331129
11341130 index_type = options [ :type ] . to_s if options . key? ( :type )
11351131 index_type ||= options [ :unique ] ? "UNIQUE" : ""
11361132 index_name = options [ :name ] . to_s if options . key? ( :name )
1137- index_name ||= index_name ( table_name , index_name_options ( column_names ) )
1133+ index_name ||= index_name ( table_name , column_names )
11381134
11391135 if options . key? ( :algorithm )
11401136 algorithm = index_algorithms . fetch ( options [ :algorithm ] ) {
@@ -1214,13 +1210,13 @@ def index_name_for_remove(table_name, options = {})
12141210
12151211 if options . is_a? ( Hash )
12161212 checks << lambda { |i | i . name == options [ :name ] . to_s } if options . key? ( :name )
1217- column_names = Array ( options [ :column ] ) . map ( & :to_s )
1213+ column_names = index_column_names ( options [ :column ] )
12181214 else
1219- column_names = Array ( options ) . map ( & :to_s )
1215+ column_names = index_column_names ( options )
12201216 end
12211217
1222- if column_names . any ?
1223- checks << lambda { |i | i . columns . join ( "_and_" ) == column_names . join ( "_and_" ) }
1218+ if column_names . present ?
1219+ checks << lambda { |i | index_name ( table_name , i . columns ) == index_name ( table_name , column_names ) }
12241220 end
12251221
12261222 raise ArgumentError , "No name or columns specified" if checks . none?
@@ -1267,8 +1263,16 @@ def create_alter_table(name)
12671263 AlterTable . new create_table_definition ( name )
12681264 end
12691265
1266+ def index_column_names ( column_names )
1267+ if column_names . is_a? ( String ) && /\W / . match? ( column_names )
1268+ column_names
1269+ else
1270+ Array ( column_names )
1271+ end
1272+ end
1273+
12701274 def index_name_options ( column_names )
1271- if column_names . is_a? ( String )
1275+ if column_names . is_a? ( String ) && / \W / . match? ( column_names )
12721276 column_names = column_names . scan ( /\w +/ ) . join ( "_" )
12731277 end
12741278
0 commit comments