Skip to content

Commit 54da53c

Browse files
authored
Merge pull request #1006 from aidanharan/index-types-61
[Rails 6.1] Fix support for different index types
2 parents 712e549 + 045e954 commit 54da53c

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
#### Fixed
4+
5+
- [#1006](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1006) Fix support for index types
6+
17
## v6.1.2.1
28

39
[Full changelog](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/compare/v6.1.2.0...v6.1.2.1)

lib/active_record/connection_adapters/sqlserver/schema_creation.rb

+11-9
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,19 @@ def visit_TableDefinition(o)
3434
end
3535

3636
def visit_CreateIndexDefinition(o)
37-
if_not_exists = o.if_not_exists
38-
39-
o.if_not_exists = false
40-
41-
sql = super
37+
index = o.index
4238

43-
if if_not_exists
44-
sql = "IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = '#{o.index.name}') #{sql}"
45-
end
39+
sql = []
40+
sql << "IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = '#{o.index.name}')" if o.if_not_exists
41+
sql << "CREATE"
42+
sql << "UNIQUE" if index.unique
43+
sql << index.type.upcase if index.type
44+
sql << "INDEX"
45+
sql << "#{quote_column_name(index.name)} ON #{quote_table_name(index.table)}"
46+
sql << "(#{quoted_columns(index)})"
47+
sql << "WHERE #{index.where}" if index.where
4648

47-
sql
49+
sql.join(" ")
4850
end
4951

5052
def add_column_options!(sql, options)
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper_sqlserver"
4+
5+
class ActiveSchemaTestSQLServer < ActiveRecord::TestCase
6+
before do
7+
connection.create_table :schema_test_table, force: true, id: false do |t|
8+
t.column :foo, :string, limit: 100
9+
t.column :state, :string
10+
end
11+
end
12+
13+
after do
14+
connection.drop_table :schema_test_table rescue nil
15+
end
16+
17+
it 'default index' do
18+
assert_sql('CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
19+
connection.add_index :schema_test_table, "foo"
20+
end
21+
end
22+
23+
it 'unique index' do
24+
assert_sql('CREATE UNIQUE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
25+
connection.add_index :schema_test_table, "foo", unique: true
26+
end
27+
end
28+
29+
it 'where condition on index' do
30+
assert_sql("CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo]) WHERE state = 'active'") do
31+
connection.add_index :schema_test_table, "foo", where: "state = 'active'"
32+
end
33+
end
34+
35+
it 'if index does not exist' do
36+
assert_sql("IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = 'index_schema_test_table_on_foo') " \
37+
"CREATE INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])") do
38+
connection.add_index :schema_test_table, "foo", if_not_exists: true
39+
end
40+
end
41+
42+
describe "index types" do
43+
it 'clustered index' do
44+
assert_sql('CREATE CLUSTERED INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
45+
connection.add_index :schema_test_table, "foo", type: :clustered
46+
end
47+
end
48+
49+
it 'nonclustered index' do
50+
assert_sql('CREATE NONCLUSTERED INDEX [index_schema_test_table_on_foo] ON [schema_test_table] ([foo])') do
51+
connection.add_index :schema_test_table, "foo", type: :nonclustered
52+
end
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)