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

Don't use schema cache when checking schema migrations

I noticed this while debugging a separate issue locally. When
`lazily_load_schema_cache` is set to true AND there's a schema cache,
running `db:drop` twice will raise a `no such table: schema_migrations`
error. The problem here was that when `check_protected_environments` is
run before drop, the task checks for the latest environment. That check
looks at `table_exists?` defined in `model_schema.rb`. The issue is that
the model schema is checking `connection.schema_cache.data_source_exists?`.

When checking whether the environment is protected or schema migrations
we don't want to use the schema cache. We want to check the database
directly. The schema cache really is for the app after boot, so it's
safe to skip the schema cache in this area since we want to check the
database directly.
This commit is contained in:
eileencodes 2021-12-15 10:18:04 -05:00
parent 139183cbce
commit 920a58a3d6
No known key found for this signature in database
GPG key ID: BA5C575120BBE8DF
2 changed files with 26 additions and 0 deletions

View file

@ -41,6 +41,10 @@ module ActiveRecord
def all_versions def all_versions
order(:version).pluck(:version) order(:version).pluck(:version)
end end
def table_exists?
connection.data_source_exists?(table_name)
end
end end
def version def version

View file

@ -701,6 +701,28 @@ module ApplicationTests
assert_equal("Not touched", File.read("db/schema.rb").strip) assert_equal("Not touched", File.read("db/schema.rb").strip)
end end
end end
test "lazily loaded schema cache isn't read when reading the schema migrations table" do
Dir.chdir(app_path) do
app_file "config/initializers/lazy_load_schema_cache.rb", <<-RUBY
Rails.application.config.active_record.lazily_load_schema_cache = true
RUBY
rails "generate", "model", "recipe", "title:string"
rails "db:migrate"
rails "db:schema:cache:dump"
file = File.read("db/schema_cache.yml")
assert_match(/schema_migrations: true/, file)
assert_match(/recipes: true/, file)
output = rails "db:drop"
assert_match(/Dropped database/, output)
repeat_output = rails "db:drop"
assert_match(/Dropped database/, repeat_output)
end
end
end end
end end
end end