Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
7805fa2b
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,发现更多精彩内容 >>
提交
7805fa2b
编写于
9月 22, 2015
作者:
Y
Yves Senn
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #21715 from rails/introduce_data_sources
introduce `conn.data_source_exists?` and `conn.data_sources`.
上级
3bbc3256
152b85f0
变更
16
隐藏空白更改
内联
并排
Showing
16 changed file
with
108 addition
and
34 deletion
+108
-34
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+10
-0
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
..._record/connection_adapters/abstract/schema_statements.rb
+14
-0
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
...tive_record/connection_adapters/abstract_mysql_adapter.rb
+2
-0
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
...ecord/connection_adapters/postgresql/schema_statements.rb
+11
-0
activerecord/lib/active_record/connection_adapters/schema_cache.rb
...ord/lib/active_record/connection_adapters/schema_cache.rb
+29
-22
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
.../lib/active_record/connection_adapters/sqlite3_adapter.rb
+2
-0
activerecord/lib/active_record/model_schema.rb
activerecord/lib/active_record/model_schema.rb
+2
-2
activerecord/lib/active_record/railties/databases.rake
activerecord/lib/active_record/railties/databases.rake
+1
-1
activerecord/lib/active_record/schema_dumper.rb
activerecord/lib/active_record/schema_dumper.rb
+1
-1
activerecord/lib/active_record/type_caster/connection.rb
activerecord/lib/active_record/type_caster/connection.rb
+1
-1
activerecord/test/active_record/connection_adapters/fake_adapter.rb
...rd/test/active_record/connection_adapters/fake_adapter.rb
+3
-3
activerecord/test/cases/adapter_test.rb
activerecord/test/cases/adapter_test.rb
+15
-0
activerecord/test/cases/adapters/postgresql/schema_test.rb
activerecord/test/cases/adapters/postgresql/schema_test.rb
+2
-0
activerecord/test/cases/connection_adapters/schema_cache_test.rb
...ecord/test/cases/connection_adapters/schema_cache_test.rb
+8
-3
activerecord/test/cases/view_test.rb
activerecord/test/cases/view_test.rb
+6
-0
activerecord/test/models/contact.rb
activerecord/test/models/contact.rb
+1
-1
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
7805fa2b
*
Introduce
`connection.data_sources`
and
`connection.data_source_exists?`
.
These methods determine what relations can be used to back Active Record
models (usually tables and views).
Also deprecate `SchemaCache#tables`, `SchemaCache#table_exists?` and
`SchemaCache#clear_table_cache!` in favor of their new data source
counterparts.
*Yves Senn*, *Matthew Draper*
*
`ActiveRecord::Tasks::MySQLDatabaseTasks`
fails if shellout to
mysql commands (like
`mysqldump`
) is not successful.
...
...
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
浏览文件 @
7805fa2b
...
...
@@ -23,6 +23,20 @@ def table_alias_for(table_name)
table_name
[
0
...
table_alias_length
].
tr
(
'.'
,
'_'
)
end
# Returns the relation names useable to back Active Record models.
# For most adapters this means all #tables and #views.
def
data_sources
tables
|
views
end
# Checks to see if the data source +name+ exists on the database.
#
# data_source_exists?(:ebooks)
#
def
data_source_exists?
(
name
)
data_sources
.
include?
(
name
.
to_s
)
end
# Returns an array of table names defined in the database.
def
tables
(
name
=
nil
)
raise
NotImplementedError
,
"#tables is not implemented"
...
...
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
浏览文件 @
7805fa2b
...
...
@@ -628,6 +628,7 @@ def collation
def
tables
(
name
=
nil
)
# :nodoc:
select_values
(
"SHOW FULL TABLES"
,
'SCHEMA'
)
end
alias
data_sources
tables
def
truncate
(
table_name
,
name
=
nil
)
execute
"TRUNCATE TABLE
#{
quote_table_name
(
table_name
)
}
"
,
name
...
...
@@ -644,6 +645,7 @@ def table_exists?(table_name)
select_values
(
sql
,
'SCHEMA'
).
any?
end
alias
data_source_exists?
table_exists?
def
views
# :nodoc:
select_values
(
"SHOW FULL TABLES WHERE table_type = 'VIEW'"
,
'SCHEMA'
)
...
...
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
浏览文件 @
7805fa2b
...
...
@@ -73,6 +73,16 @@ def tables(name = nil)
select_values
(
"SELECT tablename FROM pg_tables WHERE schemaname = ANY(current_schemas(false))"
,
'SCHEMA'
)
end
def
data_sources
# :nodoc
select_values
(
<<-
SQL
,
'SCHEMA'
)
SELECT c.relname
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r', 'v','m') -- (r)elation/table, (v)iew, (m)aterialized view
AND n.nspname = ANY (current_schemas(false))
SQL
end
# Returns true if table exists.
# If the schema is not specified as part of +name+ then it will only find tables within
# the current schema search path (regardless of permissions to access tables in other schemas)
...
...
@@ -89,6 +99,7 @@ def table_exists?(name)
AND n.nspname =
#{
name
.
schema
?
"'
#{
name
.
schema
}
'"
:
'ANY (current_schemas(false))'
}
SQL
end
alias
data_source_exists?
table_exists?
def
views
# :nodoc:
select_values
(
<<-
SQL
,
'SCHEMA'
)
...
...
activerecord/lib/active_record/connection_adapters/schema_cache.rb
浏览文件 @
7805fa2b
...
...
@@ -10,7 +10,7 @@ def initialize(conn)
@columns
=
{}
@columns_hash
=
{}
@primary_keys
=
{}
@
tables
=
{}
@
data_sources
=
{}
end
def
initialize_dup
(
other
)
...
...
@@ -18,33 +18,38 @@ def initialize_dup(other)
@columns
=
@columns
.
dup
@columns_hash
=
@columns_hash
.
dup
@primary_keys
=
@primary_keys
.
dup
@
tables
=
@tabl
es
.
dup
@
data_sources
=
@data_sourc
es
.
dup
end
def
primary_keys
(
table_name
)
@primary_keys
[
table_name
]
||=
tabl
e_exists?
(
table_name
)
?
connection
.
primary_key
(
table_name
)
:
nil
@primary_keys
[
table_name
]
||=
data_sourc
e_exists?
(
table_name
)
?
connection
.
primary_key
(
table_name
)
:
nil
end
# A cached lookup for table existence.
def
tabl
e_exists?
(
name
)
prepare_
tables
if
@tabl
es
.
empty?
return
@
tables
[
name
]
if
@tabl
es
.
key?
name
def
data_sourc
e_exists?
(
name
)
prepare_
data_sources
if
@data_sourc
es
.
empty?
return
@
data_sources
[
name
]
if
@data_sourc
es
.
key?
name
@
tables
[
name
]
=
connection
.
tabl
e_exists?
(
name
)
@
data_sources
[
name
]
=
connection
.
data_sourc
e_exists?
(
name
)
end
alias
table_exists?
data_source_exists?
deprecate
:table_exists?
=>
"use #data_source_exists? instead"
# Add internal cache for table with +table_name+.
def
add
(
table_name
)
if
tabl
e_exists?
(
table_name
)
if
data_sourc
e_exists?
(
table_name
)
primary_keys
(
table_name
)
columns
(
table_name
)
columns_hash
(
table_name
)
end
end
def
tabl
es
(
name
)
@
tabl
es
[
name
]
def
data_sourc
es
(
name
)
@
data_sourc
es
[
name
]
end
alias
tables
data_sources
deprecate
:tables
=>
"use #data_sources instead"
# Get the columns for a table
def
columns
(
table_name
)
...
...
@@ -64,36 +69,38 @@ def clear!
@columns
.
clear
@columns_hash
.
clear
@primary_keys
.
clear
@
tabl
es
.
clear
@
data_sourc
es
.
clear
@version
=
nil
end
def
size
[
@columns
,
@columns_hash
,
@primary_keys
,
@
tabl
es
].
map
(
&
:size
).
inject
:
+
[
@columns
,
@columns_hash
,
@primary_keys
,
@
data_sourc
es
].
map
(
&
:size
).
inject
:
+
end
# Clear out internal caches for t
able with +table_
name+.
def
clear_
table_cache!
(
table_
name
)
@columns
.
delete
table_
name
@columns_hash
.
delete
table_
name
@primary_keys
.
delete
table_
name
@
tables
.
delete
table_
name
# Clear out internal caches for t
he data source +
name+.
def
clear_
data_source_cache!
(
name
)
@columns
.
delete
name
@columns_hash
.
delete
name
@primary_keys
.
delete
name
@
data_sources
.
delete
name
end
alias
clear_table_cache!
clear_data_source_cache!
deprecate
:clear_table_cache!
=>
"use #clear_data_source_cache! instead"
def
marshal_dump
# if we get current version during initialization, it happens stack over flow.
@version
=
ActiveRecord
::
Migrator
.
current_version
[
@version
,
@columns
,
@columns_hash
,
@primary_keys
,
@
tabl
es
]
[
@version
,
@columns
,
@columns_hash
,
@primary_keys
,
@
data_sourc
es
]
end
def
marshal_load
(
array
)
@version
,
@columns
,
@columns_hash
,
@primary_keys
,
@
tabl
es
=
array
@version
,
@columns
,
@columns_hash
,
@primary_keys
,
@
data_sourc
es
=
array
end
private
def
prepare_
tabl
es
connection
.
tables
.
each
{
|
table
|
@tables
[
tabl
e
]
=
true
}
def
prepare_
data_sourc
es
connection
.
data_sources
.
each
{
|
source
|
@data_sources
[
sourc
e
]
=
true
}
end
end
end
...
...
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
浏览文件 @
7805fa2b
...
...
@@ -319,10 +319,12 @@ def tables(name = nil, table_name = nil) #:nodoc:
row
[
'name'
]
end
end
alias
data_sources
tables
def
table_exists?
(
table_name
)
table_name
&&
tables
(
nil
,
table_name
).
any?
end
alias
data_source_exists?
table_exists?
def
views
# :nodoc:
select_values
(
"SELECT name FROM sqlite_master WHERE type = 'view' AND name <> 'sqlite_sequence'"
,
'SCHEMA'
)
...
...
activerecord/lib/active_record/model_schema.rb
浏览文件 @
7805fa2b
...
...
@@ -213,7 +213,7 @@ def sequence_name=(value)
# Indicates whether the table associated with this class exists
def
table_exists?
connection
.
schema_cache
.
tabl
e_exists?
(
table_name
)
connection
.
schema_cache
.
data_sourc
e_exists?
(
table_name
)
end
def
attributes_builder
# :nodoc:
...
...
@@ -290,7 +290,7 @@ def content_columns
def
reset_column_information
connection
.
clear_cache!
undefine_attribute_methods
connection
.
schema_cache
.
clear_
tabl
e_cache!
(
table_name
)
connection
.
schema_cache
.
clear_
data_sourc
e_cache!
(
table_name
)
reload_schema_from_cache
end
...
...
activerecord/lib/active_record/railties/databases.rake
浏览文件 @
7805fa2b
...
...
@@ -255,7 +255,7 @@ db_namespace = namespace :db do
filename
=
File
.
join
(
ActiveRecord
::
Tasks
::
DatabaseTasks
.
db_dir
,
"schema_cache.dump"
)
con
.
schema_cache
.
clear!
con
.
tabl
es
.
each
{
|
table
|
con
.
schema_cache
.
add
(
table
)
}
con
.
data_sourc
es
.
each
{
|
table
|
con
.
schema_cache
.
add
(
table
)
}
open
(
filename
,
'wb'
)
{
|
f
|
f
.
write
(
Marshal
.
dump
(
con
.
schema_cache
))
}
end
...
...
activerecord/lib/active_record/schema_dumper.rb
浏览文件 @
7805fa2b
...
...
@@ -89,7 +89,7 @@ def extensions(stream)
end
def
tables
(
stream
)
sorted_tables
=
@connection
.
tabl
es
.
sort
-
@connection
.
views
sorted_tables
=
@connection
.
data_sourc
es
.
sort
-
@connection
.
views
sorted_tables
.
each
do
|
table_name
|
table
(
table_name
,
stream
)
unless
ignored?
(
table_name
)
...
...
activerecord/lib/active_record/type_caster/connection.rb
浏览文件 @
7805fa2b
...
...
@@ -20,7 +20,7 @@ def type_cast_for_database(attribute_name, value)
private
def
column_for
(
attribute_name
)
if
connection
.
schema_cache
.
tabl
e_exists?
(
table_name
)
if
connection
.
schema_cache
.
data_sourc
e_exists?
(
table_name
)
connection
.
schema_cache
.
columns_hash
(
table_name
)[
attribute_name
.
to_s
]
end
end
...
...
activerecord/test/active_record/connection_adapters/fake_adapter.rb
浏览文件 @
7805fa2b
...
...
@@ -7,7 +7,7 @@ def fake_connection(config)
module
ConnectionAdapters
class
FakeAdapter
<
AbstractAdapter
attr_accessor
:
tabl
es
,
:primary_keys
attr_accessor
:
data_sourc
es
,
:primary_keys
@columns
=
Hash
.
new
{
|
h
,
k
|
h
[
k
]
=
[]
}
class
<<
self
...
...
@@ -16,7 +16,7 @@ class << self
def
initialize
(
connection
,
logger
)
super
@
tables
=
[]
@
data_sources
=
[]
@primary_keys
=
{}
@columns
=
self
.
class
.
columns
end
...
...
@@ -37,7 +37,7 @@ def columns(table_name)
@columns
[
table_name
]
end
def
tabl
e_exists?
(
*
)
def
data_sourc
e_exists?
(
*
)
true
end
...
...
activerecord/test/cases/adapter_test.rb
浏览文件 @
7805fa2b
...
...
@@ -36,6 +36,21 @@ def test_table_exists?
assert
!
@connection
.
table_exists?
(
nil
)
end
def
test_data_sources
data_sources
=
@connection
.
data_sources
assert
data_sources
.
include?
(
"accounts"
)
assert
data_sources
.
include?
(
"authors"
)
assert
data_sources
.
include?
(
"tasks"
)
assert
data_sources
.
include?
(
"topics"
)
end
def
test_data_source_exists?
assert
@connection
.
data_source_exists?
(
"accounts"
)
assert
@connection
.
data_source_exists?
(
:accounts
)
assert_not
@connection
.
data_source_exists?
(
"nonexistingtable"
)
assert_not
@connection
.
data_source_exists?
(
nil
)
end
def
test_indexes
idx_name
=
"accounts_idx"
...
...
activerecord/test/cases/adapters/postgresql/schema_test.rb
浏览文件 @
7805fa2b
...
...
@@ -5,9 +5,11 @@
module
PGSchemaHelper
def
with_schema_search_path
(
schema_search_path
)
@connection
.
schema_search_path
=
schema_search_path
@connection
.
schema_cache
.
clear!
yield
if
block_given?
ensure
@connection
.
schema_search_path
=
"'$user', public"
@connection
.
schema_cache
.
clear!
end
end
...
...
activerecord/test/cases/connection_adapters/schema_cache_test.rb
浏览文件 @
7805fa2b
...
...
@@ -29,7 +29,7 @@ def test_caches_columns_hash
def
test_clearing
@cache
.
columns
(
'posts'
)
@cache
.
columns_hash
(
'posts'
)
@cache
.
tabl
es
(
'posts'
)
@cache
.
data_sourc
es
(
'posts'
)
@cache
.
primary_keys
(
'posts'
)
@cache
.
clear!
...
...
@@ -40,17 +40,22 @@ def test_clearing
def
test_dump_and_load
@cache
.
columns
(
'posts'
)
@cache
.
columns_hash
(
'posts'
)
@cache
.
tabl
es
(
'posts'
)
@cache
.
data_sourc
es
(
'posts'
)
@cache
.
primary_keys
(
'posts'
)
@cache
=
Marshal
.
load
(
Marshal
.
dump
(
@cache
))
assert_equal
11
,
@cache
.
columns
(
'posts'
).
size
assert_equal
11
,
@cache
.
columns_hash
(
'posts'
).
size
assert
@cache
.
tabl
es
(
'posts'
)
assert
@cache
.
data_sourc
es
(
'posts'
)
assert_equal
'id'
,
@cache
.
primary_keys
(
'posts'
)
end
def
test_table_methods_deprecation
assert_deprecated
{
assert
@cache
.
table_exists?
(
'posts'
)
}
assert_deprecated
{
assert
@cache
.
tables
(
'posts'
)
}
assert_deprecated
{
@cache
.
clear_table_cache!
(
'posts'
)
}
end
end
end
end
activerecord/test/cases/view_test.rb
浏览文件 @
7805fa2b
...
...
@@ -44,9 +44,15 @@ def test_view_exists
def
test_table_exists
view_name
=
Ebook
.
table_name
# TODO: switch this assertion around once we changed #tables to not return views.
assert
@connection
.
table_exists?
(
view_name
),
"'
#{
view_name
}
' table should exist"
end
def
test_views_ara_valid_data_sources
view_name
=
Ebook
.
table_name
assert
@connection
.
data_source_exists?
(
view_name
),
"'
#{
view_name
}
' should be a data source"
end
def
test_column_definitions
assert_equal
([[
"id"
,
:integer
],
[
"name"
,
:string
],
...
...
activerecord/test/models/contact.rb
浏览文件 @
7805fa2b
...
...
@@ -3,7 +3,7 @@ def self.extended(base)
base
.
class_eval
do
establish_connection
(
:adapter
=>
'fake'
)
connection
.
tabl
es
=
[
table_name
]
connection
.
data_sourc
es
=
[
table_name
]
connection
.
primary_keys
=
{
table_name
=>
'id'
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录