diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index 975acb6a2d..c5aa0008f8 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -361,6 +361,82 @@ module ApplicationTests db_schema_dump end + def db_schema_cache_dump(filename = "db/schema_cache.yml") + Dir.chdir(app_path) do + rails "db:schema:cache:dump" + + cache_size = lambda { rails("runner", "p ActiveRecord::Base.connection.schema_cache.size").strip } + cache_tables = lambda { rails("runner", "p ActiveRecord::Base.connection.schema_cache.columns('books')").strip } + + assert_equal "12", cache_size[] + assert_includes cache_tables[], "id", "expected cache_tables to include an id entry" + assert_includes cache_tables[], "title", "expected cache_tables to include a title entry" + end + end + + test "db:schema:cache:dump" do + db_schema_dump + db_schema_cache_dump + end + + test "db:schema:cache:dump with custom filename" do + Dir.chdir(app_path) do + File.open("#{app_path}/config/database.yml", "w") do |f| + f.puts <<-YAML + default: &default + adapter: sqlite3 + pool: 5 + timeout: 5000 + variables: + statement_timeout: 1000 + development: + <<: *default + database: db/development.sqlite3 + schema_cache_path: db/special_schema_cache.yml + YAML + end + end + + db_schema_dump + db_schema_cache_dump("db/special_schema_cache.yml") + end + + test "db:schema:cache:dump custom env" do + @old_schema_cache_env = ENV["SCHEMA_CACHE"] + filename = "db/special_schema_cache.yml" + ENV["SCHEMA_CACHE"] = filename + + db_schema_dump + db_schema_cache_dump(filename) + ensure + ENV["SCHEMA_CACHE"] = @old_schema_cache_env + end + + test "db:schema:cache:dump primary wins" do + Dir.chdir(app_path) do + File.open("#{app_path}/config/database.yml", "w") do |f| + f.puts <<-YAML + default: &default + adapter: sqlite3 + pool: 5 + timeout: 5000 + variables: + statement_timeout: 1000 + development: + some_entry: + <<: *default + database: db/development_other.sqlite3 + primary: + <<: *default + database: db/development.sqlite3 + YAML + end + end + + db_schema_dump + db_schema_cache_dump + end + def db_fixtures_load(expected_database) Dir.chdir(app_path) do rails "generate", "model", "book", "title:string" diff --git a/railties/test/application/rake/multi_dbs_test.rb b/railties/test/application/rake/multi_dbs_test.rb index 8e1a214acd..e841fdd1e6 100644 --- a/railties/test/application/rake/multi_dbs_test.rb +++ b/railties/test/application/rake/multi_dbs_test.rb @@ -311,6 +311,25 @@ module ApplicationTests db_migrate_and_schema_cache_dump end + # Note that schema cache loader depends on the connection and + # does not work for all connections. + test "schema_cache is loaded on primary db in multi-db app" do + require "#{app_path}/config/environment" + db_migrate_and_schema_cache_dump + + cache_size_a = lambda { rails("runner", "p ActiveRecord::Base.connection.schema_cache.size").strip } + cache_tables_a = lambda { rails("runner", "p ActiveRecord::Base.connection.schema_cache.columns('books')").strip } + cache_size_b = lambda { rails("runner", "p AnimalsBase.connection.schema_cache.size").strip } + cache_tables_b = lambda { rails("runner", "p AnimalsBase.connection.schema_cache.columns('dogs')").strip } + + assert_equal "12", cache_size_a[] + assert_includes cache_tables_a[], "title", "expected cache_tables_a to include a title entry" + + # Will be 0 because it's not loaded by the railtie + assert_equal "0", cache_size_b[] + assert_includes cache_tables_b[], "name", "expected cache_tables_b to include a name entry" + end + test "db:schema:cache:clear works on all databases" do require "#{app_path}/config/environment" db_migrate_and_schema_cache_dump_and_schema_cache_clear