Skip to content

Commit d0d9e8e

Browse files
authored
Merge pull request rails#47552 from lsylvester/where-include-index
Allow both include and where on PostgreSQL add_index
2 parents 091e04a + fd1a3b4 commit d0d9e8e

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def accept(o)
1616
delegate :quote_column_name, :quote_table_name, :quote_default_expression, :type_to_sql,
1717
:options_include_default?, :supports_indexes_in_create?, :use_foreign_keys?,
1818
:quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?,
19-
:supports_exclusion_constraints?, to: :@conn, private: true
19+
:supports_index_include?, :supports_exclusion_constraints?, to: :@conn, private: true
2020

2121
private
2222
def visit_AlterTable(o)
@@ -104,6 +104,7 @@ def visit_CreateIndexDefinition(o)
104104
sql << "#{quote_column_name(index.name)} ON #{quote_table_name(index.table)}"
105105
sql << "USING #{index.using}" if supports_index_using? && index.using
106106
sql << "(#{quoted_columns(index)})"
107+
sql << "INCLUDE (#{quoted_include_columns(o.index.include)})" if supports_index_include? && o.index.include
107108
sql << "WHERE #{index.where}" if supports_partial_index? && index.where
108109

109110
sql.join(" ")

activerecord/lib/active_record/connection_adapters/postgresql/schema_creation.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ module ConnectionAdapters
55
module PostgreSQL
66
class SchemaCreation < SchemaCreation # :nodoc:
77
private
8-
delegate :quoted_include_columns_for_index, :supports_index_include?,
9-
to: :@conn
8+
delegate :quoted_include_columns_for_index, to: :@conn
109

1110
def visit_AlterTable(o)
1211
sql = super
@@ -30,12 +29,6 @@ def visit_CheckConstraintDefinition(o)
3029
super.dup.tap { |sql| sql << " NOT VALID" unless o.validate? }
3130
end
3231

33-
def visit_CreateIndexDefinition(o)
34-
super.dup.tap do |sql|
35-
sql << " INCLUDE (#{quoted_include_columns(o.index.include)})" if supports_index_include? && o.index.include
36-
end
37-
end
38-
3932
def visit_ValidateConstraint(name)
4033
"VALIDATE CONSTRAINT #{quote_column_name(name)}"
4134
end

activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def indexes(table_name) # :nodoc:
108108
oid = row[4]
109109
comment = row[5]
110110
valid = row[6]
111-
using, expressions, where, include = inddef.scan(/ USING (\w+?) \((.+?)\)(?: NULLS(?: NOT)? DISTINCT)?(?: WHERE (.+?))?(?: INCLUDE \((.+)\))?\z/m).flatten
111+
using, expressions, include, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: NULLS(?: NOT)? DISTINCT)?(?: INCLUDE \((.+?)\))?(?: WHERE (.+))?\z/m).flatten
112112

113113
orders = {}
114114
opclasses = {}

activerecord/test/cases/migration/index_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ def test_add_index_with_multiple_included_columns
286286
connection.remove_index("testings", "last_name")
287287
assert_not connection.index_exists?("testings", "last_name")
288288
end
289+
290+
def test_add_index_with_included_column_and_where_clause
291+
connection.add_index("testings", "last_name", include: :foo, where: "first_name = 'john doe'")
292+
assert connection.index_exists?("testings", "last_name", include: :foo, where: "first_name = 'john doe'")
293+
294+
connection.remove_index("testings", "last_name")
295+
assert_not connection.index_exists?("testings", "last_name", include: :foo, where: "first_name = 'john doe'")
296+
end
289297
end
290298

291299
private

0 commit comments

Comments
 (0)