Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
b41bfb04
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,发现更多精彩内容 >>
提交
b41bfb04
编写于
5月 06, 2012
作者:
J
Jeremy Kemper
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5910 from route/rails-dbconsole
Fast path starts the database console without loading the full Rails env
上级
a29bc1cf
346bb018
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
86 addition
and
40 deletion
+86
-40
railties/lib/rails/commands.rb
railties/lib/rails/commands.rb
+1
-2
railties/lib/rails/commands/dbconsole.rb
railties/lib/rails/commands/dbconsole.rb
+28
-10
railties/test/commands/dbconsole_test.rb
railties/test/commands/dbconsole_test.rb
+57
-28
未找到文件。
railties/lib/rails/commands.rb
浏览文件 @
b41bfb04
...
...
@@ -57,8 +57,7 @@
when
'dbconsole'
require
'rails/commands/dbconsole'
require
APP_PATH
Rails
::
DBConsole
.
start
(
Rails
.
application
)
Rails
::
DBConsole
.
start
when
'application'
,
'runner'
require
"rails/commands/
#{
command
}
"
...
...
railties/lib/rails/commands/dbconsole.rb
浏览文件 @
b41bfb04
...
...
@@ -5,15 +5,37 @@
module
Rails
class
DBConsole
attr_reader
:arguments
attr_reader
:arguments
,
:config
def
self
.
start
(
app
)
new
(
app
).
start
def
self
.
start
new
(
config
).
start
end
def
initialize
(
app
,
arguments
=
ARGV
)
@app
=
app
@arguments
=
arguments
def
self
.
config
config
=
begin
YAML
.
load
(
ERB
.
new
(
IO
.
read
(
"config/database.yml"
)).
result
)
rescue
SyntaxError
,
StandardError
require
APP_PATH
Rails
.
application
.
config
.
database_configuration
end
unless
config
[
env
]
abort
"No database is configured for the environment '
#{
env
}
'"
end
config
[
env
]
end
def
self
.
env
if
Rails
.
respond_to?
(
:env
)
Rails
.
env
else
ENV
[
"RAILS_ENV"
]
||
ENV
[
"RACK_ENV"
]
||
"development"
end
end
def
initialize
(
config
,
arguments
=
ARGV
)
@config
,
@arguments
=
config
,
arguments
end
def
start
...
...
@@ -38,10 +60,6 @@ def start
abort
opt
.
to_s
unless
(
0
..
1
).
include?
(
arguments
.
size
)
end
unless
config
=
@app
.
config
.
database_configuration
[
Rails
.
env
]
abort
"No database is configured for the environment '
#{
Rails
.
env
}
'"
end
case
config
[
"adapter"
]
when
/^mysql/
...
...
railties/test/commands/dbconsole_test.rb
浏览文件 @
b41bfb04
...
...
@@ -3,42 +3,70 @@
class
Rails::DBConsoleTest
<
ActiveSupport
::
TestCase
def
teardown
%w[PGUSER PGHOST PGPORT PGPASSWORD
'
]
.
each
{
|
key
|
ENV
.
delete
(
key
)}
%w[PGUSER PGHOST PGPORT PGPASSWORD]
.
each
{
|
key
|
ENV
.
delete
(
key
)}
end
def
test_no_database_configured
start
[],
false
def
test_config
Rails
::
DBConsole
.
const_set
(
:APP_PATH
,
"erb"
)
app_config
({})
capture_abort
{
Rails
::
DBConsole
.
config
}
assert
aborted
assert_match
/No database is configured for the environment '\w+'/
,
output
app_config
(
development:
"with_init"
)
assert_equal
Rails
::
DBConsole
.
config
,
"with_init"
app_db_file
(
"development:
\n
without_init"
)
assert_equal
Rails
::
DBConsole
.
config
,
"without_init"
app_db_file
(
"development:
\n
<%= Rails.something_app_specific %>"
)
assert_equal
Rails
::
DBConsole
.
config
,
"with_init"
app_db_file
(
"development:
\n\n
invalid"
)
assert_equal
Rails
::
DBConsole
.
config
,
"with_init"
end
def
test_env
assert_equal
Rails
::
DBConsole
.
env
,
"development"
Rails
.
stubs
(
:respond_to?
).
with
(
:env
).
returns
(
false
)
assert_equal
Rails
::
DBConsole
.
env
,
"development"
ENV
[
'RACK_ENV'
]
=
"rack_env"
assert_equal
Rails
::
DBConsole
.
env
,
"rack_env"
ENV
[
'RAILS_ENV'
]
=
"rails_env"
assert_equal
Rails
::
DBConsole
.
env
,
"rails_env"
end
def
test_mysql
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
%w[mysql mysql5]
,
'db'
)
start
[],
{
adapter:
'mysql'
,
database:
'db'
}
start
(
adapter:
'mysql'
,
database:
'db'
)
assert
!
aborted
end
def
test_mysql_full
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
%w[mysql mysql5]
,
'--host=locahost'
,
'--port=1234'
,
'--socket=socket'
,
'--user=user'
,
'--default-character-set=UTF-8'
,
'-p'
,
'db'
)
start
[],
{
adapter:
'mysql'
,
database:
'db'
,
host:
'locahost'
,
port:
1234
,
socket:
'socket'
,
username:
'user'
,
password:
'qwerty'
,
encoding:
'UTF-8'
}
start
(
adapter:
'mysql'
,
database:
'db'
,
host:
'locahost'
,
port:
1234
,
socket:
'socket'
,
username:
'user'
,
password:
'qwerty'
,
encoding:
'UTF-8'
)
assert
!
aborted
end
def
test_mysql_include_password
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
%w[mysql mysql5]
,
'--user=user'
,
'--password=qwerty'
,
'db'
)
start
[
'-p'
],
{
adapter:
'mysql'
,
database:
'db'
,
username:
'user'
,
password:
'qwerty'
}
start
({
adapter:
'mysql'
,
database:
'db'
,
username:
'user'
,
password:
'qwerty'
},
[
'-p'
])
assert
!
aborted
end
def
test_postgresql
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'psql'
,
'db'
)
start
[],
{
adapter:
'postgresql'
,
database:
'db'
}
start
(
adapter:
'postgresql'
,
database:
'db'
)
assert
!
aborted
end
def
test_postgresql_full
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'psql'
,
'db'
)
start
[],
{
adapter:
'postgresql'
,
database:
'db'
,
username:
'user'
,
password:
'q1w2e3'
,
host:
'host'
,
port:
5432
}
start
(
adapter:
'postgresql'
,
database:
'db'
,
username:
'user'
,
password:
'q1w2e3'
,
host:
'host'
,
port:
5432
)
assert
!
aborted
assert_equal
'user'
,
ENV
[
'PGUSER'
]
assert_equal
'host'
,
ENV
[
'PGHOST'
]
...
...
@@ -48,7 +76,7 @@ def test_postgresql_full
def
test_postgresql_include_password
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'psql'
,
'db'
)
start
[
'-p'
],
{
adapter:
'postgresql'
,
database:
'db'
,
username:
'user'
,
password:
'q1w2e3'
}
start
({
adapter:
'postgresql'
,
database:
'db'
,
username:
'user'
,
password:
'q1w2e3'
},
[
'-p'
])
assert
!
aborted
assert_equal
'user'
,
ENV
[
'PGUSER'
]
assert_equal
'q1w2e3'
,
ENV
[
'PGPASSWORD'
]
...
...
@@ -56,42 +84,42 @@ def test_postgresql_include_password
def
test_sqlite
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlite'
,
'db'
)
start
[],
{
adapter:
'sqlite'
,
database:
'db'
}
start
(
adapter:
'sqlite'
,
database:
'db'
)
assert
!
aborted
end
def
test_sqlite3
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlite3'
,
'db'
)
start
[],
{
adapter:
'sqlite3'
,
database:
'db'
}
start
(
adapter:
'sqlite3'
,
database:
'db'
)
assert
!
aborted
end
def
test_sqlite3_mode
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlite3'
,
'-html'
,
'db'
)
start
[
'--mode'
,
'html'
],
{
adapter:
'sqlite3'
,
database:
'db'
}
start
({
adapter:
'sqlite3'
,
database:
'db'
},
[
'--mode'
,
'html'
])
assert
!
aborted
end
def
test_sqlite3_header
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlite3'
,
'-header'
,
'db'
)
start
[
'--header'
],
{
adapter:
'sqlite3'
,
database:
'db'
}
start
({
adapter:
'sqlite3'
,
database:
'db'
},
[
'--header'
])
assert
!
aborted
end
def
test_oracle
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlplus'
,
'user@db'
)
start
[],
{
adapter:
'oracle'
,
database:
'db'
,
username:
'user'
,
password:
'secret'
}
start
(
adapter:
'oracle'
,
database:
'db'
,
username:
'user'
,
password:
'secret'
)
assert
!
aborted
end
def
test_oracle_include_password
dbconsole
.
expects
(
:find_cmd_and_exec
).
with
(
'sqlplus'
,
'user/secret@db'
)
start
[
'-p'
],
{
adapter:
'oracle'
,
database:
'db'
,
username:
'user'
,
password:
'secret'
}
start
({
adapter:
'oracle'
,
database:
'db'
,
username:
'user'
,
password:
'secret'
},
[
'-p'
])
assert
!
aborted
end
def
test_unknown_command_line_client
start
[],
{
adapter:
'unknown'
,
database:
'db'
}
start
(
adapter:
'unknown'
,
database:
'db'
)
assert
aborted
assert_match
/Unknown command-line client for db/
,
output
end
...
...
@@ -100,29 +128,30 @@ def test_unknown_command_line_client
attr_reader
:aborted
,
:output
def
dbconsole
@dbconsole
||=
Rails
::
DBConsole
.
new
(
app
)
@dbconsole
||=
Rails
::
DBConsole
.
new
(
nil
)
end
def
start
(
argv
=
[],
database_configuration
=
{})
dbconsole
.
stubs
(
arguments:
argv
)
app
.
config
.
stubs
(
database_configuration:
{
Rails
.
env
=>
database_configuration
?
database_configuration
.
stringify_keys
:
database_configuration
})
def
start
(
config
=
{},
argv
=
[])
dbconsole
.
stubs
(
config:
config
.
stringify_keys
,
arguments:
argv
)
capture_abort
{
dbconsole
.
start
}
end
def
capture_abort
@aborted
=
false
@output
=
capture
(
:stderr
)
do
begin
dbconsole
.
start
yield
rescue
SystemExit
@aborted
=
true
end
end
end
def
app
@app
||=
begin
config
=
mock
(
"config"
)
stub
(
"app"
,
config:
config
)
end
def
app_db_file
(
result
)
IO
.
stubs
(
:read
).
with
(
"config/database.yml"
).
returns
(
result
)
end
def
app_config
(
result
)
Rails
.
application
.
config
.
stubs
(
:database_configuration
).
returns
(
result
.
stringify_keys
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录