Skip to content

Commit

Permalink
Merge branch 'main' into webp-image-content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
lewispb authored Feb 7, 2024
2 parents 088bb80 + f0d433b commit dd3a997
Show file tree
Hide file tree
Showing 31 changed files with 238 additions and 357 deletions.
4 changes: 1 addition & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
source "https://rubygems.org"
gemspec

gem "minitest", ">= 5.15.0"
gem "minitest", ">= 5.15.0", "< 5.22.0"

# We need a newish Rake since Active Job sets its test tasks' descriptions.
gem "rake", ">= 13"
Expand Down Expand Up @@ -143,8 +143,6 @@ group :test do
gem "debug", ">= 1.1.0", require: false
end

gem "benchmark-ips"

# Needed for Railties tests because it is included in generated apps.
gem "brakeman"
end
Expand Down
6 changes: 2 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ PATH
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
minitest (>= 5.1, < 5.22.0)
tzinfo (~> 2.0, >= 2.0.5)
rails (7.2.0.alpha)
actioncable (= 7.2.0.alpha)
Expand Down Expand Up @@ -159,7 +159,6 @@ GEM
base64 (0.2.0)
bcrypt (3.1.20)
beaneater (1.1.3)
benchmark-ips (2.13.0)
bigdecimal (3.1.5)
bindex (0.8.1)
bootsnap (1.17.0)
Expand Down Expand Up @@ -596,7 +595,6 @@ DEPENDENCIES
azure-storage-blob (~> 2.0)
backburner
bcrypt (~> 3.1.11)
benchmark-ips
bootsnap (>= 1.4.4)
brakeman
capybara (>= 3.39)
Expand All @@ -618,7 +616,7 @@ DEPENDENCIES
libxml-ruby
listen (~> 3.3)
mdl (!= 0.13.0)
minitest (>= 5.15.0)
minitest (>= 5.15.0, < 5.22.0)
minitest-bisect
minitest-ci
minitest-retry
Expand Down
1 change: 0 additions & 1 deletion actionpack/lib/action_dispatch/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class Railtie < Rails::Railtie # :nodoc:
ActionDispatch::Cookies::CookieJar.always_write_cookie = config.action_dispatch.always_write_cookie

ActionDispatch::Routing::Mapper.route_source_locations = Rails.env.development?
ActionDispatch::Routing::Mapper.backtrace_cleaner = Rails.backtrace_cleaner

ActionDispatch.test_app = app
end
Expand Down
29 changes: 18 additions & 11 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@
module ActionDispatch
module Routing
class Mapper
class BacktraceCleaner < ActiveSupport::BacktraceCleaner # :nodoc:
def initialize
super
remove_silencers!
add_core_silencer
add_stdlib_silencer
end
end

URL_OPTIONS = [:protocol, :subdomain, :domain, :host, :port]

cattr_accessor :route_source_locations, instance_accessor: false, default: false
cattr_accessor :backtrace_cleaner, instance_accessor: false, default: ActiveSupport::BacktraceCleaner.new
cattr_accessor :backtrace_cleaner, instance_accessor: false, default: BacktraceCleaner.new

class Constraints < Routing::Endpoint # :nodoc:
attr_reader :app, :constraints
Expand Down Expand Up @@ -366,11 +375,10 @@ def route_source_location
Thread.each_caller_location do |location|
next if location.path.start_with?(action_dispatch_dir)

if cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
return "#{cleaned_path}:#{location.lineno}"
else
return nil
end
cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
next if cleaned_path.nil?

return "#{cleaned_path}:#{location.lineno}"
end
nil
end
Expand All @@ -382,11 +390,10 @@ def route_source_location
caller_locations.each do |location|
next if location.path.start_with?(action_dispatch_dir)

if cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
return "#{cleaned_path}:#{location.lineno}"
else
return nil
end
cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
next if cleaned_path.nil?

return "#{cleaned_path}:#{location.lineno}"
end
nil
end
Expand Down
12 changes: 12 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
* Support `:source_location` tag option for query log tags

```ruby
config.active_record.query_log_tags << :source_location
```

Calculating the caller location is a costly operation and should be used primarily in development
(note, there is also a `config.active_record.verbose_query_logs` that serves the same purpose)
or occasionally on production for debugging purposes.

*fatkodima*

* Add an option to `ActiveRecord::Encryption::Encryptor` to disable compression

Allow compression to be disabled by setting `compress: false`
Expand Down
15 changes: 15 additions & 0 deletions activerecord/lib/active_record/query_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module ActiveRecord
# * +socket+
# * +db_host+
# * +database+
# * +source_location+
#
# Action Controller adds default tags when loaded:
#
Expand Down Expand Up @@ -108,6 +109,20 @@ def update_formatter(format)
end
end

if Thread.respond_to?(:each_caller_location)
def query_source_location # :nodoc:
Thread.each_caller_location do |location|
frame = LogSubscriber.backtrace_cleaner.clean_frame(location.path)
return frame if frame
end
nil
end
else
def query_source_location # :nodoc:
LogSubscriber.backtrace_cleaner.clean(caller_locations(1).each).first
end
end

ActiveSupport::ExecutionContext.after_change { ActiveRecord::QueryLogs.clear_cache }

private
Expand Down
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ class Railtie < Rails::Railtie # :nodoc:
pid: -> { Process.pid.to_s },
socket: ->(context) { context[:connection].pool.db_config.socket },
db_host: ->(context) { context[:connection].pool.db_config.host },
database: ->(context) { context[:connection].pool.db_config.database }
database: ->(context) { context[:connection].pool.db_config.database },
source_location: -> { QueryLogs.query_source_location }
)
ActiveRecord.disable_prepared_statements = true

Expand Down
34 changes: 19 additions & 15 deletions activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,27 +224,31 @@ def test_overriding_default_journal_mode_pragma
assert_match(/nrecognized journal_mode false/, error.message)
else
# must use a new, separate database file that hasn't been opened in WAL mode before
with_file_connection(database: "fixtures/journal_mode_test.sqlite3", pragmas: { "journal_mode" => "delete" }) do |conn|
assert_equal [{ "journal_mode" => "delete" }], conn.execute("PRAGMA journal_mode")
end
Dir.mktmpdir do |tmpdir|
database_file = File.join(tmpdir, "journal_mode_test.sqlite3")

with_file_connection(database: "fixtures/journal_mode_test.sqlite3", pragmas: { "journal_mode" => :delete }) do |conn|
assert_equal [{ "journal_mode" => "delete" }], conn.execute("PRAGMA journal_mode")
end
with_file_connection(database: database_file, pragmas: { "journal_mode" => "delete" }) do |conn|
assert_equal [{ "journal_mode" => "delete" }], conn.execute("PRAGMA journal_mode")
end

error = assert_raises(ActiveRecord::StatementInvalid) do
with_file_connection(database: "fixtures/journal_mode_test.sqlite3", pragmas: { "journal_mode" => 0 }) do |conn|
conn.execute("PRAGMA journal_mode")
with_file_connection(database: database_file, pragmas: { "journal_mode" => :delete }) do |conn|
assert_equal [{ "journal_mode" => "delete" }], conn.execute("PRAGMA journal_mode")
end
end
assert_match(/unrecognized journal_mode 0/, error.message)

error = assert_raises(ActiveRecord::StatementInvalid) do
with_file_connection(database: "fixtures/journal_mode_test.sqlite3", pragmas: { "journal_mode" => false }) do |conn|
conn.execute("PRAGMA journal_mode")
error = assert_raises(ActiveRecord::StatementInvalid) do
with_file_connection(database: database_file, pragmas: { "journal_mode" => 0 }) do |conn|
conn.execute("PRAGMA journal_mode")
end
end
assert_match(/unrecognized journal_mode 0/, error.message)

error = assert_raises(ActiveRecord::StatementInvalid) do
with_file_connection(database: database_file, pragmas: { "journal_mode" => false }) do |conn|
conn.execute("PRAGMA journal_mode")
end
end
assert_match(/unrecognized journal_mode false/, error.message)
end
assert_match(/unrecognized journal_mode false/, error.message)
end
end

Expand Down
18 changes: 13 additions & 5 deletions activerecord/test/cases/connection_pool_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ def setup
@previous_isolation_level = ActiveSupport::IsolatedExecutionState.isolation_level

# Keep a duplicate pool so we do not bother others
@db_config = ActiveRecord::Base.connection_pool.db_config
config = ActiveRecord::Base.connection_pool.db_config
@db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(
config.env_name,
config.name,
config.configuration_hash.merge(
checkout_timeout: 0.2, # Reduce checkout_timeout to speedup tests
)
)

@pool_config = ActiveRecord::ConnectionAdapters::PoolConfig.new(ActiveRecord::Base, @db_config, :writing, :default)
@pool = ConnectionPool.new(@pool_config)

Expand Down Expand Up @@ -436,7 +444,7 @@ def test_checkout_fairness
end

# this should wake up the waiting threads one by one in order
conns.each { |conn| @pool.checkin(conn); sleep 0.1 }
conns.each { |conn| @pool.checkin(conn); sleep 0.01 }

dispose_held_connections.set
threads.each(&:join)
Expand Down Expand Up @@ -768,11 +776,11 @@ def test_connection_pool_stat
with_single_connection_pool do |pool|
pool.with_connection do |connection|
stats = pool.stat
assert_equal({ size: 1, connections: 1, busy: 1, dead: 0, idle: 0, waiting: 0, checkout_timeout: 5 }, stats)
assert_equal({ size: 1, connections: 1, busy: 1, dead: 0, idle: 0, waiting: 0, checkout_timeout: 0.2 }, stats)
end

stats = pool.stat
assert_equal({ size: 1, connections: 1, busy: 0, dead: 0, idle: 1, waiting: 0, checkout_timeout: 5 }, stats)
assert_equal({ size: 1, connections: 1, busy: 0, dead: 0, idle: 1, waiting: 0, checkout_timeout: 0.2 }, stats)

assert_raise(ThreadError) do
new_thread do
Expand All @@ -782,7 +790,7 @@ def test_connection_pool_stat
end

stats = pool.stat
assert_equal({ size: 1, connections: 1, busy: 0, dead: 1, idle: 0, waiting: 0, checkout_timeout: 5 }, stats)
assert_equal({ size: 1, connections: 1, busy: 0, dead: 1, idle: 0, waiting: 0, checkout_timeout: 0.2 }, stats)
ensure
Thread.report_on_exception = original_report_on_exception
end
Expand Down
42 changes: 1 addition & 41 deletions activerecord/test/cases/encryption/helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "cases/helper"
require "benchmark/ips"

class ActiveRecord::Fixture
prepend ActiveRecord::Encryption::EncryptedFixtures
Expand Down Expand Up @@ -111,45 +110,6 @@ def assert_invalid_key_cant_read_attribute_with_custom_key_provider(model, attri
attribute_type.key_provider.keys = original_keys
end
end

module PerformanceHelpers
BENCHMARK_DURATION = 1
BENCHMARK_WARMUP = 1
BASELINE_LABEL = "Baseline"
CODE_TO_TEST_LABEL = "Code"

# Usage:
#
# baseline = -> { <some baseline code> }
#
# assert_slower_by_at_most 2, baseline: baseline do
# <the code you want to compare against the baseline>
# end
def assert_slower_by_at_most(threshold_factor, baseline:, baseline_label: BASELINE_LABEL, code_to_test_label: CODE_TO_TEST_LABEL, duration: BENCHMARK_DURATION, quiet: true, &block_to_test)
GC.start

result = nil
output, error = capture_io do
result = Benchmark.ips do |x|
x.config(time: duration, warmup: BENCHMARK_WARMUP)
x.report(code_to_test_label, &block_to_test)
x.report(baseline_label, &baseline)
x.compare!
end
end

baseline_result = result.entries.find { |entry| entry.label == baseline_label }
code_to_test_result = result.entries.find { |entry| entry.label == code_to_test_label }

times_slower = baseline_result.ips / code_to_test_result.ips

if !quiet || times_slower >= threshold_factor
puts "#{output}#{error}"
end

assert times_slower < threshold_factor, "Expecting #{threshold_factor} times slower at most, but got #{times_slower} times slower"
end
end
end

# We eager load encrypted attribute types as they are declared, so that they pick up the
Expand All @@ -163,7 +123,7 @@ def assert_slower_by_at_most(threshold_factor, baseline:, baseline_label: BASELI
end

class ActiveRecord::EncryptionTestCase < ActiveRecord::TestCase
include ActiveRecord::Encryption::EncryptionHelpers, ActiveRecord::Encryption::PerformanceHelpers
include ActiveRecord::Encryption::EncryptionHelpers

ENCRYPTION_PROPERTIES_TO_RESET = {
config: %i[ primary_key deterministic_key key_derivation_salt store_key_references hash_digest_class
Expand Down

This file was deleted.

Loading

0 comments on commit dd3a997

Please sign in to comment.