Skip to content

Commit cac604a

Browse files
committed
Remove eager loading of schema cache dump
This will be eager loaded by the define_attribute_methods initializer now that the schema cache can be automatically loaded for all connection if the file is present on disk after rails#48716.
1 parent 9362dd0 commit cac604a

File tree

2 files changed

+36
-77
lines changed

2 files changed

+36
-77
lines changed

activerecord/lib/active_record/railtie.rb

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -134,49 +134,11 @@ class Railtie < Rails::Railtie # :nodoc:
134134
end
135135
end
136136

137-
initializer "active_record.use_schema_cache_dump" do
138-
ActiveRecord::ConnectionAdapters::SchemaReflection.use_schema_cache_dump = config.active_record.use_schema_cache_dump
139-
end
140-
141-
initializer "active_record.check_schema_cache_dump" do
142-
check_schema_cache_dump_version = config.active_record.check_schema_cache_dump_version
143-
144-
ActiveRecord::ConnectionAdapters::SchemaReflection.check_schema_cache_dump_version = check_schema_cache_dump_version
137+
initializer "active_record.copy_schema_cache_config" do
138+
active_record_config = config.active_record
145139

146-
if config.active_record.use_schema_cache_dump && !config.active_record.lazily_load_schema_cache
147-
config.after_initialize do |app|
148-
ActiveSupport.on_load(:active_record) do
149-
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).first
150-
next if db_config.nil?
151-
152-
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config)
153-
154-
cache = ActiveRecord::ConnectionAdapters::SchemaCache._load_from(filename)
155-
next if cache.nil?
156-
157-
if check_schema_cache_dump_version
158-
current_version = begin
159-
ActiveRecord::Migrator.current_version
160-
rescue ActiveRecordError => error
161-
warn "Failed to validate the schema cache because of #{error.class}: #{error.message}"
162-
nil
163-
end
164-
165-
if current_version.nil?
166-
connection_pool.schema_reflection.clear!
167-
next
168-
elsif cache.schema_version != current_version
169-
warn "Ignoring #{filename} because it has expired. The current schema version is #{current_version}, but the one in the schema cache file is #{cache.schema_version}."
170-
connection_pool.schema_reflection.clear!
171-
next
172-
end
173-
end
174-
175-
Rails.logger.info("Using schema cache file #{filename}")
176-
connection_pool.schema_reflection.set_schema_cache(cache)
177-
end
178-
end
179-
end
140+
ActiveRecord::ConnectionAdapters::SchemaReflection.use_schema_cache_dump = active_record_config.use_schema_cache_dump
141+
ActiveRecord::ConnectionAdapters::SchemaReflection.check_schema_cache_dump_version = active_record_config.check_schema_cache_dump_version
180142
end
181143

182144
initializer "active_record.define_attribute_methods" do |app|

railties/test/application/initializers/frameworks_test.rb

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -232,30 +232,15 @@ def show
232232
config.eager_load = true
233233
RUBY
234234

235-
app("development")
235+
Dir.chdir(app_path) do
236+
app("development")
236237

237-
assert ActiveRecord::Base.connection.schema_cache.data_sources("posts")
238+
assert ActiveRecord::Base.connection.schema_cache.data_sources("posts")
239+
end
238240
ensure
239241
ActiveRecord::Base.connection.drop_table("posts", if_exists: true) # force drop posts table for test.
240242
end
241243

242-
test "skips checking for schema cache dump when all databases skipping database tasks" do
243-
app_file "config/database.yml", <<-YAML
244-
development:
245-
database: storage/default.sqlite3
246-
adapter: sqlite3
247-
database_tasks: false
248-
YAML
249-
250-
add_to_config <<-RUBY
251-
config.eager_load = true
252-
RUBY
253-
254-
assert_nothing_raised do
255-
app("development")
256-
end
257-
end
258-
259244
test "expire schema cache dump" do
260245
rails %w(generate model post title:string)
261246
rails %w(db:migrate db:schema:cache:dump db:rollback)
@@ -264,10 +249,15 @@ def show
264249
config.eager_load = true
265250
RUBY
266251

267-
silence_warnings do
252+
Dir.chdir(app_path) do
268253
app("development")
254+
255+
_, error = capture_io do
256+
assert_not ActiveRecord::Base.connection.schema_cache.data_sources("posts")
257+
end
258+
259+
assert_match(/Ignoring db\/schema_cache\.yml because it has expired/, error)
269260
end
270-
assert_not ActiveRecord::Base.connection.schema_cache.data_sources("posts")
271261
end
272262

273263
test "expire schema cache dump if the version can't be checked because the database is unhealthy" do
@@ -279,18 +269,20 @@ def show
279269
RUBY
280270

281271
with_unhealthy_database do
282-
silence_warnings do
272+
Dir.chdir(app_path) do
283273
app("development")
284-
end
285274

286-
assert_not_nil ActiveRecord::Base.connection_pool.schema_reflection.instance_variable_get(:@cache)
275+
assert_raises ActiveRecord::ConnectionNotEstablished do
276+
ActiveRecord::Base.connection.execute("SELECT 1")
277+
end
287278

288-
assert_raises ActiveRecord::ConnectionNotEstablished do
289-
ActiveRecord::Base.connection.execute("SELECT 1")
290-
end
279+
_, error = capture_io do
280+
assert_raises ActiveRecord::ConnectionNotEstablished do
281+
ActiveRecord::Base.connection.schema_cache.columns("posts")
282+
end
283+
end
291284

292-
assert_raises ActiveRecord::ConnectionNotEstablished do
293-
ActiveRecord::Base.connection.schema_cache.columns("posts")
285+
assert_match(/Failed to validate the schema cache because of ActiveRecord::ConnectionNotEstablished/, error)
294286
end
295287
end
296288
end
@@ -304,8 +296,11 @@ def show
304296
config.active_record.check_schema_cache_dump_version = false
305297
RUBY
306298

307-
app("development")
308-
assert ActiveRecord::Base.connection_pool.schema_reflection.data_sources(:__unused__, "posts")
299+
Dir.chdir(app_path) do
300+
app("development")
301+
302+
assert ActiveRecord::Base.connection_pool.schema_reflection.data_sources(:__unused__, "posts")
303+
end
309304
end
310305

311306
test "does not expire schema cache dump if check_schema_cache_dump_version is false and the database unhealthy" do
@@ -318,11 +313,13 @@ def show
318313
RUBY
319314

320315
with_unhealthy_database do
321-
app("development")
316+
Dir.chdir(app_path) do
317+
app("development")
322318

323-
assert ActiveRecord::Base.connection_pool.schema_reflection.data_sources(:__unused__, "posts")
324-
assert_raises ActiveRecord::ConnectionNotEstablished do
325-
ActiveRecord::Base.connection.execute("SELECT 1")
319+
assert ActiveRecord::Base.connection_pool.schema_reflection.data_sources(:__unused__, "posts")
320+
assert_raises ActiveRecord::ConnectionNotEstablished do
321+
ActiveRecord::Base.connection.execute("SELECT 1")
322+
end
326323
end
327324
end
328325
end

0 commit comments

Comments
 (0)