From 283a08763495a6b3ce0b196259ee1666f2b08cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 15 Dec 2011 18:48:10 +0100 Subject: Clean up the cache before the request in case we are running in the reload_classes_only_on_change schema. --- activerecord/lib/active_record/railtie.rb | 18 +++++--- railties/lib/rails/application/finisher.rb | 2 +- railties/test/application/console_test.rb | 2 - railties/test/application/loading_test.rb | 54 +++++++++++++++++++++-- railties/test/application/rake/migrations_test.rb | 2 +- 5 files changed, 66 insertions(+), 12 deletions(-) diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index b28e8da24c..08cf9fb504 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -85,11 +85,19 @@ module ActiveRecord end end - initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app| - ActiveSupport.on_load(:active_record) do - ActionDispatch::Reloader.to_cleanup do - ActiveRecord::Base.clear_reloadable_connections! - ActiveRecord::Base.clear_cache! + initializer "active_record.set_reloader_hooks" do |app| + hook = lambda do + ActiveRecord::Base.clear_reloadable_connections! + ActiveRecord::Base.clear_cache! + end + + if app.config.reload_classes_only_on_change + ActiveSupport.on_load(:active_record) do + ActionDispatch::Reloader.to_prepare(&hook) + end + else + ActiveSupport.on_load(:active_record) do + ActionDispatch::Reloader.to_cleanup(&hook) end end end diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 2ce2980b97..b9944bed26 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -71,7 +71,7 @@ module Rails # Set app reload just after the finisher hook to ensure # paths added in the hook are still loaded. - initializer :set_dependencies_hook, :group => :all do + initializer :set_clear_dependencies_hook, :group => :all do callback = lambda do ActiveSupport::DescendantsTracker.clear ActiveSupport::Dependencies.clear diff --git a/railties/test/application/console_test.rb b/railties/test/application/console_test.rb index 6f9d8d57b1..fa2652a6d3 100644 --- a/railties/test/application/console_test.rb +++ b/railties/test/application/console_test.rb @@ -62,8 +62,6 @@ class ConsoleTest < Test::Unit::TestCase load_environment assert User.new.respond_to?(:name) - sleep(1) - app_file "app/models/user.rb", <<-MODEL class User attr_accessor :name, :age diff --git a/railties/test/application/loading_test.rb b/railties/test/application/loading_test.rb index 9c77f6210a..c4c93cce22 100644 --- a/railties/test/application/loading_test.rb +++ b/railties/test/application/loading_test.rb @@ -120,7 +120,6 @@ class LoadingTest < Test::Unit::TestCase extend Rack::Test::Methods require "#{rails_root}/config/environment" - sleep(1) get "/c" assert_equal "1", last_response.body @@ -160,7 +159,6 @@ class LoadingTest < Test::Unit::TestCase extend Rack::Test::Methods require "#{rails_root}/config/environment" - sleep(1) get "/c" assert_equal "1", last_response.body @@ -175,7 +173,7 @@ class LoadingTest < Test::Unit::TestCase assert_equal "1", last_response.body end - test "added files also trigger reloading" do + test "added files (like db/schema.rb) also trigger reloading" do add_to_config <<-RUBY config.cache_classes = false RUBY @@ -207,6 +205,56 @@ class LoadingTest < Test::Unit::TestCase assert_equal "2", last_response.body end + test "columns migrations also trigger reloading" do + add_to_config <<-RUBY + config.cache_classes = false + RUBY + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + match '/title', :to => lambda { |env| [200, {"Content-Type" => "text/plain"}, [Post.new.title]] } + match '/body', :to => lambda { |env| [200, {"Content-Type" => "text/plain"}, [Post.new.body]] } + end + RUBY + + app_file "app/models/post.rb", <<-MODEL + class Post < ActiveRecord::Base + end + MODEL + + require 'rack/test' + extend Rack::Test::Methods + + app_file "db/migrate/1_create_posts.rb", <<-MIGRATION + class CreatePosts < ActiveRecord::Migration + def change + create_table :posts do |t| + t.string :title, :default => "TITLE" + end + end + end + MIGRATION + + Dir.chdir(app_path) { `rake db:migrate`} + require "#{rails_root}/config/environment" + + get "/title" + assert_equal "TITLE", last_response.body + + app_file "db/migrate/2_add_body_to_posts.rb", <<-MIGRATION + class AddBodyToPosts < ActiveRecord::Migration + def change + add_column :posts, :body, :text, :default => "BODY" + end + end + MIGRATION + + Dir.chdir(app_path) { `rake db:migrate` } + + get "/body" + assert_equal "BODY", last_response.body + end + protected def setup_ar! diff --git a/railties/test/application/rake/migrations_test.rb b/railties/test/application/rake/migrations_test.rb index fd8a30557e..7982c42d8f 100644 --- a/railties/test/application/rake/migrations_test.rb +++ b/railties/test/application/rake/migrations_test.rb @@ -86,7 +86,7 @@ module ApplicationTests `rails generate migration add_email_to_users email:string` end - Dir.chdir(app_path) { `rake db:migrate`} + Dir.chdir(app_path) { `rake db:migrate` } output = Dir.chdir(app_path) { `rake db:migrate:status` } assert_match(/up\s+\d{14}\s+Create users/, output) -- cgit v1.2.3