From 24c3599cc0f9987d48f2f1c4fe85fc84a6be3d31 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Thu, 13 Oct 2005 04:12:32 +0000 Subject: Support using different database adapters for development and test with ActiveRecord::Base.schema_format = :ruby git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2549 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- railties/CHANGELOG | 2 ++ railties/environments/environment.rb | 4 +++ railties/lib/tasks/databases.rake | 68 ++++++++++++++++++++++-------------- railties/lib/tasks/testing.rake | 8 ++--- 4 files changed, 52 insertions(+), 30 deletions(-) (limited to 'railties') diff --git a/railties/CHANGELOG b/railties/CHANGELOG index ae5086f43b..44b349e68f 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Support using different database adapters for development and test with ActiveRecord::Base.schema_format = :ruby [Sam Stephenson] + * Make webrick work with session(:off) * Add --version, -v option to the Rails command. Closes #1840. [stancell] diff --git a/railties/environments/environment.rb b/railties/environments/environment.rb index 66a7e74212..576667d72e 100644 --- a/railties/environments/environment.rb +++ b/railties/environments/environment.rb @@ -31,6 +31,10 @@ Rails::Initializer.run do |config| # Make Active Record use UTC-base instead of local time # config.active_record.default_timezone = :utc + + # Use Active Record's schema dumper instead of SQL when creating the test database + # (enables use of different database adapters for development and test environments) + # config.active_record.schema_format = :ruby # See Rails::Configuration for more options end diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake index b416297406..a2db15086e 100644 --- a/railties/lib/tasks/databases.rake +++ b/railties/lib/tasks/databases.rake @@ -27,6 +27,38 @@ task :db_schema_import => :environment do load file end +desc "Recreate the test database from the current environment's database schema." +task :clone_schema_to_test => :db_schema_dump do + ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) + Rake::Task[:db_schema_import].invoke +end + +desc "Dump the database structure to a SQL file" +task :db_structure_dump => :environment do + abcs = ActiveRecord::Base.configurations + case abcs[RAILS_ENV]["adapter"] + when "mysql", "oci" + ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) + File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } + when "postgresql" + ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"] + ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"] + ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"] + `pg_dump -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{abcs[RAILS_ENV]["database"]}` + when "sqlite", "sqlite3" + `#{abcs[RAILS_ENV]["adapter"]} #{abcs[RAILS_ENV]["dbfile"]} .schema > db/#{RAILS_ENV}_structure.sql` + when "sqlserver" + `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r` + `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r` + else + raise "Task not supported by '#{abcs["test"]["adapter"]}'" + end + + if ActiveRecord::Base.connection.supports_migrations? + File.open("db/#{RAILS_ENV}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information } + end +end + desc "Recreate the test databases from the development structure" task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do abcs = ActiveRecord::Base.configurations @@ -56,32 +88,6 @@ task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do end end -desc "Dump the database structure to a SQL file" -task :db_structure_dump => :environment do - abcs = ActiveRecord::Base.configurations - case abcs[RAILS_ENV]["adapter"] - when "mysql", "oci" - ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) - File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } - when "postgresql" - ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"] - ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"] - ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"] - `pg_dump -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{abcs[RAILS_ENV]["database"]}` - when "sqlite", "sqlite3" - `#{abcs[RAILS_ENV]["adapter"]} #{abcs[RAILS_ENV]["dbfile"]} .schema > db/#{RAILS_ENV}_structure.sql` - when "sqlserver" - `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r` - `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r` - else - raise "Task not supported by '#{abcs["test"]["adapter"]}'" - end - - if ActiveRecord::Base.connection.supports_migrations? - File.open("db/#{RAILS_ENV}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information } - end -end - desc "Empty the test database" task :purge_test_database => :environment do abcs = ActiveRecord::Base.configurations @@ -111,6 +117,16 @@ task :purge_test_database => :environment do end end +def prepare_test_database_task + {:sql => :clone_structure_to_test, + :ruby => :clone_schema_to_test}[ActiveRecord::Base.schema_format] +end + +desc 'Prepare the test database and load the schema' +task :prepare_test_database => :environment do + Rake::Task[prepare_test_database_task].invoke +end + desc "Creates a sessions table for use with CGI::Session::ActiveRecordStore" task :create_sessions_table => :environment do raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations? diff --git a/railties/lib/tasks/testing.rake b/railties/lib/tasks/testing.rake index 3542b74763..7273a69ed4 100644 --- a/railties/lib/tasks/testing.rake +++ b/railties/lib/tasks/testing.rake @@ -11,7 +11,7 @@ def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago) end desc 'Test recent changes.' -Rake::TestTask.new(:recent => [ :clone_structure_to_test ]) do |t| +Rake::TestTask.new(:recent => [ :prepare_test_database ]) do |t| since = TEST_CHANGES_SINCE touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } + recent_tests('app/models/*.rb', 'test/unit', since) + @@ -21,7 +21,7 @@ Rake::TestTask.new(:recent => [ :clone_structure_to_test ]) do |t| t.verbose = true t.test_files = touched.uniq end -task :test_recent => [ :clone_structure_to_test ] +task :test_recent => [ :prepare_test_database ] desc "Run the unit tests in test/unit" Rake::TestTask.new("test_units") { |t| @@ -29,7 +29,7 @@ Rake::TestTask.new("test_units") { |t| t.pattern = 'test/unit/**/*_test.rb' t.verbose = true } -task :test_units => [ :clone_structure_to_test ] +task :test_units => [ :prepare_test_database ] desc "Run the functional tests in test/functional" Rake::TestTask.new("test_functional") { |t| @@ -37,4 +37,4 @@ Rake::TestTask.new("test_functional") { |t| t.pattern = 'test/functional/**/*_test.rb' t.verbose = true } -task :test_functional => [ :clone_structure_to_test ] \ No newline at end of file +task :test_functional => [ :prepare_test_database ] \ No newline at end of file -- cgit v1.2.3