Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for INDEX INCLUDE #1301

Merged
merged 2 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

#### Added

- [#1301](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1301) Add support for INDEX INCLUDE.

#### Changed

- [#1273](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1273) TinyTDS v3+ is now required.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module SQLServer
class SchemaCreation < SchemaCreation
private

delegate :quoted_include_columns_for_index, to: :@conn

def supports_index_using?
false
end
Expand Down Expand Up @@ -44,11 +46,16 @@ def visit_CreateIndexDefinition(o)
sql << "INDEX"
sql << "#{quote_column_name(index.name)} ON #{quote_table_name(index.table)}"
sql << "(#{quoted_columns(index)})"
sql << "INCLUDE (#{quoted_include_columns(index.include)})" if supports_index_include? && index.include
sql << "WHERE #{index.where}" if index.where

sql.join(" ")
end

def quoted_include_columns(o)
(String === o) ? o : quoted_include_columns_for_index(o)
end

def add_column_options!(sql, options)
sql << " DEFAULT #{quote_default_expression_for_column_definition(options[:default], options[:column])}" if options_include_default?(options)
if options[:collation].present?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def indexes(table_name)
name = index["index_name"]
unique = index["index_description"].match?(/unique/)
where = select_value("SELECT [filter_definition] FROM sys.indexes WHERE name = #{quote(name)}", "SCHEMA")
include_columns = index_include_columns(table_name, name)
orders = {}
columns = []

Expand All @@ -63,11 +64,31 @@ def indexes(table_name)
columns << column
end

indexes << IndexDefinition.new(table_name, name, unique, columns, where: where, orders: orders)
indexes << IndexDefinition.new(table_name, name, unique, columns, where: where, orders: orders, include: include_columns.presence)
end
end
end

def index_include_columns(table_name, index_name)
sql = <<~SQL
SELECT
ic.index_id,
c.name AS column_name
FROM
sys.indexes i
JOIN
sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
JOIN
sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE
i.object_id = OBJECT_ID('#{table_name}')
AND i.name = '#{index_name}'
AND ic.is_included_column = 1;
SQL

select_all(sql, "SCHEMA").map { |row| row["column_name"] }
end

def columns(table_name)
return [] if table_name.blank?

Expand Down Expand Up @@ -421,6 +442,15 @@ def schema_names
query_values(sql, "SCHEMA")
end

def quoted_include_columns_for_index(column_names) # :nodoc:
return quote_column_name(column_names) if column_names.is_a?(Symbol)

quoted_columns = column_names.each_with_object({}) do |name, result|
result[name.to_sym] = quote_column_name(name).dup
end
add_options_for_index_columns(quoted_columns).values.join(", ")
end

private

def data_source_sql(name = nil, type: nil)
Expand Down
4 changes: 4 additions & 0 deletions lib/active_record/connection_adapters/sqlserver_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ def supports_partial_index?
true
end

def supports_index_include?
true
end

def supports_expression_index?
false
end
Expand Down
Loading