Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
a6cda541
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,发现更多精彩内容 >>
提交
a6cda541
编写于
6月 17, 2012
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #6766 from frodsan/refactor_db_tasks
add :nodoc: to AR::Tasks and update to follow the coding conventions
上级
b9660bda
588e80fd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
226 addition
and
208 deletion
+226
-208
activerecord/lib/active_record/tasks/database_tasks.rb
activerecord/lib/active_record/tasks/database_tasks.rb
+71
-66
activerecord/lib/active_record/tasks/mysql_database_tasks.rb
activerecord/lib/active_record/tasks/mysql_database_tasks.rb
+75
-70
activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
...cord/lib/active_record/tasks/postgresql_database_tasks.rb
+49
-44
activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
...verecord/lib/active_record/tasks/sqlite_database_tasks.rb
+31
-28
未找到文件。
activerecord/lib/active_record/tasks/database_tasks.rb
浏览文件 @
a6cda541
class
ActiveRecord::Tasks::DatabaseTasks
TASKS_PATTERNS
=
{
/mysql/
=>
ActiveRecord
::
Tasks
::
MySQLDatabaseTasks
,
/postgresql/
=>
ActiveRecord
::
Tasks
::
PostgreSQLDatabaseTasks
,
/sqlite/
=>
ActiveRecord
::
Tasks
::
SQLiteDatabaseTasks
}
LOCAL_HOSTS
=
[
'127.0.0.1'
,
'localhost'
]
module
ActiveRecord
module
Tasks
# :nodoc:
class
DatabaseTasks
# :nodoc:
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
}
"
end
TASKS_PATTERNS
=
{
/mysql/
=>
ActiveRecord
::
Tasks
::
MySQLDatabaseTasks
,
/postgresql/
=>
ActiveRecord
::
Tasks
::
PostgreSQLDatabaseTasks
,
/sqlite/
=>
ActiveRecord
::
Tasks
::
SQLiteDatabaseTasks
}
LOCAL_HOSTS
=
[
'127.0.0.1'
,
'localhost'
]
def
self
.
create_all
each_local_configuration
{
|
configuration
|
create
configuration
}
end
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
}
"
end
def
self
.
create_current
(
environment
=
Rails
.
env
)
each_current_configuration
(
environment
)
{
|
configuration
|
create
configuration
}
ActiveRecord
::
Base
.
establish_connection
environment
end
def
self
.
create_all
each_local_configuration
{
|
configuration
|
create
configuration
}
end
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'
]
}
"
end
def
self
.
create_current
(
environment
=
Rails
.
env
)
each_current_configuration
(
environment
)
{
|
configuration
|
create
configuration
}
ActiveRecord
::
Base
.
establish_connection
environment
end
def
self
.
drop_all
each_local_configuration
{
|
configuration
|
drop
configuration
}
end
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'
]
}
"
end
def
self
.
drop_current
(
environment
=
Rails
.
env
)
each_current_configuration
(
environment
)
{
|
configuration
|
drop
configuration
}
end
def
self
.
drop_all
each_local_configuration
{
|
configuration
|
drop
configuration
}
end
def
self
.
purge
(
configuration
)
class_for_adapter
(
configuration
[
'adapter'
]).
new
(
configuration
).
purge
end
def
self
.
drop_current
(
environment
=
Rails
.
env
)
each_current_configuration
(
environment
)
{
|
configuration
|
drop
configuration
}
end
private
def
self
.
purge
(
configuration
)
class_for_adapter
(
configuration
[
'adapter'
]).
new
(
configuration
).
purge
end
def
self
.
class_for_adapter
(
adapter
)
key
=
TASKS_PATTERNS
.
keys
.
detect
{
|
pattern
|
adapter
[
pattern
]
}
TASKS_PATTERNS
[
key
]
end
private
def
self
.
each_current_configuration
(
environment
)
environments
=
[
environment
]
environments
<<
'test'
if
environment
.
development?
def
self
.
class_for_adapter
(
adapter
)
key
=
TASKS_PATTERNS
.
keys
.
detect
{
|
pattern
|
adapter
[
pattern
]
}
TASKS_PATTERNS
[
key
]
end
configurations
=
ActiveRecord
::
Base
.
configurations
.
values_at
(
*
environments
)
configurations
.
compact
.
each
do
|
configuration
|
yield
configuration
unless
configuration
[
'database'
].
blank?
end
end
def
self
.
each_current_configuration
(
environment
)
environments
=
[
environment
]
environments
<<
'test'
if
environment
.
development?
def
self
.
each_local_configuration
ActiveRecord
::
Base
.
configurations
.
each_value
do
|
configuration
|
next
unless
configuration
[
'database'
]
configurations
=
ActiveRecord
::
Base
.
configurations
.
values_at
(
*
environments
)
configurations
.
compact
.
each
do
|
configuration
|
yield
configuration
unless
configuration
[
'database'
].
blank?
end
end
def
self
.
each_local_configuration
ActiveRecord
::
Base
.
configurations
.
each_value
do
|
configuration
|
next
unless
configuration
[
'database'
]
if
local_database?
(
configuration
)
yield
configuration
else
$stderr
.
puts
"This task only modifies local databases.
#{
configuration
[
'database'
]
}
is on a remote host."
if
local_database?
(
configuration
)
yield
configuration
else
$stderr
.
puts
"This task only modifies local databases.
#{
configuration
[
'database'
]
}
is on a remote host."
end
end
end
end
end
def
self
.
local_database?
(
configuration
)
configuration
[
'host'
].
in?
(
LOCAL_HOSTS
)
||
configuration
[
'host'
].
blank?
def
self
.
local_database?
(
configuration
)
configuration
[
'host'
].
in?
(
LOCAL_HOSTS
)
||
configuration
[
'host'
].
blank?
end
end
end
end
end
\ No newline at end of file
activerecord/lib/active_record/tasks/mysql_database_tasks.rb
浏览文件 @
a6cda541
class
ActiveRecord::Tasks::MySQLDatabaseTasks
DEFAULT_CHARSET
=
ENV
[
'CHARSET'
]
||
'utf8'
DEFAULT_COLLATION
=
ENV
[
'COLLATION'
]
||
'utf8_unicode_ci'
ACCESS_DENIED_ERROR
=
1045
module
ActiveRecord
module
Tasks
# :nodoc:
class
MySQLDatabaseTasks
# :nodoc:
delegate
:connection
,
:establish_connection
,
:to
=>
ActiveRecord
::
Base
DEFAULT_CHARSET
=
ENV
[
'CHARSET'
]
||
'utf8'
DEFAULT_COLLATION
=
ENV
[
'COLLATION'
]
||
'utf8_unicode_ci'
ACCESS_DENIED_ERROR
=
1045
def
initialize
(
configuration
)
@configuration
=
configuration
end
delegate
:connection
,
:establish_connection
,
to:
ActiveRecord
::
Base
def
create
establish_connection
configuration_without_database
connection
.
create_database
configuration
[
'database'
],
creation_options
establish_connection
configuration
rescue
error_class
=>
error
raise
error
unless
error
.
errno
==
ACCESS_DENIED_ERROR
def
initialize
(
configuration
)
@configuration
=
configuration
end
$stdout
.
print
error
.
error
establish_connection
root_configuration_without_database
connection
.
create_database
configuration
[
'database'
],
creation_options
connection
.
execute
grant_statement
.
gsub
(
/\s+/
,
' '
).
strip
establish_connection
configuration
rescue
error_class
=>
error
$stderr
.
puts
error
.
error
$stderr
.
puts
"Couldn't create database for
#{
configuration
.
inspect
}
,
#{
creation_options
.
inspect
}
"
$stderr
.
puts
"(If you set the charset manually, make sure you have a matching collation)"
if
configuration
[
'charset'
]
end
def
create
establish_connection
configuration_without_database
connection
.
create_database
configuration
[
'database'
],
creation_options
establish_connection
configuration
rescue
error_class
=>
error
raise
error
unless
error
.
errno
==
ACCESS_DENIED_ERROR
def
drop
establish_connection
configuration
connection
.
drop_database
configuration
[
'database'
]
end
$stdout
.
print
error
.
error
establish_connection
root_configuration_without_database
connection
.
create_database
configuration
[
'database'
],
creation_options
connection
.
execute
grant_statement
.
gsub
(
/\s+/
,
' '
).
strip
establish_connection
configuration
rescue
error_class
=>
error
$stderr
.
puts
error
.
error
$stderr
.
puts
"Couldn't create database for
#{
configuration
.
inspect
}
,
#{
creation_options
.
inspect
}
"
$stderr
.
puts
"(If you set the charset manually, make sure you have a matching collation)"
if
configuration
[
'charset'
]
end
def
purge
establish_connection
:test
connection
.
recreate_database
configuration
[
'database'
],
creation_options
end
def
drop
establish_connection
configuration
connection
.
drop_database
configuration
[
'database'
]
end
private
def
purge
establish_connection
:test
connection
.
recreate_database
configuration
[
'database'
],
creation_options
end
def
configuration
@configuration
end
private
def
configuration_without_database
configuration
.
merge
(
'database'
=>
nil
)
end
def
configuration
@configuration
end
def
creation_options
{
:charset
=>
(
configuration
[
'charset'
]
||
DEFAULT_CHARSET
),
:collation
=>
(
configuration
[
'collation'
]
||
DEFAULT_COLLATION
)
}
end
def
configuration_without_database
configuration
.
merge
(
'database'
=>
nil
)
end
def
error_class
case
configuration
[
'adapter'
]
when
/jdbc/
require
'active_record/railties/jdbcmysql_error'
ArJdbcMySQL
::
Error
when
/mysql2/
Mysql2
::
Error
else
Mysql
::
Error
end
end
def
creation_options
{
charset:
(
configuration
[
'charset'
]
||
DEFAULT_CHARSET
),
collation:
(
configuration
[
'collation'
]
||
DEFAULT_COLLATION
)
}
end
def
error_class
case
configuration
[
'adapter'
]
when
/jdbc/
require
'active_record/railties/jdbcmysql_error'
ArJdbcMySQL
::
Error
when
/mysql2/
Mysql2
::
Error
else
Mysql
::
Error
end
end
def
grant_statement
<<-
SQL
def
grant_statement
<<-
SQL
GRANT ALL PRIVILEGES ON
#{
configuration
[
'database'
]
}
.*
TO '
#{
configuration
[
'username'
]
}
'@'localhost'
IDENTIFIED BY '
#{
configuration
[
'password'
]
}
' WITH GRANT OPTION;
SQL
end
SQL
end
def
root_configuration_without_database
configuration_without_database
.
merge
(
'username'
=>
'root'
,
'password'
=>
root_password
)
end
def
root_configuration_without_database
configuration_without_database
.
merge
(
'username'
=>
'root'
,
'password'
=>
root_password
)
end
def
root_password
$stdout
.
print
"Please provide the root password for your mysql installation
\n
>"
$stdin
.
gets
.
strip
def
root_password
$stdout
.
print
"Please provide the root password for your mysql installation
\n
>"
$stdin
.
gets
.
strip
end
end
end
end
end
\ No newline at end of file
activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
浏览文件 @
a6cda541
class
ActiveRecord::Tasks::PostgreSQLDatabaseTasks
DEFAULT_ENCODING
=
ENV
[
'CHARSET'
]
||
'utf8'
delegate
:connection
,
:establish_connection
,
:clear_active_connections!
,
:to
=>
ActiveRecord
::
Base
def
initialize
(
configuration
)
@configuration
=
configuration
end
def
create
(
master_established
=
false
)
establish_master_connection
unless
master_established
connection
.
create_database
configuration
[
'database'
],
configuration
.
merge
(
'encoding'
=>
encoding
)
establish_connection
configuration
end
def
drop
establish_master_connection
connection
.
drop_database
configuration
[
'database'
]
end
def
purge
clear_active_connections!
drop
create
true
end
private
def
configuration
@configuration
end
def
encoding
configuration
[
'encoding'
]
||
DEFAULT_ENCODING
end
def
establish_master_connection
establish_connection
configuration
.
merge
(
'database'
=>
'postgres'
,
'schema_search_path'
=>
'public'
)
module
ActiveRecord
module
Tasks
# :nodoc:
class
PostgreSQLDatabaseTasks
# :nodoc:
DEFAULT_ENCODING
=
ENV
[
'CHARSET'
]
||
'utf8'
delegate
:connection
,
:establish_connection
,
:clear_active_connections!
,
to:
ActiveRecord
::
Base
def
initialize
(
configuration
)
@configuration
=
configuration
end
def
create
(
master_established
=
false
)
establish_master_connection
unless
master_established
connection
.
create_database
configuration
[
'database'
],
configuration
.
merge
(
'encoding'
=>
encoding
)
establish_connection
configuration
end
def
drop
establish_master_connection
connection
.
drop_database
configuration
[
'database'
]
end
def
purge
clear_active_connections!
drop
create
true
end
private
def
configuration
@configuration
end
def
encoding
configuration
[
'encoding'
]
||
DEFAULT_ENCODING
end
def
establish_master_connection
establish_connection
configuration
.
merge
(
'database'
=>
'postgres'
,
'schema_search_path'
=>
'public'
)
end
end
end
end
end
\ No newline at end of file
activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
浏览文件 @
a6cda541
class
ActiveRecord::Tasks::SQLiteDatabaseTasks
delegate
:connection
,
:establish_connection
,
to:
ActiveRecord
::
Base
module
ActiveRecord
module
Tasks
# :nodoc:
class
SQLiteDatabaseTasks
# :nodoc:
def
initialize
(
configuration
,
root
=
Rails
.
root
)
@configuration
,
@root
=
configuration
,
root
end
delegate
:connection
,
:establish_connection
,
to:
ActiveRecord
::
Base
def
create
if
File
.
exist?
(
configuration
[
'database'
])
$stderr
.
puts
"
#{
configuration
[
'database'
]
}
already exists"
return
end
def
initialize
(
configuration
,
root
=
Rails
.
root
)
@configuration
,
@root
=
configuration
,
root
end
establish_connection
configuration
connection
end
def
create
if
File
.
exist?
(
configuration
[
'database'
])
$stderr
.
puts
"
#{
configuration
[
'database'
]
}
already exists"
return
end
def
drop
require
'pathname'
path
=
Pathname
.
new
configuration
[
'database'
]
file
=
path
.
absolute?
?
path
.
to_s
:
File
.
join
(
root
,
path
)
establish_connection
configuration
connection
end
FileUtils
.
rm
(
file
)
if
File
.
exist?
(
file
)
end
def
drop
require
'pathname'
path
=
Pathname
.
new
configuration
[
'database'
]
file
=
path
.
absolute?
?
path
.
to_s
:
File
.
join
(
root
,
path
)
alias
:purge
:drop
FileUtils
.
rm
(
file
)
if
File
.
exist?
(
file
)
end
private
alias
:purge
:drop
def
configuration
@configuration
end
private
def
root
@root
end
end
def
configuration
@configuration
end
def
root
@root
end
end
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录