diff options
author | Jon Leighton <j@jonathanleighton.com> | 2013-12-29 11:05:04 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2014-01-02 13:49:00 +0000 |
commit | ff7ab3bc78abc3e8439a57ef7d755c5aa5b069f4 (patch) | |
tree | 5ad6aed8ee454c320967d767229debde268da2e2 /activerecord | |
parent | a1d0c0fa3d8ca97edc8f2a1d6ba96af19221dbad (diff) | |
download | rails-ff7ab3bc78abc3e8439a57ef7d755c5aa5b069f4.tar.gz rails-ff7ab3bc78abc3e8439a57ef7d755c5aa5b069f4.tar.bz2 rails-ff7ab3bc78abc3e8439a57ef7d755c5aa5b069f4.zip |
Automatically maintain test database schema
* Move check from generated helper to test_help.rb, so that all
applications can benefit
* Rather than just raising when the test schema has pending migrations,
try to load in the schema and only raise if there are pending
migrations afterwards
* Opt out of the check by setting
config.active_record.maintain_test_schema = false
* Deprecate db:test:* tasks. The test helper is now fully responsible
for maintaining the test schema, so we don't need rake tasks for this.
This is also a speed improvement since we're no longer reloading the
test database on every call to "rake test".
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 11 | ||||
-rw-r--r-- | activerecord/lib/active_record/core.rb | 3 | ||||
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 13 | ||||
-rw-r--r-- | activerecord/lib/active_record/railtie.rb | 9 | ||||
-rw-r--r-- | activerecord/lib/active_record/railties/databases.rake | 34 | ||||
-rw-r--r-- | activerecord/lib/active_record/tasks/database_tasks.rb | 44 |
6 files changed, 86 insertions, 28 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index e60925827b..e874486b53 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,12 @@ +* Since the `test_help.rb` in Railties now automatically maintains + your test schema, the `rake db:test:*` tasks are deprecated. This + doesn't stop you manually running other tasks on your test database + if needed: + + rake db:schema:load RAILS_ENV=test + + *Jon Leighton* + * Fix presence validator for association when the associated record responds to `to_a`. *gmarik* @@ -54,6 +63,8 @@ * Deprecated use of string argument as a configuration lookup in `ActiveRecord::Base.establish_connection`. Instead, a symbol must be given. +* Deprecated use of string argument as a configuration lookup in `ActiveRecord::Base.establish_connection`. Instead, a symbol must be given. + *José Valim* * Fixed `update_column`, `update_columns`, and `update_all` to correctly serialize diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 8808ad5a4c..18ee77f6fe 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -69,6 +69,9 @@ module ActiveRecord mattr_accessor :timestamped_migrations, instance_writer: false self.timestamped_migrations = true + # :nodoc: + mattr_accessor :maintain_test_schema, instance_accessor: false + def self.disable_implicit_join_references=(value) ActiveSupport::Deprecation.warn("Implicit join references were removed with Rails 4.1." \ "Make sure to remove this configuration because it does nothing.") diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 7d7e97e6c9..fc84b907a5 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -389,6 +389,19 @@ module ActiveRecord raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration? end + def load_schema_if_pending! + if ActiveRecord::Migrator.needs_migration? + ActiveRecord::Tasks::DatabaseTasks.load_schema + check_pending! + end + end + + def maintain_test_schema! # :nodoc: + if ActiveRecord::Base.maintain_test_schema + suppress_messages { load_schema_if_pending! } + end + end + def method_missing(name, *args, &block) # :nodoc: (delegate || superclass.delegate).send(name, *args, &block) end diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 2a8cd83285..ec85b3c843 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -31,22 +31,15 @@ module ActiveRecord config.active_record.use_schema_cache_dump = true + config.active_record.maintain_test_schema = true config.eager_load_namespaces << ActiveRecord rake_tasks do require "active_record/base" - ActiveRecord::Tasks::DatabaseTasks.seed_loader = Rails.application - ActiveRecord::Tasks::DatabaseTasks.env = Rails.env - namespace :db do task :load_config do - ActiveRecord::Tasks::DatabaseTasks.db_dir = Rails.application.config.paths["db"].first - ActiveRecord::Tasks::DatabaseTasks.migrations_paths = Rails.application.paths['db/migrate'].to_a - ActiveRecord::Tasks::DatabaseTasks.fixtures_path = File.join Rails.root, 'test', 'fixtures' - ActiveRecord::Tasks::DatabaseTasks.root = Rails.root - configuration = if ENV["DATABASE_URL"] { Rails.env => ENV["DATABASE_URL"] } else diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 4b769aa11c..25d42bbf2d 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -234,9 +234,7 @@ db_namespace = namespace :db do desc 'Load a schema.rb file into the database' task :load => [:environment, :load_config] do - file = ENV['SCHEMA'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema.rb') - ActiveRecord::Tasks::DatabaseTasks.check_schema_file(file) - load(file) + ActiveRecord::Tasks::DatabaseTasks.load_schema(:ruby, ENV['SCHEMA']) end task :load_if_ruby => ['db:create', :environment] do @@ -281,10 +279,7 @@ db_namespace = namespace :db do # desc "Recreate the databases from the structure.sql file" task :load => [:environment, :load_config] do - filename = ENV['DB_STRUCTURE'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "structure.sql") - ActiveRecord::Tasks::DatabaseTasks.check_schema_file(filename) - current_config = ActiveRecord::Tasks::DatabaseTasks.current_config - ActiveRecord::Tasks::DatabaseTasks.structure_load(current_config, filename) + ActiveRecord::Tasks::DatabaseTasks.load_schema(:sql, ENV['DB_STRUCTURE']) end task :load_if_sql => ['db:create', :environment] do @@ -294,8 +289,15 @@ db_namespace = namespace :db do namespace :test do + task :deprecated do + Rake.application.top_level_tasks.grep(/^db:test:/).each do |task| + $stderr.puts "WARNING: #{task} is deprecated. The Rails test helper now maintains " \ + "your test schema automatically, see the release notes for details." + end + end + # desc "Recreate the test database from the current schema" - task :load => 'db:test:purge' do + task :load => %w(db:test:deprecated db:test:purge) do case ActiveRecord::Base.schema_format when :ruby db_namespace["test:load_schema"].invoke @@ -305,7 +307,7 @@ db_namespace = namespace :db do end # desc "Recreate the test database from an existent schema.rb file" - task :load_schema => 'db:test:purge' do + task :load_schema => %w(db:test:deprecated db:test:purge) do begin should_reconnect = ActiveRecord::Base.connection_pool.active_connection? ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) @@ -319,7 +321,7 @@ db_namespace = namespace :db do end # desc "Recreate the test database from an existent structure.sql file" - task :load_structure => 'db:test:purge' do + task :load_structure => %w(db:test:deprecated db:test:purge) do begin ActiveRecord::Tasks::DatabaseTasks.current_config(:config => ActiveRecord::Base.configurations['test']) db_namespace["structure:load"].invoke @@ -329,7 +331,7 @@ db_namespace = namespace :db do end # desc "Recreate the test database from a fresh schema" - task :clone => :environment do + task :clone => %w(db:test:deprecated environment) do case ActiveRecord::Base.schema_format when :ruby db_namespace["test:clone_schema"].invoke @@ -339,18 +341,18 @@ db_namespace = namespace :db do end # desc "Recreate the test database from a fresh schema.rb file" - task :clone_schema => ["db:schema:dump", "db:test:load_schema"] + task :clone_schema => %w(db:test:deprecated db:schema:dump db:test:load_schema) # desc "Recreate the test database from a fresh structure.sql file" - task :clone_structure => [ "db:structure:dump", "db:test:load_structure" ] + task :clone_structure => %w(db:structure:dump db:test:load_structure) # desc "Empty the test database" - task :purge => [:environment, :load_config] do + task :purge => %w(db:test:deprecated environment load_config) do ActiveRecord::Tasks::DatabaseTasks.purge ActiveRecord::Base.configurations['test'] end # desc 'Check for pending migrations and load the test schema' - task :prepare => [:environment, :load_config] do + task :prepare => %w(db:test:deprecated environment load_config) do unless ActiveRecord::Base.configurations.blank? db_namespace['test:load'].invoke end @@ -385,5 +387,3 @@ namespace :railties do end end end - -task 'test:prepare' => ['db:test:prepare', 'db:test:load', 'db:abort_if_pending_migrations'] diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index acb7c65af5..afd493011e 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -36,9 +36,8 @@ module ActiveRecord module DatabaseTasks extend self - attr_writer :current_config - attr_accessor :database_configuration, :migrations_paths, :seed_loader, :db_dir, - :fixtures_path, :env, :root + attr_writer :current_config, :db_dir, :migrations_paths, :fixtures_path, :root, :env, :seed_loader + attr_accessor :database_configuration LOCAL_HOSTS = ['127.0.0.1', 'localhost'] @@ -51,6 +50,30 @@ module ActiveRecord register_task(/postgresql/, ActiveRecord::Tasks::PostgreSQLDatabaseTasks) register_task(/sqlite/, ActiveRecord::Tasks::SQLiteDatabaseTasks) + def db_dir + @db_dir ||= Rails.application.config.paths["db"].first + end + + def migrations_paths + @migrations_paths ||= Rails.application.paths['db/migrate'].to_a + end + + def fixtures_path + @fixtures_path ||= File.join(root, 'test', 'fixtures') + end + + def root + @root ||= Rails.root + end + + def env + @env ||= Rails.env + end + + def seed_loader + @seed_loader ||= Rails.application + end + def current_config(options = {}) options.reverse_merge! :env => env if options.has_key?(:config) @@ -133,6 +156,21 @@ module ActiveRecord class_for_adapter(configuration['adapter']).new(*arguments).structure_load(filename) end + def load_schema(format = ActiveRecord::Base.schema_format, file = nil) + case format + when :ruby + file ||= File.join(db_dir, "schema.rb") + check_schema_file(file) + load(file) + when :sql + file ||= File.join(db_dir, "structure.sql") + check_schema_file(file) + structure_load(current_config, file) + else + raise ArgumentError, "unknown format #{format.inspect}" + end + end + def check_schema_file(filename) unless File.exist?(filename) message = %{#{filename} doesn't exist yet. Run `rake db:migrate` to create it, then try again.} |