Skip to content

[Rails 6.1] Fix support for different index types #1006

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

Merged
merged 1 commit into from
Feb 10, 2022
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

#### Fixed

- [#1006](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1006) Fix support for index types

## v6.1.2.1

[Full changelog](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/compare/v6.1.2.0...v6.1.2.1)
Expand Down
20 changes: 11 additions & 9 deletions lib/active_record/connection_adapters/sqlserver/schema_creation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ def visit_TableDefinition(o)
end

def visit_CreateIndexDefinition(o)
if_not_exists = o.if_not_exists

o.if_not_exists = false

sql = super
index = o.index

if if_not_exists
sql = "IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = '#{o.index.name}') #{sql}"
end
sql = []
sql << "IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = '#{o.index.name}')" if o.if_not_exists
sql << "CREATE"
sql << "UNIQUE" if index.unique
sql << index.type.upcase if index.type
sql << "INDEX"
sql << "#{quote_column_name(index.name)} ON #{quote_table_name(index.table)}"
sql << "(#{quoted_columns(index)})"
sql << "WHERE #{index.where}" if index.where

sql
sql.join(" ")
end

def add_column_options!(sql, options)
Expand Down
55 changes: 55 additions & 0 deletions test/cases/active_schema_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require "cases/helper_sqlserver"

class ActiveSchemaTestSQLServer < ActiveRecord::TestCase
before do
connection.create_table :schema_test_table, force: true, id: false do |t|
t.column :foo, :string, limit: 100
t.column :state, :string
end
end

after do
connection.drop_table :schema_test_table rescue nil
end

it 'default index' do
assert_sql('CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
connection.add_index :schema_test_table, "foo"
end
end

it 'unique index' do
assert_sql('CREATE UNIQUE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
connection.add_index :schema_test_table, "foo", unique: true
end
end

it 'where condition on index' do
assert_sql("CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo]) WHERE state = 'active'") do
connection.add_index :schema_test_table, "foo", where: "state = 'active'"
end
end

it 'if index does not exist' do
assert_sql("IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = 'index_schema_test_table_on_foo') " \
"CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])") do
connection.add_index :schema_test_table, "foo", if_not_exists: true
end
end

describe "index types" do
it 'clustered index' do
assert_sql('CREATE CLUSTERED INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
connection.add_index :schema_test_table, "foo", type: :clustered
end
end

it 'nonclustered index' do
assert_sql('CREATE NONCLUSTERED INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
connection.add_index :schema_test_table, "foo", type: :nonclustered
end
end
end
end