Skip to content

Commit

Permalink
Enable tests to run under Rails < 7.1
Browse files Browse the repository at this point in the history
You're likely familiar with how to set up and run the tests for
this project -- after confirming that inside of `bin/setup` on the
last line it shows the same path out to `mysql` as you see when
running `which mysql` then to test against edge Rails here are the
commands:
```
bin/setup
bundle exec rake
```
And once you've done that, which configures the test database
appropriately then thankfully it's pretty easy to re-bundle in
order to then run the tests under an older version:
```
rm Gemfile.lock
RAILS_VERSION="~> 6.1" bundle
RAILS_VERSION="~> 6.1" bundle exec rake
```
  • Loading branch information
lorint committed Dec 27, 2022
1 parent 5a554dc commit 84a8908
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions lib/trilogy_adapter/backwards_ar_compatibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,40 @@
# ActiveRecord 7.1. This works without requiring any code changes to the
# ActiveRecord Trilogy Adapter itself.

require "socket"
module ::ActiveRecord
# For ActiveRecord <= 5.2
unless const_defined?("QueryAborted")
class QueryAborted < StatementInvalid
end
end

# For ActiveRecord <= 7.0
unless const_defined?("ConnectionFailed")
class ConnectionFailed < QueryAborted
end
end

begin
require "active_record/database_configurations"
# For ActiveRecord 6.0
unless DatabaseConfigurations.instance_methods.include?(:resolve)
DatabaseConfigurations.class_exec do
def resolve(config) # :nodoc:
@resolver ||= ::ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(::ActiveRecord::Base.configurations)
@resolver.resolve(config)
end
end
end
rescue LoadError
# For ActiveRecord <= 5.2
class DatabaseConfigurations < ::ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver
def initialize(configurations = {}, *args)
super(::ActiveRecord::Base.configurations)
end
end
end

module ConnectionAdapters
unless AbstractAdapter.private_instance_methods.include?(:with_raw_connection)
AbstractAdapter.class_exec do
Expand Down Expand Up @@ -45,6 +78,8 @@ def default_timezone
TrilogyAdapter.class_exec do
# For ActiveRecord <= 7.0
def initialize(*args, **kwargs)
# Turn .new(config) into .new(nil, nil, nil, config)
3.times { args.unshift nil } if args.length < 4
super
@raw_connection = @connection if @connection
# Ensure that we're treating prepared_statements in the same way that Rails 7.1 does
Expand Down Expand Up @@ -96,12 +131,58 @@ def full_version
end
end

if const_defined?("PoolConfig")
# ActiveRecord <= 6.1
if PoolConfig.instance_method(:initialize).parameters.length < 4
class PoolConfig
alias _original_initialize initialize
def initialize(connection_class, db_config, *args)
_original_initialize(connection_class, db_config)
end
end
end
else
# For ActiveRecord <= 5.2
class PoolConfig < ConnectionSpecification
def initialize(connection_class, db_config, *args)
super("primary", db_config, nil)
end
end
end

# For ActiveRecord <= 5.2
unless SchemaCache.instance_methods.include?(:database_version)
SchemaCache.class_exec do
def database_version # :nodoc:
@database_version ||= connection.get_database_version
end

def self.load_from(filename)
return unless File.file?(filename)

read(filename) do |file|
if filename.include?(".dump")
Marshal.load(file)
else
if YAML.respond_to?(:unsafe_load)
YAML.unsafe_load(file)
else
YAML.load(file)
end
end
end
end

def self.read(filename, &block)
if File.extname(filename) == ".gz"
Zlib::GzipReader.open(filename) { |gz|
yield gz.read
}
else
yield File.read(filename)
end
end
private_class_method :read
end
end

Expand Down

0 comments on commit 84a8908

Please sign in to comment.