@@ -766,7 +766,7 @@ def index_name(table_name, options) #:nodoc:
766766 raise ArgumentError , "You must specify the index name"
767767 end
768768 else
769- index_name ( table_name , column : options )
769+ index_name ( table_name , index_name_options ( options ) )
770770 end
771771 end
772772
@@ -1120,18 +1120,14 @@ def update_table_definition(table_name, base) #:nodoc:
11201120 end
11211121
11221122 def add_index_options ( table_name , column_name , comment : nil , **options ) # :nodoc:
1123- if column_name . is_a? ( String ) && /\W / . match? ( column_name )
1124- column_names = column_name
1125- else
1126- column_names = Array ( column_name )
1127- end
1123+ column_names = index_column_names ( column_name )
11281124
11291125 options . assert_valid_keys ( :unique , :order , :name , :where , :length , :internal , :using , :algorithm , :type )
11301126
11311127 index_type = options [ :type ] . to_s if options . key? ( :type )
11321128 index_type ||= options [ :unique ] ? "UNIQUE" : ""
11331129 index_name = options [ :name ] . to_s if options . key? ( :name )
1134- index_name ||= index_name ( table_name , index_name_options ( column_names ) )
1130+ index_name ||= index_name ( table_name , column_names )
11351131
11361132 if options . key? ( :algorithm )
11371133 algorithm = index_algorithms . fetch ( options [ :algorithm ] ) {
@@ -1208,13 +1204,13 @@ def index_name_for_remove(table_name, options = {}) # :doc:
12081204
12091205 if options . is_a? ( Hash )
12101206 checks << lambda { |i | i . name == options [ :name ] . to_s } if options . key? ( :name )
1211- column_names = Array ( options [ :column ] ) . map ( & :to_s )
1207+ column_names = index_column_names ( options [ :column ] )
12121208 else
1213- column_names = Array ( options ) . map ( & :to_s )
1209+ column_names = index_column_names ( options )
12141210 end
12151211
1216- if column_names . any ?
1217- checks << lambda { |i | i . columns . join ( "_and_" ) == column_names . join ( "_and_" ) }
1212+ if column_names . present ?
1213+ checks << lambda { |i | index_name ( table_name , i . columns ) == index_name ( table_name , column_names ) }
12181214 end
12191215
12201216 raise ArgumentError , "No name or columns specified" if checks . none?
@@ -1261,8 +1257,16 @@ def create_alter_table(name)
12611257 AlterTable . new create_table_definition ( name )
12621258 end
12631259
1260+ def index_column_names ( column_names )
1261+ if column_names . is_a? ( String ) && /\W / . match? ( column_names )
1262+ column_names
1263+ else
1264+ Array ( column_names )
1265+ end
1266+ end
1267+
12641268 def index_name_options ( column_names )
1265- if column_names . is_a? ( String )
1269+ if column_names . is_a? ( String ) && / \W / . match? ( column_names )
12661270 column_names = column_names . scan ( /\w +/ ) . join ( "_" )
12671271 end
12681272
0 commit comments