Skip to content

Commit d779517

Browse files
authored
Rails 6.1: Add support for if_not_exists to indexes (#891)
* add create index if exists support See rails/rails@7ba0803 * add changelog item
1 parent 5d50851 commit d779517

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [#861](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/861) Fix Rails 6.1 database config
1313
- [#890](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/890) Fix removal of invalid ordering from select statements
1414
- [#881](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/881) Dump column collation to schema.rb and allow collation changes using column_change
15+
- [#891](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/891) Add support for if_not_exists to indexes
1516
- [#892](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/892) Add support for if_exists on remove_column
1617

1718
#### Changed

lib/active_record/connection_adapters/sqlserver/schema_creation.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ def visit_TableDefinition(o)
3333
sql
3434
end
3535

36+
def visit_CreateIndexDefinition(o)
37+
if_not_exists = o.if_not_exists
38+
39+
o.if_not_exists = false
40+
41+
sql = super
42+
43+
if if_not_exists
44+
sql = "IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = '#{o.index.name}') #{sql}"
45+
end
46+
47+
sql
48+
end
49+
3650
def add_column_options!(sql, options)
3751
sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
3852
if options[:null] == false

0 commit comments

Comments
 (0)