Skip to content

Commit

Permalink
Add Rails 7.2 compatibility
Browse files Browse the repository at this point in the history
Also improves 7.1 compatibility

Close #252

Ref:
- rails/rails@009c7e7
- rails/rails#50064
  • Loading branch information
tagliala committed Nov 28, 2023
1 parent 05910e8 commit 2cb4051
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/active_record/connection_adapters/chronomodel_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
require 'chrono_model'

module ActiveRecord
# TODO: Remove when dropping Rails < 7.2 compatibility
module ConnectionHandling
def chronomodel_adapter_class
ConnectionAdapters::PostgreSQLAdapter
ChronoModel::Adapter
end

# Install the new adapter in ActiveRecord. This approach is required because
# the PG adapter defines +add_column+ itself, thus making impossible to use
# super() in overridden Module methods.
#
def chronomodel_connection(config) # :nodoc:
return chronomodel_adapter_class.new(config) if ActiveRecord::VERSION::STRING >= '7.1'

conn_params = config.symbolize_keys

conn_params.delete_if { |_, v| v.nil? }
Expand All @@ -29,9 +32,6 @@ def chronomodel_connection(config) # :nodoc:

adapter = ChronoModel::Adapter.new(conn, logger, conn_params, config)

# Rails 7.2.0, see ifad/chronomodel#236
adapter.connect! if adapter.respond_to?(:connect!)

unless adapter.chrono_supported?
raise ChronoModel::Error, 'Your database server is not supported by ChronoModel. ' \
'Currently, only PostgreSQL >= 9.3 is supported.'
Expand Down
4 changes: 4 additions & 0 deletions lib/chrono_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ def self.history_models

raise 'In order to use ChronoModel, set `config.active_record.schema_format` to `:sql`'
end

if ActiveRecord::ConnectionAdapters.respond_to?(:register)
ActiveRecord::ConnectionAdapters.register 'chronomodel', 'ChronoModel::Adapter', 'chrono_model/adapter'
end
15 changes: 15 additions & 0 deletions lib/chrono_model/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ class Adapter < ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
# The schema holding historical data
HISTORY_SCHEMA = 'history'

if ActiveRecord::VERSION::STRING >= '7.1'
def initialize(*)
super

connect!

unless chrono_supported?
raise ChronoModel::Error, 'Your database server is not supported by ChronoModel. ' \
'Currently, only PostgreSQL >= 9.3 is supported.'
end

chrono_setup!
end
end

# Returns true whether the connection adapter supports our
# implementation of temporal tables. Currently, Chronomodel
# is supported starting with PostgreSQL 9.3 (90300 in PostgreSQL's
Expand Down
5 changes: 4 additions & 1 deletion spec/aruba/rake_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
describe 'bundle exec rake -T' do
subject { last_command_started }

before { run_command_and_stop('bundle exec rake -T') }
before do
copy_db_config
run_command_and_stop('bundle exec rake -T')
end

it { is_expected.to have_output(load_schema_task(as_regexp: true)) }
end
Expand Down

0 comments on commit 2cb4051

Please sign in to comment.