diff --git a/lib/plsql/schema.rb b/lib/plsql/schema.rb index 165811a..2062bef 100644 --- a/lib/plsql/schema.rb +++ b/lib/plsql/schema.rb @@ -99,12 +99,15 @@ def default_timezone @original_schema.default_timezone else @default_timezone || - # Use ActiveRecord default_timezone when ActiveRecord connection is used, - # preferring the connection's activerecord_class so a subclass override - # (available in AR < 8.0) is honored before falling back to the - # module-level accessor (AR 7.0+; the only one in AR 8.0+). - (@connection && (ar_class = @connection.activerecord_class) && - (ar_class.respond_to?(:default_timezone) ? ar_class.default_timezone : ActiveRecord.default_timezone)) || + # Use ActiveRecord default_timezone when ActiveRecord connection is used. + # Prefer the module-level accessor (AR 7.0+; the only one in AR 8.0+) so + # that AR 7.0/7.1's deprecation warning for ActiveRecord::Base.default_timezone + # is not emitted. Fall back to the per-class accessor only on pre-7.0 AR, + # where ActiveRecord.default_timezone does not exist. + (@connection && @connection.activerecord_class && + (ActiveRecord.respond_to?(:default_timezone) ? + ActiveRecord.default_timezone : + @connection.activerecord_class.default_timezone)) || # default to local timezone :local end diff --git a/spec/plsql/schema_spec.rb b/spec/plsql/schema_spec.rb index 0662882..d540e3e 100644 --- a/spec/plsql/schema_spec.rb +++ b/spec/plsql/schema_spec.rb @@ -218,6 +218,26 @@ class TestModel < TestBaseModel expect(plsql.default_timezone).to eq(:utc) end + it "should not emit ActiveRecord::Base.default_timezone deprecation warning (#234)" do + skip "ActiveRecord.default_timezone is not available" unless ActiveRecord.respond_to?(:default_timezone=) + + original_default_timezone = ActiveRecord.default_timezone + ActiveRecord.default_timezone = :utc + + deprecator = ActiveRecord.respond_to?(:deprecator) ? ActiveRecord.deprecator : ActiveSupport::Deprecation + original_behavior = deprecator.behavior + warnings = [] + begin + deprecator.behavior = ->(message, *) { warnings << message } + expect(plsql.default_timezone).to eq(:utc) + ensure + deprecator.behavior = original_behavior + ActiveRecord.default_timezone = original_default_timezone + end + + expect(warnings.grep(/default_timezone/)).to be_empty + end + it "should have the same connection as default schema" do expect(plsql.hr.connection).to eq(plsql.connection) end