Skip to content

Commit 56b5608

Browse files
committed
Remove indexes before removing a column.
The migration tests indicate that we should be able to remove columns that have indexes on them. SQLServer doesn't like that, so we'll attempt to remove an indexes on this column before we remove the column. Had to reorg the indexes method into a private a public one. The public one messes with the autocommit status of the connection for somereason and that was breaking the remove_column call. Maybe the public one doesn't need to do this though.
1 parent 10142fb commit 56b5608

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -453,19 +453,7 @@ def tables(name = nil)
453453

454454
def indexes(table_name, name = nil)
455455
ActiveRecord::Base.connection.instance_variable_get("@connection")["AutoCommit"] = false
456-
indexes = []
457-
execute("EXEC sp_helpindex '#{table_name}'", name) do |handle|
458-
if handle.column_info.any?
459-
handle.each do |index|
460-
unique = index[1] =~ /unique/
461-
primary = index[1] =~ /primary key/
462-
if !primary
463-
indexes << IndexDefinition.new(table_name, index[0], unique, index[2].split(", ").map {|e| e.gsub('(-)','')})
464-
end
465-
end
466-
end
467-
end
468-
indexes
456+
__indexes(table_name, name)
469457
ensure
470458
ActiveRecord::Base.connection.instance_variable_get("@connection")["AutoCommit"] = true
471459
end
@@ -507,6 +495,7 @@ def change_column_default(table_name, column_name, default)
507495
def remove_column(table_name, column_name)
508496
remove_check_constraints(table_name, column_name)
509497
remove_default_constraint(table_name, column_name)
498+
remove_indexes(table_name, column_name)
510499
execute "ALTER TABLE [#{table_name}] DROP COLUMN #{quote_column_name(column_name)}"
511500
end
512501

@@ -526,11 +515,33 @@ def remove_check_constraints(table_name, column_name)
526515
end
527516
end
528517

518+
def remove_indexes(table_name, column_name)
519+
__indexes(table_name).select {|idx| idx.columns.include? column_name }.each do |idx|
520+
remove_index(table_name, {:name => idx.name})
521+
end
522+
end
523+
529524
def remove_index(table_name, options = {})
530525
execute "DROP INDEX #{table_name}.#{quote_column_name(index_name(table_name, options))}"
531526
end
532527

533-
private
528+
private
529+
def __indexes(table_name, name = nil)
530+
indexes = []
531+
execute("EXEC sp_helpindex '#{table_name}'", name) do |handle|
532+
if handle.column_info.any?
533+
handle.each do |index|
534+
unique = index[1] =~ /unique/
535+
primary = index[1] =~ /primary key/
536+
if !primary
537+
indexes << IndexDefinition.new(table_name, index[0], unique, index[2].split(", ").map {|e| e.gsub('(-)','')})
538+
end
539+
end
540+
end
541+
end
542+
indexes
543+
end
544+
534545
def select(sql, name = nil)
535546
repair_special_columns(sql)
536547

0 commit comments

Comments
 (0)