提交 2de84685 编写于 作者: A Arun Agrawal

Reorganize ActiveRecord tasks [Arun Agrawal & Abd ar-Rahman Hamidi]

上级 de804e90
...@@ -38,33 +38,38 @@ namespace :test do ...@@ -38,33 +38,38 @@ namespace :test do
end end
end end
desc 'Build MySQL and PostgreSQL test databases'
namespace :db do namespace :db do
desc 'Build MySQL and PostgreSQL test databases' task :create => ['mysql:db:build', 'postgresql:db:build']
task create: ['mysql:build_databases', 'postgresql:build_databases'] task :drop => ['mysql:db:drop', 'postgresql:db:drop']
desc 'Drop MySQL and PostgreSQL test databases'
task drop: ['mysql:drop_databases', 'postgresql:drop_databases']
end end
%w( mysql mysql2 postgresql sqlite3 sqlite3_mem firebird db2 oracle sybase openbase frontbase jdbcmysql jdbcpostgresql jdbcsqlite3 jdbcderby jdbch2 jdbchsqldb ).each do |adapter| %w( mysql mysql2 postgresql sqlite3 sqlite3_mem firebird db2 oracle sybase openbase frontbase jdbcmysql jdbcpostgresql jdbcsqlite3 jdbcderby jdbch2 jdbchsqldb ).each do |adapter|
Rake::TestTask.new("test_#{adapter}") { |t| namespace :test do
adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z0-9]+/] namespace :db do
t.libs << 'test' Rake::TestTask.new(adapter) { |t|
t.test_files = (Dir.glob( "test/cases/**/*_test.rb" ).reject { adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z0-9]+/]
|x| x =~ /\/adapters\// t.libs << 'test'
} + Dir.glob("test/cases/adapters/#{adapter_short}/**/*_test.rb")).sort t.test_files = (Dir.glob( "test/cases/**/*_test.rb" ).reject {
|x| x =~ /\/adapters\//
t.warning = true } + Dir.glob("test/cases/adapters/#{adapter_short}/**/*_test.rb")).sort
t.verbose = true
} t.warning = true
t.verbose = true
task "isolated_test_#{adapter}" do }
adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z0-9]+/]
puts [adapter, adapter_short].inspect namespace :isolated do
(Dir["test/cases/**/*_test.rb"].reject { task adapter do
|x| x =~ /\/adapters\// adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z0-9]+/]
} + Dir["test/cases/adapters/#{adapter_short}/**/*_test.rb"]).all? do |file| puts [adapter, adapter_short].inspect
sh(Gem.ruby, '-w' ,"-Itest", file) (Dir["test/cases/**/*_test.rb"].reject {
end or raise "Failures" |x| x =~ /\/adapters\//
} + Dir["test/cases/adapters/#{adapter_short}/**/*_test.rb"]).all? do |file|
sh(Gem.ruby, '-w' ,"-Itest", file)
end or raise "Failures"
end
end
end
end end
namespace adapter do namespace adapter do
...@@ -76,8 +81,8 @@ end ...@@ -76,8 +81,8 @@ end
end end
# Make sure the adapter test evaluates the env setting task # Make sure the adapter test evaluates the env setting task
task "test_#{adapter}" => "#{adapter}:env" task "test_#{adapter}" => ["#{adapter}:env", "test:db:#{adapter}"]
task "isolated_test_#{adapter}" => "#{adapter}:env" task "isolated_test_#{adapter}" => ["#{adapter}:env", "test:db:isolated:#{adapter}"]
end end
rule '.sqlite3' do |t| rule '.sqlite3' do |t|
...@@ -90,102 +95,109 @@ task :test_sqlite3 => [ ...@@ -90,102 +95,109 @@ task :test_sqlite3 => [
] ]
namespace :mysql do namespace :mysql do
desc 'Build the MySQL test databases' namespace :db do
task :build_databases do desc 'Build the MySQL test databases'
config = ARTest.config['connections']['mysql'] task :build do
%x( mysql --user=#{config['arunit']['username']} -e "create DATABASE #{config['arunit']['database']} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci ") config = ARTest.config['connections']['mysql']
%x( mysql --user=#{config['arunit2']['username']} -e "create DATABASE #{config['arunit2']['database']} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci ") %x( mysql --user=#{config['arunit']['username']} -e "create DATABASE #{config['arunit']['database']} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci ")
end %x( mysql --user=#{config['arunit2']['username']} -e "create DATABASE #{config['arunit2']['database']} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci ")
end
desc 'Drop the MySQL test databases' desc 'Drop the MySQL test databases'
task :drop_databases do task :drop do
config = ARTest.config['connections']['mysql'] config = ARTest.config['connections']['mysql']
%x( mysqladmin --user=#{config['arunit']['username']} -f drop #{config['arunit']['database']} ) %x( mysqladmin --user=#{config['arunit']['username']} -f drop #{config['arunit']['database']} )
%x( mysqladmin --user=#{config['arunit2']['username']} -f drop #{config['arunit2']['database']} ) %x( mysqladmin --user=#{config['arunit2']['username']} -f drop #{config['arunit2']['database']} )
end end
desc 'Rebuild the MySQL test databases' desc 'Rebuild the MySQL test databases'
task :rebuild_databases => [:drop_databases, :build_databases] task :rebuild => [:drop, :build]
end
end end
task :build_mysql_databases => 'mysql:build_databases' task :build_mysql_databases => 'mysql:db:build'
task :drop_mysql_databases => 'mysql:drop_databases' task :drop_mysql_databases => 'mysql:db:drop'
task :rebuild_mysql_databases => 'mysql:rebuild_databases' task :rebuild_mysql_databases => 'mysql:db:rebuild'
namespace :postgresql do namespace :postgresql do
desc 'Build the PostgreSQL test databases'
task :build_databases do
config = ARTest.config['connections']['postgresql']
%x( createdb -E UTF8 -T template0 #{config['arunit']['database']} )
%x( createdb -E UTF8 -T template0 #{config['arunit2']['database']} )
# notify about preparing hstore # notify about preparing hstore
if %x( createdb --version ).strip.gsub(/(.*)(\d\.\d\.\d)$/, "\\2") < "9.1.0" namespace :db do
puts "Please prepare hstore data type. See http://www.postgresql.org/docs/9.0/static/hstore.html" desc 'Build the PostgreSQL test databases'
task :build do
config = ARTest.config['connections']['postgresql']
%x( createdb -E UTF8 -T template0 #{config['arunit']['database']} )
%x( createdb -E UTF8 -T template0 #{config['arunit2']['database']} )
# prepare hstore
if %x( createdb --version ).strip.gsub(/(.*)(\d\.\d\.\d)$/, "\\2") < "9.1.0"
puts "Please prepare hstore data type. See http://www.postgresql.org/docs/9.0/static/hstore.html"
end
end end
end
desc 'Drop the PostgreSQL test databases' desc 'Drop the PostgreSQL test databases'
task :drop_databases do task :drop do
config = ARTest.config['connections']['postgresql'] config = ARTest.config['connections']['postgresql']
%x( dropdb #{config['arunit']['database']} ) %x( dropdb #{config['arunit']['database']} )
%x( dropdb #{config['arunit2']['database']} ) %x( dropdb #{config['arunit2']['database']} )
end end
desc 'Rebuild the PostgreSQL test databases' desc 'Rebuild the PostgreSQL test databases'
task :rebuild_databases => [:drop_databases, :build_databases] task :rebuild => [:drop, :build]
end
end end
task :build_postgresql_databases => 'postgresql:build_databases' task :build_postgresql_databases => 'postgresql:db:build'
task :drop_postgresql_databases => 'postgresql:drop_databases' task :drop_postgresql_databases => 'postgresql:db:drop'
task :rebuild_postgresql_databases => 'postgresql:rebuild_databases' task :rebuild_postgresql_databases => 'postgresql:db:rebuild'
namespace :frontbase do namespace :frontbase do
desc 'Build the FrontBase test databases' namespace :db do
task :build_databases => :rebuild_frontbase_databases desc 'Build the FrontBase test databases'
task :build => :rebuild_frontbase_databases
desc 'Rebuild the FrontBase test databases' desc 'Rebuild the FrontBase test databases'
task :rebuild_databases do task :rebuild do
build_frontbase_database = Proc.new do |db_name, sql_definition_file| build_frontbase_database = Proc.new do |db_name, sql_definition_file|
%( %(
STOP DATABASE #{db_name}; STOP DATABASE #{db_name};
DELETE DATABASE #{db_name}; DELETE DATABASE #{db_name};
CREATE DATABASE #{db_name}; CREATE DATABASE #{db_name};
CONNECT TO #{db_name} AS SESSION_NAME USER _SYSTEM; CONNECT TO #{db_name} AS SESSION_NAME USER _SYSTEM;
SET COMMIT FALSE; SET COMMIT FALSE;
CREATE USER RAILS; CREATE USER RAILS;
CREATE SCHEMA RAILS AUTHORIZATION RAILS; CREATE SCHEMA RAILS AUTHORIZATION RAILS;
COMMIT; COMMIT;
SET SESSION AUTHORIZATION RAILS; SET SESSION AUTHORIZATION RAILS;
SCRIPT '#{sql_definition_file}'; SCRIPT '#{sql_definition_file}';
COMMIT; COMMIT;
DISCONNECT ALL; DISCONNECT ALL;
) )
end end
config = ARTest.config['connections']['frontbase'] config = ARTest.config['connections']['frontbase']
create_activerecord_unittest = build_frontbase_database[config['arunit']['database'], File.join(SCHEMA_ROOT, 'frontbase.sql')] create_activerecord_unittest = build_frontbase_database[config['arunit']['database'], File.join(SCHEMA_ROOT, 'frontbase.sql')]
create_activerecord_unittest2 = build_frontbase_database[config['arunit2']['database'], File.join(SCHEMA_ROOT, 'frontbase2.sql')] create_activerecord_unittest2 = build_frontbase_database[config['arunit2']['database'], File.join(SCHEMA_ROOT, 'frontbase2.sql')]
execute_frontbase_sql = Proc.new do |sql| execute_frontbase_sql = Proc.new do |sql|
system(<<-SHELL) system(<<-SHELL)
/Library/FrontBase/bin/sql92 <<-SQL /Library/FrontBase/bin/sql92 <<-SQL
#{sql} #{sql}
SQL SQL
SHELL SHELL
end
execute_frontbase_sql[create_activerecord_unittest]
execute_frontbase_sql[create_activerecord_unittest2]
end end
execute_frontbase_sql[create_activerecord_unittest]
execute_frontbase_sql[create_activerecord_unittest2]
end end
end end
task :build_frontbase_databases => 'frontbase:build_databases' task :build_frontbase_databases => 'frontbase:db:build'
task :rebuild_frontbase_databases => 'frontbase:rebuild_databases' task :rebuild_frontbase_databases => 'frontbase:db:rebuild'
spec = eval(File.read('activerecord.gemspec')) spec = eval(File.read('activerecord.gemspec'))
......
...@@ -52,7 +52,7 @@ def heading ...@@ -52,7 +52,7 @@ def heading
def tasks def tasks
if activerecord? if activerecord?
['mysql:rebuild_databases', "#{adapter}:#{'isolated_' if isolated?}test"] ['mysql:db:rebuild', "#{adapter}:#{'isolated_' if isolated?}test"]
else else
["test#{':isolated' if isolated?}"] ["test#{':isolated' if isolated?}"]
end end
......
...@@ -265,15 +265,15 @@ This is how you run the Active Record test suite only for SQLite3: ...@@ -265,15 +265,15 @@ This is how you run the Active Record test suite only for SQLite3:
```bash ```bash
$ cd activerecord $ cd activerecord
$ bundle exec rake test_sqlite3 $ bundle exec rake test:db:sqlite3
``` ```
You can now run the tests as you did for `sqlite3`. The tasks are respectively You can now run the tests as you did for `sqlite3`. The tasks are respectively
```bash ```bash
test_mysql test:db:mysql
test_mysql2 test:db:mysql2
test_postgresql test:db:postgresql
``` ```
Finally, Finally,
......
...@@ -249,7 +249,7 @@ and create the test databases: ...@@ -249,7 +249,7 @@ and create the test databases:
```bash ```bash
$ cd activerecord $ cd activerecord
$ bundle exec rake mysql:build_databases $ bundle exec rake mysql:db:build
``` ```
PostgreSQL's authentication works differently. A simple way to set up the development environment for example is to run with your development account PostgreSQL's authentication works differently. A simple way to set up the development environment for example is to run with your development account
...@@ -267,7 +267,7 @@ and then create the test databases with ...@@ -267,7 +267,7 @@ and then create the test databases with
```bash ```bash
$ cd activerecord $ cd activerecord
$ bundle exec rake postgresql:build_databases $ bundle exec rake postgresql:db:build
``` ```
It is possible to build databases for both PostgreSQL and MySQL with It is possible to build databases for both PostgreSQL and MySQL with
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册