Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
9edeb32a
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,发现更多精彩内容 >>
提交
9edeb32a
编写于
6月 19, 2012
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #6782 from kennyj/db-rake-structure-dump
Refactor db:structure:dump task.
上级
82cf95cf
d8dfcacd
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
147 addition
and
14 deletion
+147
-14
activerecord/lib/active_record/railties/databases.rake
activerecord/lib/active_record/railties/databases.rake
+3
-13
activerecord/lib/active_record/tasks/database_tasks.rb
activerecord/lib/active_record/tasks/database_tasks.rb
+6
-0
activerecord/lib/active_record/tasks/mysql_database_tasks.rb
activerecord/lib/active_record/tasks/mysql_database_tasks.rb
+5
-0
activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
...cord/lib/active_record/tasks/postgresql_database_tasks.rb
+22
-0
activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
...verecord/lib/active_record/tasks/sqlite_database_tasks.rb
+5
-0
activerecord/test/cases/database_tasks_test.rb
activerecord/test/cases/database_tasks_test.rb
+35
-1
activerecord/test/cases/mysql_rake_test.rb
activerecord/test/cases/mysql_rake_test.rb
+24
-0
activerecord/test/cases/postgresql_rake_test.rb
activerecord/test/cases/postgresql_rake_test.rb
+25
-0
activerecord/test/cases/sqlite_rake_test.rb
activerecord/test/cases/sqlite_rake_test.rb
+22
-0
未找到文件。
activerecord/lib/active_record/railties/databases.rake
浏览文件 @
9edeb32a
...
@@ -275,21 +275,11 @@ db_namespace = namespace :db do
...
@@ -275,21 +275,11 @@ db_namespace = namespace :db do
abcs
=
ActiveRecord
::
Base
.
configurations
abcs
=
ActiveRecord
::
Base
.
configurations
filename
=
ENV
[
'DB_STRUCTURE'
]
||
File
.
join
(
Rails
.
root
,
"db"
,
"structure.sql"
)
filename
=
ENV
[
'DB_STRUCTURE'
]
||
File
.
join
(
Rails
.
root
,
"db"
,
"structure.sql"
)
case
abcs
[
Rails
.
env
][
'adapter'
]
case
abcs
[
Rails
.
env
][
'adapter'
]
when
/mysql/
,
'oci'
,
'oracle'
when
/mysql/
,
/postgresql/
,
/sqlite/
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_dump
(
abcs
[
Rails
.
env
],
filename
)
when
'oci'
,
'oracle'
ActiveRecord
::
Base
.
establish_connection
(
abcs
[
Rails
.
env
])
ActiveRecord
::
Base
.
establish_connection
(
abcs
[
Rails
.
env
])
File
.
open
(
filename
,
"w:utf-8"
)
{
|
f
|
f
<<
ActiveRecord
::
Base
.
connection
.
structure_dump
}
File
.
open
(
filename
,
"w:utf-8"
)
{
|
f
|
f
<<
ActiveRecord
::
Base
.
connection
.
structure_dump
}
when
/postgresql/
set_psql_env
(
abcs
[
Rails
.
env
])
search_path
=
abcs
[
Rails
.
env
][
'schema_search_path'
]
unless
search_path
.
blank?
search_path
=
search_path
.
split
(
","
).
map
{
|
search_path_part
|
"--schema=
#{
Shellwords
.
escape
(
search_path_part
.
strip
)
}
"
}.
join
(
" "
)
end
`pg_dump -i -s -x -O -f
#{
Shellwords
.
escape
(
filename
)
}
#{
search_path
}
#{
Shellwords
.
escape
(
abcs
[
Rails
.
env
][
'database'
])
}
`
raise
'Error dumping database'
if
$?
.
exitstatus
==
1
File
.
open
(
filename
,
"a"
)
{
|
f
|
f
<<
"SET search_path TO
#{
ActiveRecord
::
Base
.
connection
.
schema_search_path
}
;
\n\n
"
}
when
/sqlite/
dbfile
=
abcs
[
Rails
.
env
][
'database'
]
`sqlite3
#{
dbfile
}
.schema >
#{
filename
}
`
when
'sqlserver'
when
'sqlserver'
`smoscript -s
#{
abcs
[
Rails
.
env
][
'host'
]
}
-d
#{
abcs
[
Rails
.
env
][
'database'
]
}
-u
#{
abcs
[
Rails
.
env
][
'username'
]
}
-p
#{
abcs
[
Rails
.
env
][
'password'
]
}
-f
#{
filename
}
-A -U`
`smoscript -s
#{
abcs
[
Rails
.
env
][
'host'
]
}
-d
#{
abcs
[
Rails
.
env
][
'database'
]
}
-u
#{
abcs
[
Rails
.
env
][
'username'
]
}
-p
#{
abcs
[
Rails
.
env
][
'password'
]
}
-f
#{
filename
}
-A -U`
when
"firebird"
when
"firebird"
...
...
activerecord/lib/active_record/tasks/database_tasks.rb
浏览文件 @
9edeb32a
...
@@ -60,6 +60,12 @@ def purge(configuration)
...
@@ -60,6 +60,12 @@ def purge(configuration)
class_for_adapter
(
configuration
[
'adapter'
]).
new
(
configuration
).
purge
class_for_adapter
(
configuration
[
'adapter'
]).
new
(
configuration
).
purge
end
end
def
structure_dump
(
*
arguments
)
configuration
=
arguments
.
first
filename
=
arguments
.
delete_at
1
class_for_adapter
(
configuration
[
'adapter'
]).
new
(
*
arguments
).
structure_dump
(
filename
)
end
private
private
def
class_for_adapter
(
adapter
)
def
class_for_adapter
(
adapter
)
...
...
activerecord/lib/active_record/tasks/mysql_database_tasks.rb
浏览文件 @
9edeb32a
...
@@ -44,6 +44,11 @@ def charset
...
@@ -44,6 +44,11 @@ def charset
connection
.
charset
connection
.
charset
end
end
def
structure_dump
(
filename
)
establish_connection
configuration
File
.
open
(
filename
,
"w:utf-8"
)
{
|
f
|
f
<<
ActiveRecord
::
Base
.
connection
.
structure_dump
}
end
private
private
def
configuration
def
configuration
...
...
activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
浏览文件 @
9edeb32a
require
'shellwords'
module
ActiveRecord
module
ActiveRecord
module
Tasks
# :nodoc:
module
Tasks
# :nodoc:
class
PostgreSQLDatabaseTasks
# :nodoc:
class
PostgreSQLDatabaseTasks
# :nodoc:
...
@@ -33,6 +35,19 @@ def purge
...
@@ -33,6 +35,19 @@ def purge
create
true
create
true
end
end
def
structure_dump
(
filename
)
set_psql_env
search_path
=
configuration
[
'schema_search_path'
]
unless
search_path
.
blank?
search_path
=
search_path
.
split
(
","
).
map
{
|
search_path_part
|
"--schema=
#{
Shellwords
.
escape
(
search_path_part
.
strip
)
}
"
}.
join
(
" "
)
end
command
=
"pg_dump -i -s -x -O -f
#{
Shellwords
.
escape
(
filename
)
}
#{
search_path
}
#{
Shellwords
.
escape
(
configuration
[
'database'
])
}
"
raise
'Error dumping database'
unless
Kernel
.
system
(
command
)
File
.
open
(
filename
,
"a"
)
{
|
f
|
f
<<
"SET search_path TO
#{
ActiveRecord
::
Base
.
connection
.
schema_search_path
}
;
\n\n
"
}
end
private
private
def
configuration
def
configuration
...
@@ -49,6 +64,13 @@ def establish_master_connection
...
@@ -49,6 +64,13 @@ def establish_master_connection
'schema_search_path'
=>
'public'
'schema_search_path'
=>
'public'
)
)
end
end
def
set_psql_env
ENV
[
'PGHOST'
]
=
configuration
[
'host'
]
if
configuration
[
'host'
]
ENV
[
'PGPORT'
]
=
configuration
[
'port'
].
to_s
if
configuration
[
'port'
]
ENV
[
'PGPASSWORD'
]
=
configuration
[
'password'
].
to_s
if
configuration
[
'password'
]
ENV
[
'PGUSER'
]
=
configuration
[
'username'
].
to_s
if
configuration
[
'username'
]
end
end
end
end
end
end
end
activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
浏览文件 @
9edeb32a
...
@@ -31,6 +31,11 @@ def charset
...
@@ -31,6 +31,11 @@ def charset
connection
.
encoding
connection
.
encoding
end
end
def
structure_dump
(
filename
)
dbfile
=
configuration
[
'database'
]
`sqlite3
#{
dbfile
}
.schema >
#{
filename
}
`
end
private
private
def
configuration
def
configuration
...
...
activerecord/test/cases/database_tasks_test.rb
浏览文件 @
9edeb32a
...
@@ -303,7 +303,7 @@ def setup
...
@@ -303,7 +303,7 @@ def setup
returns
@postgresql_tasks
returns
@postgresql_tasks
ActiveRecord
::
Tasks
::
SQLiteDatabaseTasks
.
stubs
(
:new
).
returns
@sqlite_tasks
ActiveRecord
::
Tasks
::
SQLiteDatabaseTasks
.
stubs
(
:new
).
returns
@sqlite_tasks
end
end
def
test_mysql_charset
def
test_mysql_charset
@mysql_tasks
.
expects
(
:charset
)
@mysql_tasks
.
expects
(
:charset
)
...
@@ -328,4 +328,38 @@ def test_sqlite_charset
...
@@ -328,4 +328,38 @@ def test_sqlite_charset
ActiveRecord
::
Tasks
::
DatabaseTasks
.
charset
'adapter'
=>
'sqlite3'
ActiveRecord
::
Tasks
::
DatabaseTasks
.
charset
'adapter'
=>
'sqlite3'
end
end
end
end
class
DatabaseTasksStructureDumpTest
<
ActiveRecord
::
TestCase
def
setup
@mysql_tasks
,
@postgresql_tasks
,
@sqlite_tasks
=
stub
,
stub
,
stub
ActiveRecord
::
Tasks
::
MySQLDatabaseTasks
.
stubs
(
:new
).
returns
@mysql_tasks
ActiveRecord
::
Tasks
::
PostgreSQLDatabaseTasks
.
stubs
(
:new
).
returns
@postgresql_tasks
ActiveRecord
::
Tasks
::
SQLiteDatabaseTasks
.
stubs
(
:new
).
returns
@sqlite_tasks
end
def
test_mysql_structure_dump
@mysql_tasks
.
expects
(
:structure_dump
).
with
(
"awesome-file.sql"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_dump
({
'adapter'
=>
'mysql'
},
"awesome-file.sql"
)
end
def
test_mysql2_structure_dump
@mysql_tasks
.
expects
(
:structure_dump
).
with
(
"awesome-file.sql"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_dump
({
'adapter'
=>
'mysql2'
},
"awesome-file.sql"
)
end
def
test_postgresql_structure_dump
@postgresql_tasks
.
expects
(
:structure_dump
).
with
(
"awesome-file.sql"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_dump
({
'adapter'
=>
'postgresql'
},
"awesome-file.sql"
)
end
def
test_sqlite_structure_dump
@sqlite_tasks
.
expects
(
:structure_dump
).
with
(
"awesome-file.sql"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_dump
({
'adapter'
=>
'sqlite3'
},
"awesome-file.sql"
)
end
end
end
end
activerecord/test/cases/mysql_rake_test.rb
浏览文件 @
9edeb32a
...
@@ -194,4 +194,28 @@ def test_db_retrieves_charset
...
@@ -194,4 +194,28 @@ def test_db_retrieves_charset
ActiveRecord
::
Tasks
::
DatabaseTasks
.
charset
@configuration
ActiveRecord
::
Tasks
::
DatabaseTasks
.
charset
@configuration
end
end
end
end
class
MySQLStructureDumpTest
<
ActiveRecord
::
TestCase
def
setup
@connection
=
stub
(
:structure_dump
=>
true
)
@configuration
=
{
'adapter'
=>
'mysql'
,
'database'
=>
'test-db'
}
ActiveRecord
::
Base
.
stubs
(
:connection
).
returns
(
@connection
)
ActiveRecord
::
Base
.
stubs
(
:establish_connection
).
returns
(
true
)
end
def
test_structure_dump
filename
=
"awesome-file.sql"
ActiveRecord
::
Base
.
expects
(
:establish_connection
).
with
(
@configuration
)
@connection
.
expects
(
:structure_dump
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_dump
(
@configuration
,
filename
)
assert
File
.
exists?
(
filename
)
ensure
FileUtils
.
rm
(
filename
)
end
end
end
end
activerecord/test/cases/postgresql_rake_test.rb
浏览文件 @
9edeb32a
...
@@ -150,4 +150,29 @@ def test_db_retrieves_charset
...
@@ -150,4 +150,29 @@ def test_db_retrieves_charset
ActiveRecord
::
Tasks
::
DatabaseTasks
.
charset
@configuration
ActiveRecord
::
Tasks
::
DatabaseTasks
.
charset
@configuration
end
end
end
end
class
PostgreSQLStructureDumpTest
<
ActiveRecord
::
TestCase
def
setup
@connection
=
stub
(
:structure_dump
=>
true
)
@configuration
=
{
'adapter'
=>
'postgresql'
,
'database'
=>
'my-app-db'
}
ActiveRecord
::
Base
.
stubs
(
:connection
).
returns
(
@connection
)
ActiveRecord
::
Base
.
stubs
(
:establish_connection
).
returns
(
true
)
Kernel
.
stubs
(
:system
)
end
def
test_structure_dump
filename
=
"awesome-file.sql"
Kernel
.
expects
(
:system
).
with
(
"pg_dump -i -s -x -O -f
#{
filename
}
my-app-db"
).
returns
(
true
)
@connection
.
expects
(
:schema_search_path
).
returns
(
"foo"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_dump
(
@configuration
,
filename
)
assert
File
.
exists?
(
filename
)
ensure
FileUtils
.
rm
(
filename
)
end
end
end
end
activerecord/test/cases/sqlite_rake_test.rb
浏览文件 @
9edeb32a
...
@@ -123,4 +123,26 @@ def test_db_retrieves_charset
...
@@ -123,4 +123,26 @@ def test_db_retrieves_charset
ActiveRecord
::
Tasks
::
DatabaseTasks
.
charset
@configuration
,
'/rails/root'
ActiveRecord
::
Tasks
::
DatabaseTasks
.
charset
@configuration
,
'/rails/root'
end
end
end
end
class
SqliteStructureDumpTest
<
ActiveRecord
::
TestCase
def
setup
@database
=
"db_create.sqlite3"
@configuration
=
{
'adapter'
=>
'sqlite3'
,
'database'
=>
@database
}
end
def
test_structure_dump
dbfile
=
@database
filename
=
"awesome-file.sql"
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_dump
@configuration
,
filename
,
'/rails/root'
assert
File
.
exists?
(
dbfile
)
assert
File
.
exists?
(
filename
)
ensure
FileUtils
.
rm
(
filename
)
FileUtils
.
rm
(
dbfile
)
end
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录