Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
55f8dfd9
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 搜索 >>
提交
55f8dfd9
编写于
6月 17, 2012
作者:
P
Pat Allan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rails is the default (but now override able) source for environment and path.
上级
8d5710c8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
57 addition
and
43 deletion
+57
-43
activerecord/lib/active_record/tasks/database_tasks.rb
activerecord/lib/active_record/tasks/database_tasks.rb
+18
-12
activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
...verecord/lib/active_record/tasks/sqlite_database_tasks.rb
+7
-3
activerecord/test/cases/database_tasks_test.rb
activerecord/test/cases/database_tasks_test.rb
+15
-14
activerecord/test/cases/sqlite_rake_test.rb
activerecord/test/cases/sqlite_rake_test.rb
+17
-14
未找到文件。
activerecord/lib/active_record/tasks/database_tasks.rb
浏览文件 @
55f8dfd9
...
...
@@ -6,8 +6,9 @@ class ActiveRecord::Tasks::DatabaseTasks
}
LOCAL_HOSTS
=
[
'127.0.0.1'
,
'localhost'
]
def
self
.
create
(
configuration
)
class_for_adapter
(
configuration
[
'adapter'
]).
new
(
configuration
).
create
def
self
.
create
(
*
arguments
)
configuration
=
arguments
.
first
class_for_adapter
(
configuration
[
'adapter'
]).
new
(
*
arguments
).
create
rescue
Exception
=>
error
$stderr
.
puts
error
,
*
(
error
.
backtrace
)
$stderr
.
puts
"Couldn't create database for
#{
configuration
.
inspect
}
"
...
...
@@ -17,13 +18,16 @@ def self.create_all
each_local_configuration
{
|
configuration
|
create
configuration
}
end
def
self
.
create_current
each_current_configuration
{
|
configuration
|
create
configuration
}
ActiveRecord
::
Base
.
establish_connection
Rails
.
env
def
self
.
create_current
(
environment
=
Rails
.
env
)
each_current_configuration
(
environment
)
{
|
configuration
|
create
configuration
}
ActiveRecord
::
Base
.
establish_connection
environment
end
def
self
.
drop
(
configuration
)
class_for_adapter
(
configuration
[
'adapter'
]).
new
(
configuration
).
drop
def
self
.
drop
(
*
arguments
)
configuration
=
arguments
.
first
class_for_adapter
(
configuration
[
'adapter'
]).
new
(
*
arguments
).
drop
rescue
Exception
=>
error
$stderr
.
puts
error
,
*
(
error
.
backtrace
)
$stderr
.
puts
"Couldn't drop
#{
configuration
[
'database'
]
}
"
...
...
@@ -33,8 +37,10 @@ def self.drop_all
each_local_configuration
{
|
configuration
|
drop
configuration
}
end
def
self
.
drop_current
each_current_configuration
{
|
configuration
|
drop
configuration
}
def
self
.
drop_current
(
environment
=
Rails
.
env
)
each_current_configuration
(
environment
)
{
|
configuration
|
drop
configuration
}
end
def
self
.
purge
(
configuration
)
...
...
@@ -48,9 +54,9 @@ def self.class_for_adapter(adapter)
TASKS_PATTERNS
[
key
]
end
def
self
.
each_current_configuration
environments
=
[
Rails
.
env
]
environments
<<
'test'
if
Rails
.
env
.
development?
def
self
.
each_current_configuration
(
environment
)
environments
=
[
environment
]
environments
<<
'test'
if
environment
.
development?
configurations
=
ActiveRecord
::
Base
.
configurations
.
values_at
(
*
environments
)
configurations
.
compact
.
each
do
|
configuration
|
...
...
activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
浏览文件 @
55f8dfd9
class
ActiveRecord::Tasks::SQLiteDatabaseTasks
delegate
:connection
,
:establish_connection
,
:to
=>
ActiveRecord
::
Base
def
initialize
(
configuration
)
@configuration
=
configuration
def
initialize
(
configuration
,
root
=
Rails
.
root
)
@configuration
,
@root
=
configuration
,
root
end
def
create
...
...
@@ -18,7 +18,7 @@ def create
def
drop
require
'pathname'
path
=
Pathname
.
new
configuration
[
'database'
]
file
=
path
.
absolute?
?
path
.
to_s
:
File
.
join
(
Rails
.
root
,
path
)
file
=
path
.
absolute?
?
path
.
to_s
:
File
.
join
(
root
,
path
)
FileUtils
.
rm
(
file
)
end
...
...
@@ -30,4 +30,8 @@ def drop
def
configuration
@configuration
end
def
root
@root
end
end
activerecord/test/cases/database_tasks_test.rb
浏览文件 @
55f8dfd9
...
...
@@ -106,32 +106,33 @@ def setup
end
def
test_creates_current_environment_database
Rails
.
stubs
(
:env
).
returns
ActiveSupport
::
StringInquirer
.
new
(
'production'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
expects
(
:create
).
with
(
'database'
=>
'prod-db'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create_current
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create_current
(
ActiveSupport
::
StringInquirer
.
new
(
'production'
)
)
end
def
test_creates_test_database_when_environment_is_database
Rails
.
stubs
(
:env
).
returns
ActiveSupport
::
StringInquirer
.
new
(
'development'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
expects
(
:create
).
with
(
'database'
=>
'dev-db'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
expects
(
:create
).
with
(
'database'
=>
'test-db'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create_current
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create_current
(
ActiveSupport
::
StringInquirer
.
new
(
'development'
)
)
end
def
test_establishes_connection_for_the_given_environment
Rails
.
stubs
(
:env
).
returns
ActiveSupport
::
StringInquirer
.
new
(
'development'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
stubs
(
:create
).
returns
true
ActiveRecord
::
Base
.
expects
(
:establish_connection
).
with
(
'development'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create_current
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create_current
(
ActiveSupport
::
StringInquirer
.
new
(
'development'
)
)
end
end
...
...
@@ -239,23 +240,23 @@ def setup
end
def
test_creates_current_environment_database
Rails
.
stubs
(
:env
).
returns
ActiveSupport
::
StringInquirer
.
new
(
'production'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
expects
(
:drop
).
with
(
'database'
=>
'prod-db'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
drop_current
ActiveRecord
::
Tasks
::
DatabaseTasks
.
drop_current
(
ActiveSupport
::
StringInquirer
.
new
(
'production'
)
)
end
def
test_creates_test_database_when_environment_is_database
Rails
.
stubs
(
:env
).
returns
ActiveSupport
::
StringInquirer
.
new
(
'development'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
expects
(
:drop
).
with
(
'database'
=>
'dev-db'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
expects
(
:drop
).
with
(
'database'
=>
'test-db'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
drop_current
ActiveRecord
::
Tasks
::
DatabaseTasks
.
drop_current
(
ActiveSupport
::
StringInquirer
.
new
(
'development'
)
)
end
end
...
...
activerecord/test/cases/sqlite_rake_test.rb
浏览文件 @
55f8dfd9
...
...
@@ -19,7 +19,7 @@ def setup
def
test_db_checks_database_exists
File
.
expects
(
:exist?
).
with
(
@database
).
returns
(
false
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create
@configuration
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create
@configuration
,
'/rails/root'
end
def
test_db_create_when_file_exists
...
...
@@ -27,7 +27,7 @@ def test_db_create_when_file_exists
$stderr
.
expects
(
:puts
).
with
(
"
#{
@database
}
already exists"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create
@configuration
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create
@configuration
,
'/rails/root'
end
def
test_db_create_with_file_does_nothing
...
...
@@ -36,18 +36,13 @@ def test_db_create_with_file_does_nothing
ActiveRecord
::
Base
.
expects
(
:establish_connection
).
never
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create
@configuration
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create
@configuration
,
'/rails/root'
end
def
test_db_create_establishes_a_connection
ActiveRecord
::
Base
.
expects
(
:establish_connection
).
with
(
@configuration
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create
@configuration
end
def
test_db_create_returns_the_connection
assert_equal
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create
(
@configuration
),
@connection
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create
@configuration
,
'/rails/root'
end
def
test_db_create_with_error_prints_message
...
...
@@ -57,7 +52,7 @@ def test_db_create_with_error_prints_message
$stderr
.
expects
(
:puts
).
with
(
"Couldn't create database for
#{
@configuration
.
inspect
}
"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create
(
@configuration
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create
@configuration
,
'/rails/root'
end
end
...
...
@@ -70,7 +65,6 @@ def setup
'database'
=>
@database
}
Rails
.
stubs
(
:root
).
returns
(
'/rails/root'
)
Pathname
.
stubs
(
:new
).
returns
(
@path
)
File
.
stubs
(
:join
).
returns
(
'/former/relative/path'
)
FileUtils
.
stubs
(
:rm
).
returns
(
true
)
...
...
@@ -79,7 +73,7 @@ def setup
def
test_creates_path_from_database
Pathname
.
expects
(
:new
).
with
(
@database
).
returns
(
@path
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
drop
@configuration
ActiveRecord
::
Tasks
::
DatabaseTasks
.
drop
@configuration
,
'/rails/root'
end
def
test_removes_file_with_absolute_path
...
...
@@ -87,7 +81,16 @@ def test_removes_file_with_absolute_path
FileUtils
.
expects
(
:rm
).
with
(
'/absolute/path'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
drop
@configuration
ActiveRecord
::
Tasks
::
DatabaseTasks
.
drop
@configuration
,
'/rails/root'
end
def
test_generates_absolute_path_with_given_root
@path
.
stubs
(
:absolute?
).
returns
(
false
)
File
.
expects
(
:join
).
with
(
'/rails/root'
,
@path
).
returns
(
'/former/relative/path'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
drop
@configuration
,
'/rails/root'
end
def
test_removes_file_with_relative_path
...
...
@@ -95,7 +98,7 @@ def test_removes_file_with_relative_path
FileUtils
.
expects
(
:rm
).
with
(
'/former/relative/path'
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
drop
@configuration
ActiveRecord
::
Tasks
::
DatabaseTasks
.
drop
@configuration
,
'/rails/root'
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录