mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add schema cache tests
The schema cache tests test the following scenarios: 1) The default case works (single db, primary spec name (dev is default to primary in 2-tier config), standard default schema cache filename) 2) Primary always wins over other entries 3) A custom schema cache filename works when set in the configuration 4) A custom schema cache filename works when set in the ENV Cases that don't work: 1) A non-primary database entry picks up a namespaced schema cache file This can't work currently because there's no way of knowing which cache we actually want. In this railtie we can only load ActiveRecord::Base's schema cache. If we grab the first config we risk loading a cache for another connection because order is not guaranteed. 2) Multi-db schema caches The reasons are similar to above. In addition we can't loop through the configs, establish a connection, and load the cache because we don't know what parent class to establish a connection to. In that case AR Base will always get the cache and it would cause the last one to win and therefore be loaded on the wrong connection. The real fix for these issues is to get rid of the railtie entirely, but for now we needed to set this back to what the behavior was before recent changes but with the ability to pass a custom key. Co-authored-by: Katrina Owen <kytrinyx@github.com>
This commit is contained in:
parent
a974898c5d
commit
c1a215a06b
2 changed files with 95 additions and 0 deletions
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue