# frozen_string_literal: true require 'chrono_model' module ActiveRecord # TODO: Remove when dropping Rails < 7.2 compatibility module ConnectionHandling def chronomodel_adapter_class 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? } # Map ActiveRecords param names to PGs. conn_params[:user] = conn_params.delete(:username) if conn_params[:username] conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database] # Forward only valid config params to PG::Connection.connect. valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl] conn_params.slice!(*valid_conn_param_keys) conn = PG.connect(conn_params) if ActiveRecord::VERSION::MAJOR >= 6 adapter = ChronoModel::Adapter.new(conn, logger, conn_params, config) unless adapter.chrono_supported? raise ChronoModel::Error, 'Your database server is not supported by ChronoModel. ' \ 'Currently, only PostgreSQL >= 9.3 is supported.' end adapter.chrono_setup! adapter rescue ::PG::Error => e raise ActiveRecord::NoDatabaseError if e.message.include?(conn_params[:dbname]) raise end end end