mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #13350 from ccutrer/sqlite-partial-indexes
sqlite >= 3.8.0 supports partial indexes
This commit is contained in:
commit
93b38d54df
3 changed files with 25 additions and 1 deletions
|
@ -1,3 +1,9 @@
|
|||
* Enable partial indexes for sqlite >= 3.8.0
|
||||
|
||||
See http://www.sqlite.org/partialindex.html
|
||||
|
||||
*Cody Cutrer*
|
||||
|
||||
* Don't try to get the subclass if the inheritance column doesn't exist
|
||||
|
||||
The `subclass_from_attrs` method is called even if the column specified by
|
||||
|
|
|
@ -155,6 +155,10 @@ module ActiveRecord
|
|||
true
|
||||
end
|
||||
|
||||
def supports_partial_index?
|
||||
sqlite_version >= '3.8.0'
|
||||
end
|
||||
|
||||
# Returns true, since this connection adapter supports prepared statement
|
||||
# caching.
|
||||
def supports_statement_cache?
|
||||
|
@ -397,13 +401,25 @@ module ActiveRecord
|
|||
# Returns an array of indexes for the given table.
|
||||
def indexes(table_name, name = nil) #:nodoc:
|
||||
exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", 'SCHEMA').map do |row|
|
||||
sql = <<-SQL
|
||||
SELECT sql
|
||||
FROM sqlite_master
|
||||
WHERE name=#{quote(row['name'])} AND type='index'
|
||||
UNION ALL
|
||||
SELECT sql
|
||||
FROM sqlite_temp_master
|
||||
WHERE name=#{quote(row['name'])} AND type='index'
|
||||
SQL
|
||||
index_sql = exec_query(sql).first['sql']
|
||||
match = /\sWHERE\s+(.+)$/i.match(index_sql)
|
||||
where = match[1] if match
|
||||
IndexDefinition.new(
|
||||
table_name,
|
||||
row['name'],
|
||||
row['unique'] != 0,
|
||||
exec_query("PRAGMA index_info('#{row['name']}')", "SCHEMA").map { |col|
|
||||
col['name']
|
||||
})
|
||||
}, nil, nil, where)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -190,6 +190,8 @@ class SchemaDumperTest < ActiveRecord::TestCase
|
|||
assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index", where: "(rating > 10)", using: :btree', index_definition
|
||||
elsif current_adapter?(:MysqlAdapter) || current_adapter?(:Mysql2Adapter)
|
||||
assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index", using: :btree', index_definition
|
||||
elsif current_adapter?(:SQLite3Adapter) && ActiveRecord::Base.connection.supports_partial_index?
|
||||
assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index", where: "rating > 10"', index_definition
|
||||
else
|
||||
assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index"', index_definition
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue