|
| 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