Sha256: 9cd1807fc5275262bccc17d9dd7350be6b9b072ce37a5b1c25889f58de7cf6e7

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module SQLRunner
  module Adapters
    class ActiveRecord
      class PostgreSQL < SQLRunner::Adapters::PostgreSQL
        def initialize(connection) # rubocop:disable Lint/MissingSuper
          @connection = connection
        end

        def connect(*)
        end

        def disconnect(*)
        end
      end

      class MySQL < SQLRunner::Adapters::MySQL
        def initialize(connection) # rubocop:disable Lint/MissingSuper
          @connection = connection
        end

        def connect(*)
        end

        def disconnect(*)
        end
      end

      class ConnectionPool
        def with
          ::ActiveRecord::Base.connection_pool.with_connection do |connection|
            connection = connection.instance_variable_get(:@connection)

            adapter = case connection.class.name
                      when "PG::Connection"
                        PostgreSQL.new(connection)
                      when "Mysql2::Client"
                        MySQL.new(connection)
                      else
                        raise UnsupportedDatabase
                      end

            yield(adapter)
          end
        end

        def shutdown
        end
      end

      def self.load
        require "active_record"
      rescue LoadError
        raise MissingDependency, "make sure the `activerecord` gem is available"
      end

      def self.create_connection_pool(*)
        ConnectionPool.new
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sql_runner-0.3.0 lib/sql_runner/adapters/active_record.rb