Skip to content

Commit 7cea354

Browse files
yahondaclaude
andcommitted
Prefer ActiveRecord.default_timezone over per-class accessor (rsim#234)
Calling `ar_class.default_timezone` on Rails 7.0.x prints DEPRECATION WARNING: ActiveRecord::Base.default_timezone is deprecated and will be removed in Rails 7.1. Use ActiveRecord.default_timezone instead. even though both accessors return the same value. The per-class accessor was introduced as a deprecated proxy in Rails 7.0 (rails/rails@c3f43075a3) and removed entirely in Rails 7.1 (rails/rails@96c9db1b48), so the warning only fires on Rails 7.0.x. Switch the order in PLSQL::Schema#default_timezone so that we ask the module-level ActiveRecord.default_timezone first when it is available (AR 7.0+) and only fall back to the per-class accessor on pre-7.0 AR where the module-level one does not exist. This silences the warning on Rails 7.0.x and is a no-op on Rails 7.1+ (where the per-class accessor was already gone) and on Rails <= 6.x (where the module-level accessor did not yet exist). Add a regression test that captures deprecator output while resolving plsql.default_timezone with `activerecord_class` set, and asserts that no warning mentioning default_timezone was raised. The existing ActiveRecord.default_timezone is captured before assignment and restored from the ensure block alongside the deprecator behavior so the test is order-independent. Verified by rsim#269 / rsim#271 that the test fails on AR 7.0 without this fix and passes on AR 7.1+ regardless. Fixes rsim#234 References: - rails/rails@c3f43075a3 (added the deprecated delegators in Rails 7.0) - rails/rails@96c9db1b48 (removed them in Rails 7.1) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e39e8e4 commit 7cea354

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

lib/plsql/schema.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ def default_timezone
9999
@original_schema.default_timezone
100100
else
101101
@default_timezone ||
102-
# Use ActiveRecord default_timezone when ActiveRecord connection is used,
103-
# preferring the connection's activerecord_class so a subclass override
104-
# (available in AR < 8.0) is honored before falling back to the
105-
# module-level accessor (AR 7.0+; the only one in AR 8.0+).
106-
(@connection && (ar_class = @connection.activerecord_class) &&
107-
(ar_class.respond_to?(:default_timezone) ? ar_class.default_timezone : ActiveRecord.default_timezone)) ||
102+
# Use ActiveRecord default_timezone when ActiveRecord connection is used.
103+
# Prefer the module-level accessor (AR 7.0+; the only one in AR 8.0+) so
104+
# that AR 7.0/7.1's deprecation warning for ActiveRecord::Base.default_timezone
105+
# is not emitted. Fall back to the per-class accessor only on pre-7.0 AR,
106+
# where ActiveRecord.default_timezone does not exist.
107+
(@connection && @connection.activerecord_class &&
108+
(ActiveRecord.respond_to?(:default_timezone) ?
109+
ActiveRecord.default_timezone :
110+
@connection.activerecord_class.default_timezone)) ||
108111
# default to local timezone
109112
:local
110113
end

spec/plsql/schema_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,26 @@ class TestModel < TestBaseModel
218218
expect(plsql.default_timezone).to eq(:utc)
219219
end
220220

221+
it "should not emit ActiveRecord::Base.default_timezone deprecation warning (#234)" do
222+
skip "ActiveRecord.default_timezone is not available" unless ActiveRecord.respond_to?(:default_timezone=)
223+
224+
original_default_timezone = ActiveRecord.default_timezone
225+
ActiveRecord.default_timezone = :utc
226+
227+
deprecator = ActiveRecord.respond_to?(:deprecator) ? ActiveRecord.deprecator : ActiveSupport::Deprecation
228+
original_behavior = deprecator.behavior
229+
warnings = []
230+
begin
231+
deprecator.behavior = ->(message, *) { warnings << message }
232+
expect(plsql.default_timezone).to eq(:utc)
233+
ensure
234+
deprecator.behavior = original_behavior
235+
ActiveRecord.default_timezone = original_default_timezone
236+
end
237+
238+
expect(warnings.grep(/default_timezone/)).to be_empty
239+
end
240+
221241
it "should have the same connection as default schema" do
222242
expect(plsql.hr.connection).to eq(plsql.connection)
223243
end

0 commit comments

Comments
 (0)