Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
130513d0
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,发现更多精彩内容 >>
未验证
提交
130513d0
编写于
4月 16, 2020
作者:
E
Eileen M. Uchitelle
提交者:
GitHub
4月 16, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #38967 from eileencodes/add-if-exists-to-remove-index
Add `if_exists` option to `remove_index`
上级
ab18f608
36ea1084
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
40 addition
and
3 deletion
+40
-3
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+6
-0
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
..._record/connection_adapters/abstract/schema_statements.rb
+8
-0
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
...ecord/connection_adapters/postgresql/schema_statements.rb
+5
-1
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
.../lib/active_record/connection_adapters/sqlite3_adapter.rb
+4
-1
activerecord/test/cases/migration/index_test.rb
activerecord/test/cases/migration/index_test.rb
+17
-1
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
130513d0
*
Add support for
`if_exists`
option for removing an index.
The `remove_index` method can take an `if_exists` option. If this is set to true an error won't be raised if the index doesn't exist.
*Eileen M. Uchitelle*
*
Remove ibm_db, informix, mssql, oracle, and oracle12 Arel visitors which are not used in the code base.
*Ryuta Kamizono*
...
...
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
浏览文件 @
130513d0
...
...
@@ -845,6 +845,11 @@ def add_index(table_name, column_name, options = {})
#
# remove_index :accounts, :branch_id, name: :by_branch_party
#
# Checks if the index exists before trying to remove it. Will silently ignore indexes that
# don't exist.
#
# remove_index :accounts, if_exists: true
#
# Removes the index named +by_branch_party+ in the +accounts+ table +concurrently+.
#
# remove_index :accounts, name: :by_branch_party, algorithm: :concurrently
...
...
@@ -855,7 +860,10 @@ def add_index(table_name, column_name, options = {})
#
# For more information see the {"Transactional Migrations" section}[rdoc-ref:Migration].
def
remove_index
(
table_name
,
column_name
=
nil
,
options
=
{})
return
if
options
[
:if_exists
]
&&
!
index_exists?
(
table_name
,
column_name
,
options
)
index_name
=
index_name_for_remove
(
table_name
,
column_name
,
options
)
execute
"DROP INDEX
#{
quote_column_name
(
index_name
)
}
ON
#{
quote_table_name
(
table_name
)
}
"
end
...
...
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
浏览文件 @
130513d0
...
...
@@ -448,7 +448,7 @@ def add_index(table_name, column_name, options = {}) #:nodoc:
end
end
def
remove_index
(
table_name
,
column_name
=
nil
,
options
=
{})
#:nodoc:
def
remove_index
(
table_name
,
column_name
=
nil
,
options
=
{})
#
:nodoc:
table
=
Utils
.
extract_schema_qualified_name
(
table_name
.
to_s
)
if
column_name
.
is_a?
(
Hash
)
...
...
@@ -467,13 +467,17 @@ def remove_index(table_name, column_name = nil, options = {}) #:nodoc:
end
end
return
if
options
[
:if_exists
]
&&
!
index_exists?
(
table_name
,
column_name
,
options
)
index_to_remove
=
PostgreSQL
::
Name
.
new
(
table
.
schema
,
index_name_for_remove
(
table
.
to_s
,
column_name
,
options
))
algorithm
=
if
options
.
key?
(
:algorithm
)
index_algorithms
.
fetch
(
options
[
:algorithm
])
do
raise
ArgumentError
.
new
(
"Algorithm must be one of the following:
#{
index_algorithms
.
keys
.
map
(
&
:inspect
).
join
(
', '
)
}
"
)
end
end
execute
"DROP INDEX
#{
algorithm
}
#{
quote_table_name
(
index_to_remove
)
}
"
end
...
...
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
浏览文件 @
130513d0
...
...
@@ -226,8 +226,11 @@ def primary_keys(table_name) # :nodoc:
pks
.
sort_by
{
|
f
|
f
[
"pk"
]
}.
map
{
|
f
|
f
[
"name"
]
}
end
def
remove_index
(
table_name
,
column_name
,
options
=
{})
#:nodoc:
def
remove_index
(
table_name
,
column_name
,
options
=
{})
# :nodoc:
return
if
options
[
:if_exists
]
&&
!
index_exists?
(
table_name
,
column_name
,
options
)
index_name
=
index_name_for_remove
(
table_name
,
column_name
,
options
)
exec_query
"DROP INDEX
#{
quote_column_name
(
index_name
)
}
"
end
...
...
activerecord/test/cases/migration/index_test.rb
浏览文件 @
130513d0
...
...
@@ -72,12 +72,28 @@ def test_add_index_does_not_accept_too_long_index_names
connection
.
add_index
(
table_name
,
"foo"
,
name:
good_index_name
)
end
def
test_add_index_which_already_exists_does_not_raise_error
def
test_add_index_which_already_exists_does_not_raise_error
_with_option
connection
.
add_index
(
table_name
,
"foo"
,
if_not_exists:
true
)
assert_nothing_raised
do
connection
.
add_index
(
table_name
,
"foo"
,
if_not_exists:
true
)
end
assert
connection
.
index_name_exists?
(
table_name
,
"index_testings_on_foo"
)
end
def
test_remove_index_which_does_not_exist_doesnt_raise_with_option
connection
.
add_index
(
table_name
,
"foo"
)
connection
.
remove_index
(
table_name
,
"foo"
)
assert_raises
ArgumentError
do
connection
.
remove_index
(
table_name
,
"foo"
)
end
assert_nothing_raised
do
connection
.
remove_index
(
table_name
,
"foo"
,
if_exists:
true
)
end
end
def
test_internal_index_with_name_matching_database_limit
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录