aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Stephenson <sam@37signals.com>2005-10-13 04:12:32 +0000
committerSam Stephenson <sam@37signals.com>2005-10-13 04:12:32 +0000
commit24c3599cc0f9987d48f2f1c4fe85fc84a6be3d31 (patch)
tree66730b24a1957dd7cf878fca1c62bc30505769bd
parent140a5f8f7b7deea713b5c7cbccbc8b57fecb2c7d (diff)
downloadrails-24c3599cc0f9987d48f2f1c4fe85fc84a6be3d31.tar.gz
rails-24c3599cc0f9987d48f2f1c4fe85fc84a6be3d31.tar.bz2
rails-24c3599cc0f9987d48f2f1c4fe85fc84a6be3d31.zip
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
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb9
-rw-r--r--railties/CHANGELOG2
-rw-r--r--railties/environments/environment.rb4
-rw-r--r--railties/lib/tasks/databases.rake68
-rw-r--r--railties/lib/tasks/testing.rake8
6 files changed, 63 insertions, 30 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index b0947a800d..8aa05e88ce 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add ActiveRecord::Base.schema_format setting which specifies how databases should be dumped [Sam Stephenson]
+
* Optimize postgresql selects. [skaes@web.de]
* Update DB2 adapter. #2206. [contact@maik-schmidt.de]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index b28247769f..3f0201a202 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -310,6 +310,15 @@ module ActiveRecord #:nodoc:
# mode, because the methods would be regenerated on each request.
cattr_accessor :generate_read_methods
@@generate_read_methods = true
+
+ # Specifies the format to use when dumping the database schema with Rails'
+ # Rakefile. If :sql, the schema is dumped as (potentially database-
+ # specific) SQL statements. If :ruby, the schema is dumped as an
+ # ActiveRecord::Schema file which can be loaded into any database that
+ # supports migrations. Use :ruby if you want to have different database
+ # adapters for, e.g., your development and test environments.
+ cattr_accessor :schema_format
+ @@schema_format = :sql
class << self # Class methods
# Find operates with three different retrieval approaches:
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