mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Clean up the cache before the request in case we are running in the reload_classes_only_on_change schema.
This commit is contained in:
parent
de947c621d
commit
283a087634
5 changed files with 66 additions and 12 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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!
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue