1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #38444 from eileencodes/add-schema-cache-tests-and-fix-one-more-bug

Add schema cache tests
This commit is contained in:
Eileen M. Uchitelle 2020-02-13 09:32:59 -05:00 committed by GitHub
commit 7ac5e5e292
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 0 deletions

View file

@ -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"

View file

@ -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