Skip to content

Commit

Permalink
Merge pull request #26 from lorint/support_rails_7
Browse files Browse the repository at this point in the history
Support Rails 7.0
  • Loading branch information
bensheldon authored Jun 16, 2023
2 parents 592d98f + 2867e28 commit 7976c68
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 173 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
matrix:
rails_version:
- main
- "~>7.0.0"
ruby_version:
- '3.0'
- '3.1'
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.4
3.0.6
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Added

- Removed support for Rails 7.1

## 2.2.0

### Changed
Expand Down
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

source "https://rubygems.org"

if !ENV["RAILS_VERSION"] || ENV["RAILS_VERSION"] == "main"
gem "activerecord", git: "https://github.com/rails/rails", branch: "main"
if !ENV["RAILS_VERSION"]
gem "activerecord"
else
gem "activerecord", ENV["RAILS_VERSION"]
end

gem "trilogy", git: "https://github.com/github/trilogy", branch: "main", glob: "contrib/ruby/*.gemspec"
gem "trilogy"

gemspec
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

Active Record database adapter for [Trilogy](https://github.com/github/trilogy)

This gem offers Trilogy support for versions of ActiveRecord prior to 7.1. Currently supports:

- Rails v7.0.x

## Requirements

- [Ruby](https://www.ruby-lang.org) 2.7 or higher
- [Active Record](https://github.com/rails/rails) 7.1 or higher
- [Trilogy](https://github.com/github/trilogy) 2.1.1 or higher
- [Active Record](https://github.com/rails/rails) 7.0.x
- [Trilogy](https://github.com/github/trilogy) 2.4.0 or higher

## Setup

Expand Down
4 changes: 2 additions & 2 deletions activerecord-trilogy-adapter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
"bug_tracker_uri" => "https://github.com/github/activerecord-trilogy-adapter/issues"
}

spec.add_dependency "trilogy", ">= 2.3.0"
spec.add_dependency "activerecord", "~> 7.1.a"
spec.add_dependency "trilogy", ">= 2.4.0"
spec.add_dependency "activerecord", [">= 7.0", "< 7.1a"]
spec.add_development_dependency "minitest", "~> 5.11"
spec.add_development_dependency "minitest-focus", "~> 1.1"
spec.add_development_dependency "pry", "~> 0.10"
Expand Down
67 changes: 55 additions & 12 deletions lib/active_record/connection_adapters/trilogy_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ def translate_connect_error(config, error)
end
end

def initialize(connection, logger, connection_options, config)
super
# Ensure that we're treating prepared_statements in the same way that Rails 7.1 does
@prepared_statements = self.class.type_cast_config_to_boolean(
@config.fetch(:prepared_statements) { default_prepared_statements }
)
end

def supports_json?
!mariadb? && database_version >= "5.7.8"
end
Expand All @@ -135,12 +143,53 @@ def supports_lazy_transactions?
end

def quote_string(string)
with_raw_connection(allow_retry: true, uses_transaction: false) do |conn|
with_trilogy_connection(allow_retry: true, uses_transaction: false) do |conn|
conn.escape(string)
end
end

def connect!
verify!
self
end

def reconnect!
@lock.synchronize do
disconnect!
connect
rescue StandardError => original_exception
raise translate_exception_class(original_exception, nil, nil)
end
end

def with_trilogy_connection(uses_transaction: true, **_kwargs)
@lock.synchronize do
verify!
materialize_transactions if uses_transaction
yield connection
end
end

def raw_execute(sql, name, async: false, allow_retry: false, uses_transaction: true)
mark_transaction_written_if_write(sql)

log(sql, name, async: async) do
with_trilogy_connection(allow_retry: allow_retry, uses_transaction: uses_transaction) do |conn|
sync_timezone_changes(conn)
conn.query(sql)
end
end
end

def execute(sql, name = nil, **kwargs)
sql = transform_query(sql)
check_if_write_query(sql)
super
end

def active?
return false if connection&.closed?

connection&.ping || false
rescue ::Trilogy::Error
false
Expand Down Expand Up @@ -182,13 +231,7 @@ def error_number(exception)
end

private
def connection
@raw_connection
end

def connection=(conn)
@raw_connection = conn
end
attr_accessor :connection

def connect
self.connection = self.class.new_client(@config)
Expand All @@ -202,7 +245,7 @@ def reconnect

def sync_timezone_changes(conn)
# Sync any changes since connection last established.
if default_timezone == :local
if ActiveRecord.default_timezone == :local
conn.query_flags |= ::Trilogy::QUERY_FLAGS_LOCAL_TIMEZONE
else
conn.query_flags &= ~::Trilogy::QUERY_FLAGS_LOCAL_TIMEZONE
Expand All @@ -212,7 +255,7 @@ def sync_timezone_changes(conn)
def execute_batch(statements, name = nil)
statements = statements.map { |sql| transform_query(sql) }
combine_multi_statements(statements).each do |statement|
with_raw_connection do |conn|
with_trilogy_connection do |conn|
raw_execute(statement, name)
conn.next_result while conn.more_results_exist?
end
Expand All @@ -228,7 +271,7 @@ def with_multi_statements
return yield
end

with_raw_connection do |conn|
with_trilogy_connection do |conn|
conn.set_server_option(Trilogy::SET_SERVER_MULTI_STATEMENTS_ON)

yield
Expand Down Expand Up @@ -269,7 +312,7 @@ def full_version
end

def get_full_version
with_raw_connection(allow_retry: true, uses_transaction: false) do |conn|
with_trilogy_connection(allow_retry: true, uses_transaction: false) do |conn|
conn.server_info[:version]
end
end
Expand Down
18 changes: 9 additions & 9 deletions lib/trilogy_adapter/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,43 @@
module TrilogyAdapter
module Errors
# ServerShutdown will be raised when the database server was shutdown.
class ServerShutdown < ActiveRecord::ConnectionFailed
class ServerShutdown < ::ActiveRecord::QueryAborted
end

# ServerLost will be raised when the database connection was lost.
class ServerLost < ActiveRecord::ConnectionFailed
class ServerLost < ::ActiveRecord::QueryAborted
end

# ServerGone will be raised when the database connection is gone.
class ServerGone < ActiveRecord::ConnectionFailed
class ServerGone < ::ActiveRecord::QueryAborted
end

# BrokenPipe will be raised when a system process connection fails.
class BrokenPipe < ActiveRecord::ConnectionFailed
class BrokenPipe < ::ActiveRecord::QueryAborted
end

# SocketError will be raised when Ruby encounters a network error.
class SocketError < ActiveRecord::ConnectionFailed
class SocketError < ::ActiveRecord::QueryAborted
end

# ConnectionResetByPeer will be raised when a network connection is closed
# outside the sytstem process.
class ConnectionResetByPeer < ActiveRecord::ConnectionFailed
class ConnectionResetByPeer < ::ActiveRecord::QueryAborted
end

# ClosedConnection will be raised when the Trilogy encounters a closed
# connection.
class ClosedConnection < ActiveRecord::ConnectionFailed
class ClosedConnection < ::ActiveRecord::QueryAborted
end

# InvalidSequenceId will be raised when Trilogy ecounters an invalid sequence
# id.
class InvalidSequenceId < ActiveRecord::ConnectionFailed
class InvalidSequenceId < ::ActiveRecord::QueryAborted
end

# UnexpectedPacket will be raised when Trilogy ecounters an unexpected
# response packet.
class UnexpectedPacket < ActiveRecord::ConnectionFailed
class UnexpectedPacket < ::ActiveRecord::QueryAborted
end
end
end
Loading

0 comments on commit 7976c68

Please sign in to comment.