Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
8ceb883b
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,发现更多精彩内容 >>
提交
8ceb883b
编写于
4月 03, 2015
作者:
G
Grey Baker
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support removing custom-names indexes when only specifying column names
上级
9771f5e1
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
107 addition
and
18 deletion
+107
-18
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
..._record/connection_adapters/abstract/schema_statements.rb
+27
-13
activerecord/lib/active_record/migration/compatibility.rb
activerecord/lib/active_record/migration/compatibility.rb
+25
-0
activerecord/test/cases/adapters/postgresql/active_schema_test.rb
...cord/test/cases/adapters/postgresql/active_schema_test.rb
+5
-3
activerecord/test/cases/adapters/postgresql/schema_test.rb
activerecord/test/cases/adapters/postgresql/schema_test.rb
+0
-2
activerecord/test/cases/migration/compatibility_test.rb
activerecord/test/cases/migration/compatibility_test.rb
+42
-0
activerecord/test/cases/migration/index_test.rb
activerecord/test/cases/migration/index_test.rb
+8
-0
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
浏览文件 @
8ceb883b
...
...
@@ -699,15 +699,15 @@ def add_index(table_name, column_name, options = {})
# Removes the given index from the table.
#
# Removes the
+index_accounts_on_column+ in the +accounts+ table
.
# Removes the
index on +branch_id+ in the +accounts+ table if exactly one such index exists
.
#
# remove_index :accounts, :branch_id
#
# Removes the index
named +index_accounts_on_branch_id+ in the +accounts+ table
.
# Removes the index
on +branch_id+ in the +accounts+ table if exactly one such index exists
.
#
# remove_index :accounts, column: :branch_id
#
# Removes the index
named +index_accounts_on_branch_id_and_party_id+ in the +accounts+ table
.
# Removes the index
on +branch_id+ and +party_id+ in the +accounts+ table if exactly one such index exists
.
#
# remove_index :accounts, column: [:branch_id, :party_id]
#
...
...
@@ -1126,21 +1126,35 @@ def quoted_columns_for_index(column_names, options = {})
end
def
index_name_for_remove
(
table_name
,
options
=
{})
index_name
=
index_name
(
table_name
,
options
)
# if the adapter doesn't support the indexes call the best we can do
# is return the default index name for the options provided
return
index_name
(
table_name
,
options
)
unless
respond_to?
(
:indexes
)
unless
index_name_exists?
(
table_name
,
index_name
,
true
)
if
options
.
is_a?
(
Hash
)
&&
options
.
has_key?
(
:name
)
options_without_column
=
options
.
dup
options_without_column
.
delete
:column
index_name_without_column
=
index_name
(
table_name
,
options_without_column
)
checks
=
[]
return
index_name_without_column
if
index_name_exists?
(
table_name
,
index_name_without_column
,
false
)
end
if
options
.
is_a?
(
Hash
)
checks
<<
lambda
{
|
i
|
i
.
name
==
options
[
:name
].
to_s
}
if
options
.
has_key?
(
:name
)
column_names
=
Array
(
options
[
:column
]).
map
(
&
:to_s
)
else
column_names
=
Array
(
options
).
map
(
&
:to_s
)
end
raise
ArgumentError
,
"Index name '
#{
index_name
}
' on table '
#{
table_name
}
' does not exist"
if
column_names
.
any?
checks
<<
lambda
{
|
i
|
i
.
columns
.
join
(
'_and_'
)
==
column_names
.
join
(
'_and_'
)
}
end
index_name
raise
ArgumentError
"No name or columns specified"
if
checks
.
none?
matching_indexes
=
indexes
(
table_name
).
select
{
|
i
|
checks
.
all?
{
|
check
|
check
[
i
]
}
}
if
matching_indexes
.
count
>
1
raise
ArgumentError
,
"Multiple indexes found on
#{
table_name
}
columns
#{
column_names
}
. "
\
"Specify an index name from
#{
matching_indexes
.
map
(
&
:name
).
join
(
', '
)
}
"
elsif
matching_indexes
.
none?
raise
ArgumentError
,
"No indexes found on
#{
table_name
}
with the options provided."
else
matching_indexes
.
first
.
name
end
end
def
rename_table_indexes
(
table_name
,
new_name
)
...
...
activerecord/lib/active_record/migration/compatibility.rb
浏览文件 @
8ceb883b
...
...
@@ -39,6 +39,31 @@ def index_exists?(table_name, column_name, options = {})
end
super
end
def
remove_index
(
table_name
,
options
=
{})
index_name
=
index_name_for_remove
(
table_name
,
options
)
execute
"DROP INDEX
#{
quote_column_name
(
index_name
)
}
ON
#{
quote_table_name
(
table_name
)
}
"
end
private
def
index_name_for_remove
(
table_name
,
options
=
{})
index_name
=
index_name
(
table_name
,
options
)
unless
index_name_exists?
(
table_name
,
index_name
,
true
)
if
options
.
is_a?
(
Hash
)
&&
options
.
has_key?
(
:name
)
options_without_column
=
options
.
dup
options_without_column
.
delete
:column
index_name_without_column
=
index_name
(
table_name
,
options_without_column
)
return
index_name_without_column
if
index_name_exists?
(
table_name
,
index_name_without_column
,
false
)
end
raise
ArgumentError
,
"Index name '
#{
index_name
}
' on table '
#{
table_name
}
' does not exist"
end
index_name
end
end
class
V4_2
<
V5_0
...
...
activerecord/test/cases/adapters/postgresql/active_schema_test.rb
浏览文件 @
8ceb883b
...
...
@@ -54,8 +54,10 @@ def test_add_index
end
def
test_remove_index
# remove_index calls index_name_exists? which can't work since execute is stubbed
ActiveRecord
::
ConnectionAdapters
::
PostgreSQLAdapter
.
send
(
:define_method
,
:index_name_exists?
)
{
|*|
true
}
# remove_index calls index_name_for_remove which can't work since execute is stubbed
ActiveRecord
::
ConnectionAdapters
::
PostgreSQLAdapter
.
send
(
:define_method
,
:index_name_for_remove
)
do
|*|
'index_people_on_last_name'
end
expected
=
%(DROP INDEX CONCURRENTLY "index_people_on_last_name")
assert_equal
expected
,
remove_index
(
:people
,
name:
"index_people_on_last_name"
,
algorithm: :concurrently
)
...
...
@@ -64,7 +66,7 @@ def test_remove_index
add_index
(
:people
,
:last_name
,
algorithm: :copy
)
end
ActiveRecord
::
ConnectionAdapters
::
PostgreSQLAdapter
.
send
:remove_method
,
:index_name_
exists?
ActiveRecord
::
ConnectionAdapters
::
PostgreSQLAdapter
.
send
:remove_method
,
:index_name_
for_remove
end
private
...
...
activerecord/test/cases/adapters/postgresql/schema_test.rb
浏览文件 @
8ceb883b
...
...
@@ -323,8 +323,6 @@ def test_dump_indexes_for_schema_multiple_schemas_in_search_path
def
test_with_uppercase_index_name
@connection
.
execute
"CREATE INDEX
\"
things_Index
\"
ON
#{
SCHEMA_NAME
}
.things (name)"
assert_nothing_raised
{
@connection
.
remove_index
"things"
,
name:
"
#{
SCHEMA_NAME
}
.things_Index"
}
@connection
.
execute
"CREATE INDEX
\"
things_Index
\"
ON
#{
SCHEMA_NAME
}
.things (name)"
with_schema_search_path
SCHEMA_NAME
do
assert_nothing_raised
{
@connection
.
remove_index
"things"
,
name:
"things_Index"
}
...
...
activerecord/test/cases/migration/compatibility_test.rb
0 → 100644
浏览文件 @
8ceb883b
require
'cases/helper'
module
ActiveRecord
class
Migration
class
CompatibilityTest
<
ActiveRecord
::
TestCase
attr_reader
:connection
self
.
use_transactional_tests
=
false
def
setup
super
@connection
=
ActiveRecord
::
Base
.
connection
@verbose_was
=
ActiveRecord
::
Migration
.
verbose
ActiveRecord
::
Migration
.
verbose
=
false
connection
.
create_table
:testings
do
|
t
|
t
.
column
:foo
,
:string
,
:limit
=>
100
t
.
column
:bar
,
:string
,
:limit
=>
100
end
end
teardown
do
connection
.
drop_table
:testings
rescue
nil
ActiveRecord
::
Migration
.
verbose
=
@verbose_was
end
def
test_migration_doesnt_remove_named_index
connection
.
add_index
:testings
,
:foo
,
:name
=>
"custom_index_name"
migration
=
Class
.
new
(
ActiveRecord
::
Migration
[
4.2
])
{
def
version
;
101
end
def
migrate
(
x
)
remove_index
:testings
,
:foo
end
}.
new
assert
connection
.
index_exists?
(
:testings
,
:foo
,
name:
"custom_index_name"
)
assert_raise
(
StandardError
)
{
ActiveRecord
::
Migrator
.
new
(
:up
,
[
migration
]).
migrate
}
assert
connection
.
index_exists?
(
:testings
,
:foo
,
name:
"custom_index_name"
)
end
end
end
end
activerecord/test/cases/migration/index_test.rb
浏览文件 @
8ceb883b
...
...
@@ -135,6 +135,14 @@ def test_named_index_exists
assert
!
connection
.
index_exists?
(
:testings
,
:foo
,
:name
=>
"other_index_name"
)
end
def
test_remove_named_index
connection
.
add_index
:testings
,
:foo
,
:name
=>
"custom_index_name"
assert
connection
.
index_exists?
(
:testings
,
:foo
)
connection
.
remove_index
:testings
,
:foo
assert
!
connection
.
index_exists?
(
:testings
,
:foo
)
end
def
test_add_index_attribute_length_limit
connection
.
add_index
:testings
,
[
:foo
,
:bar
],
:length
=>
{
:foo
=>
10
,
:bar
=>
nil
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录