diff options
author | kennyj <kennyj@gmail.com> | 2012-03-01 01:13:14 +0900 |
---|---|---|
committer | kennyj <kennyj@gmail.com> | 2012-03-01 01:13:14 +0900 |
commit | 0da12df2608969330bd47c543866f79ff8ca9edd (patch) | |
tree | 7299cbc132879c3af3490d461b1d6c405abed474 | |
parent | 0150a212c068c6e4a580218913f44f278e086bd3 (diff) | |
download | rails-0da12df2608969330bd47c543866f79ff8ca9edd.tar.gz rails-0da12df2608969330bd47c543866f79ff8ca9edd.tar.bz2 rails-0da12df2608969330bd47c543866f79ff8ca9edd.zip |
Load db/schema_cache.dump duaring boot time.
5 files changed, 32 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index c6af682d28..3cc714040d 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -86,6 +86,11 @@ module ActiveRecord end end + def schema_cache=(cache) + cache.connection = self + @schema_cache = cache + end + def expire @in_use = false end diff --git a/activerecord/lib/active_record/connection_adapters/schema_cache.rb b/activerecord/lib/active_record/connection_adapters/schema_cache.rb index cd8c5844a4..53fa404305 100644 --- a/activerecord/lib/active_record/connection_adapters/schema_cache.rb +++ b/activerecord/lib/active_record/connection_adapters/schema_cache.rb @@ -2,7 +2,7 @@ module ActiveRecord module ConnectionAdapters class SchemaCache attr_reader :columns, :columns_hash, :primary_keys, :tables - attr_reader :connection + attr_accessor :connection def initialize(conn) @connection = conn diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 058dd58efb..c287356a7b 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -107,7 +107,7 @@ module ActiveRecord config.watchable_files.concat ["#{app.root}/db/schema.rb", "#{app.root}/db/structure.sql"] end - config.after_initialize do + config.after_initialize do |app| ActiveSupport.on_load(:active_record) do ActiveRecord::Base.instantiate_observers @@ -115,6 +115,17 @@ module ActiveRecord ActiveRecord::Base.instantiate_observers end end + + ActiveSupport.on_load(:active_record) do + if app.config.use_schema_cache_dump + filename = File.join(app.config.paths["db"].first, "schema_cache.dump") + if File.file?(filename) + cache = Marshal.load(open(filename, 'rb') { |f| f.read }) + ActiveRecord::Base.connection.schema_cache = cache + end + end + end + end end end diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 1e424d9b4a..0f5fc2b7bc 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -11,7 +11,7 @@ module Rails :force_ssl, :helpers_paths, :logger, :log_tags, :preload_frameworks, :railties_order, :relative_url_root, :secret_token, :serve_static_assets, :ssl_options, :static_cache_control, :session_options, - :time_zone, :reload_classes_only_on_change + :time_zone, :reload_classes_only_on_change, :use_schema_cache_dump attr_writer :log_level attr_reader :encoding @@ -41,6 +41,7 @@ module Rails @file_watcher = ActiveSupport::FileUpdateChecker @exceptions_app = nil @autoflush_log = true + @use_schema_cache_dump = true @assets = ActiveSupport::OrderedOptions.new @assets.enabled = false diff --git a/railties/test/application/initializers/frameworks_test.rb b/railties/test/application/initializers/frameworks_test.rb index 8812685620..d8012eed64 100644 --- a/railties/test/application/initializers/frameworks_test.rb +++ b/railties/test/application/initializers/frameworks_test.rb @@ -193,5 +193,17 @@ module ApplicationTests require "#{app_path}/config/environment" assert_nil defined?(ActiveRecord::Base) end + + test "use schema cache dump" do + Dir.chdir(app_path) do + `rails generate model post title:string` + `bundle exec rake db:migrate` + `bundle exec rake db:schema:cache:dump` + end + require "#{app_path}/config/environment" + ActiveRecord::Base.connection.drop_table("posts") # force drop posts table for test. + assert ActiveRecord::Base.connection.schema_cache.tables["posts"] + end + end end |