Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
eee9f070
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,发现更多精彩内容 >>
未验证
提交
eee9f070
编写于
7月 08, 2019
作者:
G
Guillermo Iguaran
提交者:
GitHub
7月 08, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #36621 from robertomiranda/r/backport
(Backport 6-0-stable) Add database_exists? method to connection adapters
上级
9b7ed440
ee9036ea
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
71 addition
and
0 deletion
+71
-0
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+4
-0
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
...lib/active_record/connection_adapters/abstract_adapter.rb
+5
-0
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
...d/lib/active_record/connection_adapters/mysql2_adapter.rb
+6
-0
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
...b/active_record/connection_adapters/postgresql_adapter.rb
+6
-0
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
.../lib/active_record/connection_adapters/sqlite3_adapter.rb
+10
-0
activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb
...erecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb
+12
-0
activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
...test/cases/adapters/postgresql/postgresql_adapter_test.rb
+12
-0
activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
...ecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
+16
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
eee9f070
*
Add database_exists? method to connection adapters to check if a database exists.
*Guilherme Mansur*
*
PostgreSQL: Fix GROUP BY with ORDER BY virtual count attribute.
Fixes #36022.
...
...
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
浏览文件 @
eee9f070
...
...
@@ -264,6 +264,11 @@ def adapter_name
self
.
class
::
ADAPTER_NAME
end
# Does the database for this adapter exist?
def
self
.
database_exists?
(
config
)
raise
NotImplementedError
end
# Does this adapter support DDL rollbacks in transactions? That is, would
# CREATE TABLE or ALTER TABLE get rolled back by a transaction?
def
supports_ddl_transactions?
...
...
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
浏览文件 @
eee9f070
...
...
@@ -42,6 +42,12 @@ def initialize(connection, logger, connection_options, config)
configure_connection
end
def
self
.
database_exists?
(
config
)
!!
ActiveRecord
::
Base
.
mysql2_connection
(
config
)
rescue
ActiveRecord
::
NoDatabaseError
false
end
def
supports_json?
!
mariadb?
&&
database_version
>=
"5.7.8"
end
...
...
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
浏览文件 @
eee9f070
...
...
@@ -259,6 +259,12 @@ def initialize(connection, logger, connection_parameters, config)
@use_insert_returning
=
@config
.
key?
(
:insert_returning
)
?
self
.
class
.
type_cast_config_to_boolean
(
@config
[
:insert_returning
])
:
true
end
def
self
.
database_exists?
(
config
)
!!
ActiveRecord
::
Base
.
postgresql_connection
(
config
)
rescue
ActiveRecord
::
NoDatabaseError
false
end
# Is this connection alive and ready for queries?
def
active?
@lock
.
synchronize
do
...
...
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
浏览文件 @
eee9f070
...
...
@@ -98,6 +98,16 @@ def initialize(connection, logger, connection_options, config)
configure_connection
end
def
self
.
database_exists?
(
config
)
config
=
config
.
symbolize_keys
if
config
[
:database
]
==
":memory:"
return
true
else
database_file
=
defined?
(
Rails
.
root
)
?
File
.
expand_path
(
config
[
:database
],
Rails
.
root
)
:
config
[
:database
]
File
.
exist?
(
database_file
)
end
end
def
supports_ddl_transactions?
true
end
...
...
activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb
浏览文件 @
eee9f070
...
...
@@ -20,6 +20,18 @@ def test_exec_query_nothing_raises_with_no_result_queries
end
end
def
test_database_exists_returns_false_if_database_does_not_exist
config
=
ActiveRecord
::
Base
.
configurations
[
"arunit"
].
merge
(
database:
"inexistent_activerecord_unittest"
)
assert_not
ActiveRecord
::
ConnectionAdapters
::
Mysql2Adapter
.
database_exists?
(
config
),
"expected database to not exist"
end
def
test_database_exists_returns_true_when_the_database_exists
config
=
ActiveRecord
::
Base
.
configurations
[
"arunit"
]
assert
ActiveRecord
::
ConnectionAdapters
::
Mysql2Adapter
.
database_exists?
(
config
),
"expected database
#{
config
[
:database
]
}
to exist"
end
def
test_columns_for_distinct_zero_orders
assert_equal
"posts.id"
,
@conn
.
columns_for_distinct
(
"posts.id"
,
[])
...
...
activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
浏览文件 @
eee9f070
...
...
@@ -24,6 +24,18 @@ def test_bad_connection
end
end
def
test_database_exists_returns_false_when_the_database_does_not_exist
config
=
{
database:
"non_extant_database"
,
adapter:
"postgresql"
}
assert_not
ActiveRecord
::
ConnectionAdapters
::
PostgreSQLAdapter
.
database_exists?
(
config
),
"expected database
#{
config
[
:database
]
}
to not exist"
end
def
test_database_exists_returns_true_when_the_database_exists
config
=
ActiveRecord
::
Base
.
configurations
[
"arunit"
]
assert
ActiveRecord
::
ConnectionAdapters
::
PostgreSQLAdapter
.
database_exists?
(
config
),
"expected database
#{
config
[
:database
]
}
to exist"
end
def
test_primary_key
with_example_table
do
assert_equal
"id"
,
@connection
.
primary_key
(
"ex"
)
...
...
activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
浏览文件 @
eee9f070
...
...
@@ -30,6 +30,17 @@ def test_bad_connection
end
end
def
test_database_exists_returns_false_when_the_database_does_not_exist
assert_not
SQLite3Adapter
.
database_exists?
(
adapter:
"sqlite3"
,
database:
"non_extant_db"
),
"expected non_extant_db to not exist"
end
def
test_database_exists_returns_true_when_databae_exists
config
=
ActiveRecord
::
Base
.
configurations
[
"arunit"
]
assert
SQLite3Adapter
.
database_exists?
(
config
),
"expected
#{
config
[
:database
]
}
to exist"
end
unless
in_memory_db?
def
test_connect_with_url
original_connection
=
ActiveRecord
::
Base
.
remove_connection
...
...
@@ -53,6 +64,11 @@ def test_connect_memory_with_url
end
end
def
test_database_exists_returns_true_for_an_in_memory_db
assert
SQLite3Adapter
.
database_exists?
(
database:
":memory:"
),
"Expected in memory database to exist"
end
def
test_column_types
owner
=
Owner
.
create!
(
name:
"hello"
.
encode
(
"ascii-8bit"
))
owner
.
reload
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录