Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
24f264e9
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,发现更多精彩内容 >>
提交
24f264e9
编写于
5月 12, 2016
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SQLite: Foreign Key Support
https://www.sqlite.org/foreignkeys.html
上级
b0923e4c
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
81 addition
and
30 deletion
+81
-30
activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb
...ve_record/connection_adapters/abstract/schema_creation.rb
+3
-3
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
...lib/active_record/connection_adapters/abstract_adapter.rb
+6
-0
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
.../lib/active_record/connection_adapters/sqlite3_adapter.rb
+25
-0
activerecord/test/cases/migration/foreign_key_test.rb
activerecord/test/cases/migration/foreign_key_test.rb
+27
-7
activerecord/test/cases/migration/references_foreign_key_test.rb
...ecord/test/cases/migration/references_foreign_key_test.rb
+20
-2
activerecord/test/schema/sqlite_specific_schema.rb
activerecord/test/schema/sqlite_specific_schema.rb
+0
-18
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb
浏览文件 @
24f264e9
...
...
@@ -15,9 +15,9 @@ def accept(o)
end
delegate
:quote_column_name
,
:quote_table_name
,
:quote_default_expression
,
:type_to_sql
,
:options_include_default?
,
:supports_indexes_in_create?
,
:supports_foreign_keys?
,
:foreign_key_options
,
to: :@conn
:options_include_default?
,
:supports_indexes_in_create?
,
:supports_foreign_keys
_in_create
?
,
:foreign_key_options
,
to: :@conn
private
:quote_column_name
,
:quote_table_name
,
:quote_default_expression
,
:type_to_sql
,
:options_include_default?
,
:supports_indexes_in_create?
,
:supports_foreign_keys?
,
:foreign_key_options
:options_include_default?
,
:supports_indexes_in_create?
,
:supports_foreign_keys
_in_create
?
,
:foreign_key_options
private
...
...
@@ -49,7 +49,7 @@ def visit_TableDefinition(o)
statements
.
concat
(
o
.
indexes
.
map
{
|
column_name
,
options
|
index_in_create
(
o
.
name
,
column_name
,
options
)
})
end
if
supports_foreign_keys?
if
supports_foreign_keys
_in_create
?
statements
.
concat
(
o
.
foreign_keys
.
map
{
|
to_table
,
options
|
foreign_key_in_create
(
o
.
name
,
to_table
,
options
)
})
end
...
...
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
浏览文件 @
24f264e9
...
...
@@ -310,6 +310,12 @@ def supports_foreign_keys?
false
end
# Does this adapter support creating foreign key constraints
# in the same statement as creating the table?
def
supports_foreign_keys_in_create?
supports_foreign_keys?
end
# Does this adapter support views?
def
supports_views?
false
...
...
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
浏览文件 @
24f264e9
...
...
@@ -130,6 +130,10 @@ def requires_reloading?
true
end
def
supports_foreign_keys_in_create?
sqlite_version
>=
"3.6.19"
end
def
supports_views?
true
end
...
...
@@ -439,6 +443,19 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
rename_column_indexes
(
table_name
,
column
.
name
,
new_column_name
)
end
def
foreign_keys
(
table_name
)
fk_info
=
select_all
(
"PRAGMA foreign_key_list(
#{
quote
(
table_name
)
}
)"
,
"SCHEMA"
)
fk_info
.
map
do
|
row
|
options
=
{
column:
row
[
"from"
],
primary_key:
row
[
"to"
],
on_delete:
extract_foreign_key_action
(
row
[
"on_delete"
]),
on_update:
extract_foreign_key_action
(
row
[
"on_update"
])
}
ForeignKeyDefinition
.
new
(
table_name
,
row
[
"table"
],
options
)
end
end
private
def
table_structure
(
table_name
)
...
...
@@ -592,6 +609,14 @@ def create_table_definition(*args)
SQLite3
::
TableDefinition
.
new
(
*
args
)
end
def
extract_foreign_key_action
(
specifier
)
case
specifier
when
"CASCADE"
;
:cascade
when
"SET NULL"
;
:nullify
when
"RESTRICT"
;
:restrict
end
end
def
configure_connection
execute
(
"PRAGMA foreign_keys = ON"
,
"SCHEMA"
)
end
...
...
activerecord/test/cases/migration/foreign_key_test.rb
浏览文件 @
24f264e9
...
...
@@ -2,6 +2,26 @@
require
"support/ddl_helper"
require
"support/schema_dumping_helper"
if
ActiveRecord
::
Base
.
connection
.
supports_foreign_keys_in_create?
module
ActiveRecord
class
Migration
class
ForeignKeyInCreateTest
<
ActiveRecord
::
TestCase
def
test_foreign_keys
foreign_keys
=
ActiveRecord
::
Base
.
connection
.
foreign_keys
(
"fk_test_has_fk"
)
assert_equal
1
,
foreign_keys
.
size
fk
=
foreign_keys
.
first
assert_equal
"fk_test_has_fk"
,
fk
.
from_table
assert_equal
"fk_test_has_pk"
,
fk
.
to_table
assert_equal
"fk_id"
,
fk
.
column
assert_equal
"pk_id"
,
fk
.
primary_key
assert_equal
"fk_name"
,
fk
.
name
unless
current_adapter?
(
:SQLite3Adapter
)
end
end
end
end
end
if
ActiveRecord
::
Base
.
connection
.
supports_foreign_keys?
module
ActiveRecord
class
Migration
...
...
@@ -29,10 +49,8 @@ class Astronaut < ActiveRecord::Base
end
teardown
do
if
defined?
(
@connection
)
@connection
.
drop_table
"astronauts"
,
if_exists:
true
@connection
.
drop_table
"rockets"
,
if_exists:
true
end
@connection
.
drop_table
"astronauts"
,
if_exists:
true
@connection
.
drop_table
"rockets"
,
if_exists:
true
end
def
test_foreign_keys
...
...
@@ -305,9 +323,11 @@ def test_remove_foreign_key_should_be_noop
@connection
.
remove_foreign_key
:clubs
,
:categories
end
def
test_foreign_keys_should_raise_not_implemented
assert_raises
NotImplementedError
do
@connection
.
foreign_keys
(
"clubs"
)
unless
current_adapter?
(
:SQLite3Adapter
)
def
test_foreign_keys_should_raise_not_implemented
assert_raises
NotImplementedError
do
@connection
.
foreign_keys
(
"clubs"
)
end
end
end
end
...
...
activerecord/test/cases/migration/references_foreign_key_test.rb
浏览文件 @
24f264e9
require
"cases/helper"
if
ActiveRecord
::
Base
.
connection
.
supports_foreign_keys?
if
ActiveRecord
::
Base
.
connection
.
supports_foreign_keys
_in_create
?
module
ActiveRecord
class
Migration
class
ReferencesForeignKeyTest
<
ActiveRecord
::
TestCase
class
ReferencesForeignKey
InCreate
Test
<
ActiveRecord
::
TestCase
setup
do
@connection
=
ActiveRecord
::
Base
.
connection
@connection
.
create_table
(
:testing_parents
,
force:
true
)
...
...
@@ -61,6 +61,24 @@ class ReferencesForeignKeyTest < ActiveRecord::TestCase
assert_equal
([[
"testings"
,
"testing_parents"
,
"parent_id"
]],
fks
.
map
{
|
fk
|
[
fk
.
from_table
,
fk
.
to_table
,
fk
.
column
]
})
end
end
end
end
end
if
ActiveRecord
::
Base
.
connection
.
supports_foreign_keys?
module
ActiveRecord
class
Migration
class
ReferencesForeignKeyTest
<
ActiveRecord
::
TestCase
setup
do
@connection
=
ActiveRecord
::
Base
.
connection
@connection
.
create_table
(
:testing_parents
,
force:
true
)
end
teardown
do
@connection
.
drop_table
"testings"
,
if_exists:
true
@connection
.
drop_table
"testing_parents"
,
if_exists:
true
end
test
"foreign keys cannot be added to polymorphic relations when creating the table"
do
@connection
.
create_table
:testings
do
|
t
|
...
...
activerecord/test/schema/sqlite_specific_schema.rb
已删除
100644 → 0
浏览文件 @
b0923e4c
ActiveRecord
::
Schema
.
define
do
execute
"DROP TABLE fk_test_has_fk"
rescue
nil
execute
"DROP TABLE fk_test_has_pk"
rescue
nil
execute
<<
_SQL
CREATE TABLE 'fk_test_has_pk' (
'pk_id' INTEGER NOT NULL PRIMARY KEY
);
_SQL
execute
<<
_SQL
CREATE TABLE 'fk_test_has_fk' (
'id' INTEGER NOT NULL PRIMARY KEY,
'fk_id' INTEGER NOT NULL,
FOREIGN KEY ('fk_id') REFERENCES 'fk_test_has_pk'('pk_id')
);
_SQL
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录