Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3092324e
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3092324e
编写于
2月 10, 2012
作者:
A
Aaron Patterson
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4956 from mhfs/pg_partial_indices
Add support for partial indices to PostgreSQL adapter
上级
fc3fad8d
aaffc2ac
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
77 addition
and
5 deletion
+77
-5
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+13
-0
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
...record/connection_adapters/abstract/schema_definitions.rb
+1
-1
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
..._record/connection_adapters/abstract/schema_statements.rb
+13
-3
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
...lib/active_record/connection_adapters/abstract_adapter.rb
+5
-0
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
...b/active_record/connection_adapters/postgresql_adapter.rb
+6
-1
activerecord/lib/active_record/schema_dumper.rb
activerecord/lib/active_record/schema_dumper.rb
+2
-0
activerecord/test/cases/adapters/postgresql/active_schema_test.rb
...cord/test/cases/adapters/postgresql/active_schema_test.rb
+12
-0
activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
...test/cases/adapters/postgresql/postgresql_adapter_test.rb
+6
-0
activerecord/test/cases/migration/index_test.rb
activerecord/test/cases/migration/index_test.rb
+9
-0
activerecord/test/cases/schema_dumper_test.rb
activerecord/test/cases/schema_dumper_test.rb
+9
-0
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+1
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
3092324e
## Rails 4.0.0 (unreleased) ##
*
Added support for partial indices to PostgreSQL adapter
The `add_index` method now supports a `where` option that receives a
string with the partial index criteria.
add_index(:accounts, :code, :where => "active")
Generates
CREATE INDEX index_accounts_on_code ON accounts(code) WHERE active
*Marcelo Silveira*
*
Implemented ActiveRecord::Relation#none method
The `none` method returns a chainable relation with zero records
...
...
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
浏览文件 @
3092324e
...
...
@@ -6,7 +6,7 @@
module
ActiveRecord
module
ConnectionAdapters
#:nodoc:
class
IndexDefinition
<
Struct
.
new
(
:table
,
:name
,
:unique
,
:columns
,
:lengths
,
:orders
)
#:nodoc:
class
IndexDefinition
<
Struct
.
new
(
:table
,
:name
,
:unique
,
:columns
,
:lengths
,
:orders
,
:where
)
#:nodoc:
end
# Abstract representation of a column definition. Instances of this type
...
...
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
浏览文件 @
3092324e
...
...
@@ -381,9 +381,16 @@ def rename_column(table_name, column_name, new_column_name)
#
# Note: mysql doesn't yet support index order (it accepts the syntax but ignores it)
#
# ====== Creating a partial index
# add_index(:accounts, [:branch_id, :party_id], :unique => true, :where => "active")
# generates
# CREATE UNIQUE INDEX index_accounts_on_branch_id_and_party_id ON accounts(branch_id, party_id) WHERE active
#
# Note: only supported by PostgreSQL
#
def
add_index
(
table_name
,
column_name
,
options
=
{})
index_name
,
index_type
,
index_columns
=
add_index_options
(
table_name
,
column_name
,
options
)
execute
"CREATE
#{
index_type
}
INDEX
#{
quote_column_name
(
index_name
)
}
ON
#{
quote_table_name
(
table_name
)
}
(
#{
index_columns
}
)"
index_name
,
index_type
,
index_columns
,
index_options
=
add_index_options
(
table_name
,
column_name
,
options
)
execute
"CREATE
#{
index_type
}
INDEX
#{
quote_column_name
(
index_name
)
}
ON
#{
quote_table_name
(
table_name
)
}
(
#{
index_columns
}
)
#{
index_options
}
"
end
# Remove the given index from the table.
...
...
@@ -581,6 +588,9 @@ def add_index_options(table_name, column_name, options = {})
if
Hash
===
options
# legacy support, since this param was a string
index_type
=
options
[
:unique
]
?
"UNIQUE"
:
""
index_name
=
options
[
:name
].
to_s
if
options
.
key?
(
:name
)
if
supports_partial_index?
index_options
=
options
[
:where
]
?
" WHERE
#{
options
[
:where
]
}
"
:
""
end
else
index_type
=
options
end
...
...
@@ -593,7 +603,7 @@ def add_index_options(table_name, column_name, options = {})
end
index_columns
=
quoted_columns_for_index
(
column_names
,
options
).
join
(
", "
)
[
index_name
,
index_type
,
index_columns
]
[
index_name
,
index_type
,
index_columns
,
index_options
]
end
def
index_name_for_remove
(
table_name
,
options
=
{})
...
...
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
浏览文件 @
3092324e
...
...
@@ -142,6 +142,11 @@ def supports_index_sort_order?
false
end
# Does this adapter support partial indices?
def
supports_partial_index?
false
end
# Does this adapter support explain? As of this writing sqlite3,
# mysql2, and postgresql are the only ones that do.
def
supports_explain?
...
...
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
浏览文件 @
3092324e
...
...
@@ -302,6 +302,10 @@ def supports_index_sort_order?
true
end
def
supports_partial_index?
true
end
class
StatementPool
<
ConnectionAdapters
::
StatementPool
def
initialize
(
connection
,
max
)
super
...
...
@@ -885,8 +889,9 @@ def indexes(table_name, name = nil)
# add info on sort order for columns (only desc order is explicitly specified, asc is the default)
desc_order_columns
=
inddef
.
scan
(
/(\w+) DESC/
).
flatten
orders
=
desc_order_columns
.
any?
?
Hash
[
desc_order_columns
.
map
{
|
order_column
|
[
order_column
,
:desc
]}]
:
{}
where
=
inddef
.
scan
(
/WHERE (.+)$/
).
flatten
[
0
]
column_names
.
empty?
?
nil
:
IndexDefinition
.
new
(
table_name
,
index_name
,
unique
,
column_names
,
[],
orders
)
column_names
.
empty?
?
nil
:
IndexDefinition
.
new
(
table_name
,
index_name
,
unique
,
column_names
,
[],
orders
,
where
)
end
.
compact
end
...
...
activerecord/lib/active_record/schema_dumper.rb
浏览文件 @
3092324e
...
...
@@ -197,6 +197,8 @@ def indexes(table, stream)
index_orders
=
(
index
.
orders
||
{})
statement_parts
<<
(
':order => '
+
index
.
orders
.
inspect
)
unless
index_orders
.
empty?
statement_parts
<<
(
':where => '
+
index
.
where
.
inspect
)
if
index
.
where
' '
+
statement_parts
.
join
(
', '
)
end
...
...
activerecord/test/cases/adapters/postgresql/active_schema_test.rb
浏览文件 @
3092324e
...
...
@@ -21,6 +21,18 @@ def test_create_database_with_encoding
assert_equal
%(CREATE DATABASE "aimonetti" ENCODING = 'latin1')
,
create_database
(
:aimonetti
,
:encoding
=>
:latin1
)
end
def
test_add_index
# add_index calls index_name_exists? which can't work since execute is stubbed
ActiveRecord
::
ConnectionAdapters
::
PostgreSQLAdapter
.
send
(
:define_method
,
:index_name_exists?
)
do
|*|
false
end
expected
=
%(CREATE UNIQUE INDEX "index_people_on_last_name" ON "people" ("last_name") WHERE state = 'active')
assert_equal
expected
,
add_index
(
:people
,
:last_name
,
:unique
=>
true
,
:where
=>
"state = 'active'"
)
ActiveRecord
::
ConnectionAdapters
::
PostgreSQLAdapter
.
send
(
:remove_method
,
:index_name_exists?
)
end
private
def
method_missing
(
method_symbol
,
*
arguments
)
ActiveRecord
::
Base
.
connection
.
send
(
method_symbol
,
*
arguments
)
...
...
activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
浏览文件 @
3092324e
...
...
@@ -179,6 +179,12 @@ def test_substitute_at
assert_equal
Arel
.
sql
(
'$2'
),
bind
end
def
test_partial_index
@connection
.
add_index
'ex'
,
%w{ id number }
,
:name
=>
'partial'
,
:where
=>
"number > 100"
index
=
@connection
.
indexes
(
'ex'
).
find
{
|
idx
|
idx
.
name
==
'partial'
}
assert_equal
"(number > 100)"
,
index
.
where
end
private
def
insert
(
ctx
,
data
)
binds
=
data
.
map
{
|
name
,
value
|
...
...
activerecord/test/cases/migration/index_test.rb
浏览文件 @
3092324e
...
...
@@ -171,6 +171,15 @@ def test_add_index
end
end
def
test_add_partial_index
skip
'only on pg'
unless
current_adapter?
(
:PostgreSQLAdapter
)
connection
.
add_index
(
"testings"
,
"last_name"
,
:where
=>
"first_name = 'john doe'"
)
assert
connection
.
index_exists?
(
"testings"
,
"last_name"
)
connection
.
remove_index
(
"testings"
,
"last_name"
)
assert
!
connection
.
index_exists?
(
"testings"
,
"last_name"
)
end
end
end
end
activerecord/test/cases/schema_dumper_test.rb
浏览文件 @
3092324e
...
...
@@ -185,6 +185,15 @@ def test_schema_dumps_index_columns_in_right_order
assert_equal
'add_index "companies", ["firm_id", "type", "rating", "ruby_type"], :name => "company_index"'
,
index_definition
end
def
test_schema_dumps_partial_indices
index_definition
=
standard_dump
.
split
(
/\n/
).
grep
(
/add_index.*company_partial_index/
).
first
.
strip
if
current_adapter?
(
:PostgreSQLAdapter
)
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
end
def
test_schema_dump_should_honor_nonstandard_primary_keys
output
=
standard_dump
match
=
output
.
match
(
%r{create_table "movies"(.*)do}
)
...
...
activerecord/test/schema/schema.rb
浏览文件 @
3092324e
...
...
@@ -175,6 +175,7 @@ def create_table(*args, &block)
end
add_index
:companies
,
[
:firm_id
,
:type
,
:rating
,
:ruby_type
],
:name
=>
"company_index"
add_index
:companies
,
[
:firm_id
,
:type
],
:name
=>
"company_partial_index"
,
:where
=>
"rating > 10"
create_table
:computers
,
:force
=>
true
do
|
t
|
t
.
integer
:developer
,
:null
=>
false
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录