Skip to content

Commit 1303688

Browse files
committed
A bit of Ruby clarity on the new #get_indexs method, was #__indexes method.
1 parent 63944ec commit 1303688

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ def remove_index(table_name, options = {})
557557

558558
def indexes(table_name, name = nil)
559559
ActiveRecord::Base.connection.instance_variable_get("@connection")["AutoCommit"] = false
560-
__indexes(table_name, name)
560+
get_indexes(table_name,name)
561561
ensure
562562
ActiveRecord::Base.connection.instance_variable_get("@connection")["AutoCommit"] = true
563563
end
@@ -802,25 +802,26 @@ def remove_default_constraint(table_name, column_name)
802802
end
803803

804804
def remove_indexes(table_name, column_name)
805-
__indexes(table_name).select {|idx| idx.columns.include? column_name }.each do |idx|
806-
remove_index(table_name, {:name => idx.name})
805+
get_indexes(table_name).select{ |index| index.columns.include?(column_name.to_s) }.each do |index|
806+
remove_index(table_name, {:name => index.name})
807807
end
808808
end
809809

810-
def __indexes(table_name, name = nil)
811-
indexes = []
812-
execute("EXEC sp_helpindex #{quote_table_name(table_name)}", name) do |handle|
813-
if handle.column_info.any?
814-
handle.each do |index|
815-
unique = index[1] =~ /unique/
816-
primary = index[1] =~ /primary key/
817-
if !primary
818-
indexes << IndexDefinition.new(table_name, index[0], unique, index[2].split(", ").map {|e| e.gsub('(-)','')})
819-
end
810+
def get_indexes(table_name, name = nil)
811+
select("EXEC sp_helpindex #{quote_table_name(table_name)}",name).inject([]) do |indexes,index|
812+
if index['index_description'] =~ /primary key/
813+
indexes
814+
else
815+
name = index['index_name']
816+
unique = index['index_description'] =~ /unique/
817+
columns = index['index_keys'].split(',').map do |column|
818+
column.strip!
819+
column.gsub! '(-)', '' if column.ends_with?('(-)')
820+
column
820821
end
822+
indexes << IndexDefinition.new(table_name, name, unique, columns)
821823
end
822824
end
823-
indexes
824825
end
825826

826827
# IDENTITY INSERTS =========================================#

test/cases/adapter_test_sqlserver.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,18 @@ def setup
200200
context 'For indexes' do
201201

202202
setup do
203-
@connection.execute "CREATE INDEX idx_credit_limit ON accounts (credit_limit DESC)" rescue nil
203+
@desc_index_name = 'idx_credit_limit_test_desc'
204+
@connection.execute "CREATE INDEX #{@desc_index_name} ON accounts (credit_limit DESC)"
204205
end
205206

206207
teardown do
207-
@connection.execute "DROP INDEX accounts.idx_credit_limit"
208+
@connection.execute "DROP INDEX accounts.#{@desc_index_name}"
208209
end
209-
210+
210211
should 'have indexes with descending order' do
211-
assert_equal ["credit_limit"], @connection.indexes('accounts').first.columns
212+
assert @connection.indexes('accounts').detect { |i| i.name == @desc_index_name }
212213
end
213-
214+
214215
end
215216

216217

0 commit comments

Comments
 (0)