From 033a4ea673e6c393c78e9bdace8d9f27b0d19db9 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Tue, 28 Nov 2023 17:08:55 +0100 Subject: [PATCH] Add Rails 7.2 compatibility Also improves 7.1 compatibility Close #252 Ref: - rails/rails@009c7e7 - rails/rails#50064 --- .../connection_adapters/chronomodel_adapter.rb | 8 ++++---- lib/chrono_model.rb | 4 ++++ lib/chrono_model/adapter.rb | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/active_record/connection_adapters/chronomodel_adapter.rb b/lib/active_record/connection_adapters/chronomodel_adapter.rb index 9626377..1a71f12 100644 --- a/lib/active_record/connection_adapters/chronomodel_adapter.rb +++ b/lib/active_record/connection_adapters/chronomodel_adapter.rb @@ -3,9 +3,10 @@ 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 @@ -13,6 +14,8 @@ def chronomodel_adapter_class # 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? } @@ -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.' diff --git a/lib/chrono_model.rb b/lib/chrono_model.rb index 2a2a583..3ed9101 100644 --- a/lib/chrono_model.rb +++ b/lib/chrono_model.rb @@ -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 diff --git a/lib/chrono_model/adapter.rb b/lib/chrono_model/adapter.rb index 519a941..354db72 100644 --- a/lib/chrono_model/adapter.rb +++ b/lib/chrono_model/adapter.rb @@ -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