Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
19911959
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 搜索 >>
提交
19911959
编写于
6月 21, 2012
作者:
K
kennyj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor db:structure:load task.
上级
4f700edc
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
129 addition
and
19 deletion
+129
-19
activerecord/lib/active_record/railties/databases.rake
activerecord/lib/active_record/railties/databases.rake
+2
-19
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
+8
-0
activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
...cord/lib/active_record/tasks/postgresql_database_tasks.rb
+5
-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
+34
-0
activerecord/test/cases/mysql_rake_test.rb
activerecord/test/cases/mysql_rake_test.rb
+25
-0
activerecord/test/cases/postgresql_rake_test.rb
activerecord/test/cases/postgresql_rake_test.rb
+22
-0
activerecord/test/cases/sqlite_rake_test.rb
activerecord/test/cases/sqlite_rake_test.rb
+22
-0
未找到文件。
activerecord/lib/active_record/railties/databases.rake
浏览文件 @
19911959
...
...
@@ -303,18 +303,8 @@ db_namespace = namespace :db do
abcs
=
ActiveRecord
::
Base
.
configurations
filename
=
ENV
[
'DB_STRUCTURE'
]
||
File
.
join
(
Rails
.
root
,
"db"
,
"structure.sql"
)
case
abcs
[
env
][
'adapter'
]
when
/mysql/
ActiveRecord
::
Base
.
establish_connection
(
abcs
[
env
])
ActiveRecord
::
Base
.
connection
.
execute
(
'SET foreign_key_checks = 0'
)
IO
.
read
(
filename
).
split
(
"
\n\n
"
).
each
do
|
table
|
ActiveRecord
::
Base
.
connection
.
execute
(
table
)
end
when
/postgresql/
set_psql_env
(
abcs
[
env
])
`psql -f "
#{
filename
}
"
#{
abcs
[
env
][
'database'
]
}
`
when
/sqlite/
dbfile
=
abcs
[
env
][
'database'
]
`sqlite3
#{
dbfile
}
< "
#{
filename
}
"`
when
/mysql/
,
/postgresql/
,
/sqlite/
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_load
(
abcs
[
Rails
.
env
],
filename
)
when
'sqlserver'
`sqlcmd -S
#{
abcs
[
env
][
'host'
]
}
-d
#{
abcs
[
env
][
'database'
]
}
-U
#{
abcs
[
env
][
'username'
]
}
-P
#{
abcs
[
env
][
'password'
]
}
-i
#{
filename
}
`
when
'oci'
,
'oracle'
...
...
@@ -462,10 +452,3 @@ end
def
firebird_db_string
(
config
)
FireRuby
::
Database
.
db_string_for
(
config
.
symbolize_keys
)
end
def
set_psql_env
(
config
)
ENV
[
'PGHOST'
]
=
config
[
'host'
]
if
config
[
'host'
]
ENV
[
'PGPORT'
]
=
config
[
'port'
].
to_s
if
config
[
'port'
]
ENV
[
'PGPASSWORD'
]
=
config
[
'password'
].
to_s
if
config
[
'password'
]
ENV
[
'PGUSER'
]
=
config
[
'username'
].
to_s
if
config
[
'username'
]
end
activerecord/lib/active_record/tasks/database_tasks.rb
浏览文件 @
19911959
...
...
@@ -66,6 +66,12 @@ def structure_dump(*arguments)
class_for_adapter
(
configuration
[
'adapter'
]).
new
(
*
arguments
).
structure_dump
(
filename
)
end
def
structure_load
(
*
arguments
)
configuration
=
arguments
.
first
filename
=
arguments
.
delete_at
1
class_for_adapter
(
configuration
[
'adapter'
]).
new
(
*
arguments
).
structure_load
(
filename
)
end
private
def
class_for_adapter
(
adapter
)
...
...
activerecord/lib/active_record/tasks/mysql_database_tasks.rb
浏览文件 @
19911959
...
...
@@ -49,6 +49,14 @@ def structure_dump(filename)
File
.
open
(
filename
,
"w:utf-8"
)
{
|
f
|
f
<<
ActiveRecord
::
Base
.
connection
.
structure_dump
}
end
def
structure_load
(
filename
)
establish_connection
(
configuration
)
connection
.
execute
(
'SET foreign_key_checks = 0'
)
IO
.
read
(
filename
).
split
(
"
\n\n
"
).
each
do
|
table
|
connection
.
execute
(
table
)
end
end
private
def
configuration
...
...
activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
浏览文件 @
19911959
...
...
@@ -48,6 +48,11 @@ def structure_dump(filename)
File
.
open
(
filename
,
"a"
)
{
|
f
|
f
<<
"SET search_path TO
#{
ActiveRecord
::
Base
.
connection
.
schema_search_path
}
;
\n\n
"
}
end
def
structure_load
(
filename
)
set_psql_env
Kernel
.
system
(
"psql -f
#{
filename
}
#{
configuration
[
'database'
]
}
"
)
end
private
def
configuration
...
...
activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
浏览文件 @
19911959
...
...
@@ -36,6 +36,11 @@ def structure_dump(filename)
`sqlite3
#{
dbfile
}
.schema >
#{
filename
}
`
end
def
structure_load
(
filename
)
dbfile
=
configuration
[
'database'
]
`sqlite3
#{
dbfile
}
< "
#{
filename
}
"`
end
private
def
configuration
...
...
activerecord/test/cases/database_tasks_test.rb
浏览文件 @
19911959
...
...
@@ -362,4 +362,38 @@ def test_sqlite_structure_dump
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_dump
({
'adapter'
=>
'sqlite3'
},
"awesome-file.sql"
)
end
end
class
DatabaseTasksStructureLoadTest
<
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_load
@mysql_tasks
.
expects
(
:structure_load
).
with
(
"awesome-file.sql"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_load
({
'adapter'
=>
'mysql'
},
"awesome-file.sql"
)
end
def
test_mysql2_structure_load
@mysql_tasks
.
expects
(
:structure_load
).
with
(
"awesome-file.sql"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_load
({
'adapter'
=>
'mysql2'
},
"awesome-file.sql"
)
end
def
test_postgresql_structure_load
@postgresql_tasks
.
expects
(
:structure_load
).
with
(
"awesome-file.sql"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_load
({
'adapter'
=>
'postgresql'
},
"awesome-file.sql"
)
end
def
test_sqlite_structure_load
@sqlite_tasks
.
expects
(
:structure_load
).
with
(
"awesome-file.sql"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_load
({
'adapter'
=>
'sqlite3'
},
"awesome-file.sql"
)
end
end
end
activerecord/test/cases/mysql_rake_test.rb
浏览文件 @
19911959
...
...
@@ -218,4 +218,29 @@ def test_structure_dump
FileUtils
.
rm
(
filename
)
end
end
class
MySQLStructureLoadTest
<
ActiveRecord
::
TestCase
def
setup
@connection
=
stub
@configuration
=
{
'adapter'
=>
'mysql'
,
'database'
=>
'test-db'
}
ActiveRecord
::
Base
.
stubs
(
:connection
).
returns
(
@connection
)
ActiveRecord
::
Base
.
stubs
(
:establish_connection
).
returns
(
true
)
end
def
test_structure_load
filename
=
"awesome-file.sql"
ActiveRecord
::
Base
.
expects
(
:establish_connection
).
with
(
@configuration
)
@connection
.
expects
(
:execute
).
twice
open
(
filename
,
'w'
)
{
|
f
|
f
.
puts
(
"SELECT CURDATE();"
)
}
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_load
(
@configuration
,
filename
)
ensure
FileUtils
.
rm
(
filename
)
end
end
end
activerecord/test/cases/postgresql_rake_test.rb
浏览文件 @
19911959
...
...
@@ -175,4 +175,26 @@ def test_structure_dump
FileUtils
.
rm
(
filename
)
end
end
class
PostgreSQLStructureLoadTest
<
ActiveRecord
::
TestCase
def
setup
@connection
=
stub
@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
(
"psql -f
#{
filename
}
my-app-db"
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_load
(
@configuration
,
filename
)
end
end
end
activerecord/test/cases/sqlite_rake_test.rb
浏览文件 @
19911959
...
...
@@ -145,4 +145,26 @@ def test_structure_dump
FileUtils
.
rm
(
dbfile
)
end
end
class
SqliteStructureLoadTest
<
ActiveRecord
::
TestCase
def
setup
@database
=
"db_create.sqlite3"
@configuration
=
{
'adapter'
=>
'sqlite3'
,
'database'
=>
@database
}
end
def
test_structure_load
dbfile
=
@database
filename
=
"awesome-file.sql"
open
(
filename
,
'w'
)
{
|
f
|
f
.
puts
(
"select datetime('now', 'localtime');"
)
}
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_load
@configuration
,
filename
,
'/rails/root'
assert
File
.
exists?
(
dbfile
)
ensure
FileUtils
.
rm
(
filename
)
FileUtils
.
rm
(
dbfile
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录