Permalink
Browse files

Support using different database adapters for development and test wi…

…th ActiveRecord::Base.schema_format = :ruby

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2549 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
sstephenson committed Oct 13, 2005
1 parent 140a5f8 commit 24c3599cc0f9987d48f2f1c4fe85fc84a6be3d31
View
@@ -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]
@@ -310,6 +310,15 @@ def self.reset_subclasses
# 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:
View
@@ -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]
@@ -31,6 +31,10 @@
# 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
@@ -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?
@@ -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,20 +21,20 @@ 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|
t.libs << "test"
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|
t.libs << "test"
t.pattern = 'test/functional/**/*_test.rb'
t.verbose = true
}
-task :test_functional => [ :clone_structure_to_test ]
+task :test_functional => [ :prepare_test_database ]

0 comments on commit 24c3599

Please sign in to comment.