Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e39b1f3c
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,体验更适合开发者的 AI 搜索 >>
提交
e39b1f3c
编写于
11月 22, 2014
作者:
S
Sean Griffin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rename the primary key index when renaming a table in pg
Backport of
dcc143cd
Fixes #12856 Closes #14088
上级
63b80b5b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
43 addition
and
0 deletion
+43
-0
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+6
-0
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
...ecord/connection_adapters/postgresql/schema_statements.rb
+3
-0
activerecord/test/cases/adapters/postgresql/rename_table_test.rb
...ecord/test/cases/adapters/postgresql/rename_table_test.rb
+34
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
e39b1f3c
*
Renaming a table in pg also renames the primary key index.
Fixes #12856
*Sean Griffin*
*
Make it possible to access fixtures excluded by a
`default_scope`
.
*Yves Senn*
...
...
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
浏览文件 @
e39b1f3c
...
...
@@ -382,7 +382,10 @@ def rename_table(table_name, new_name)
pk
,
seq
=
pk_and_sequence_for
(
new_name
)
if
seq
==
"
#{
table_name
}
_
#{
pk
}
_seq"
new_seq
=
"
#{
new_name
}
_
#{
pk
}
_seq"
idx
=
"
#{
table_name
}
_pkey"
new_idx
=
"
#{
new_name
}
_pkey"
execute
"ALTER TABLE
#{
quote_table_name
(
seq
)
}
RENAME TO
#{
quote_table_name
(
new_seq
)
}
"
execute
"ALTER INDEX
#{
quote_table_name
(
idx
)
}
RENAME TO
#{
quote_table_name
(
new_idx
)
}
"
end
rename_table_indexes
(
table_name
,
new_name
)
...
...
activerecord/test/cases/adapters/postgresql/rename_table_test.rb
0 → 100644
浏览文件 @
e39b1f3c
require
"cases/helper"
class
PostgresqlRenameTableTest
<
ActiveRecord
::
TestCase
def
setup
@connection
=
ActiveRecord
::
Base
.
connection
@connection
.
create_table
:before_rename
,
force:
true
end
def
teardown
@connection
.
execute
'DROP TABLE IF EXISTS "before_rename"'
@connection
.
execute
'DROP TABLE IF EXISTS "after_rename"'
end
test
"renaming a table also renames the primary key index"
do
# sanity check
assert_equal
1
,
num_indices_named
(
"before_rename_pkey"
)
assert_equal
0
,
num_indices_named
(
"after_rename_pkey"
)
@connection
.
rename_table
:before_rename
,
:after_rename
assert_equal
0
,
num_indices_named
(
"before_rename_pkey"
)
assert_equal
1
,
num_indices_named
(
"after_rename_pkey"
)
end
private
def
num_indices_named
(
name
)
@connection
.
execute
(
<<-
SQL
).
values
.
length
SELECT 1 FROM "pg_index"
JOIN "pg_class" ON "pg_index"."indexrelid" = "pg_class"."oid"
WHERE "pg_class"."relname" = '
#{
name
}
'
SQL
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录