Sha256: 4077c43c7c1807d77f816758a49f9053634408ec0d3ccbaab4bec03c177de654

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require 'active_record/log_subscriber'

module Apartment
  # Custom Log subscriber to include database name and schema name in sql logs
  class LogSubscriber < ActiveRecord::LogSubscriber
    # NOTE: for some reason, if the method definition is not here, then the custom debug method is not called
    # rubocop:disable Lint/UselessMethodDefinition
    def sql(event)
      super(event)
    end
    # rubocop:enable Lint/UselessMethodDefinition

    private

    def debug(progname = nil, &block)
      progname = "  #{apartment_log}#{progname}" unless progname.nil?

      super(progname, &block)
    end

    def apartment_log
      database = color("[#{Apartment.connection.raw_connection.db}] ", ActiveSupport::LogSubscriber::MAGENTA, true)
      schema = nil
      unless Apartment.connection.schema_search_path.nil?
        schema = color("[#{Apartment.connection.schema_search_path.tr('"', '')}] ",
                       ActiveSupport::LogSubscriber::YELLOW, true)
      end
      "#{database}#{schema}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ros-apartment-2.11.0 lib/apartment/log_subscriber.rb
ros-apartment-2.10.0 lib/apartment/log_subscriber.rb
ros-apartment-2.9.0 lib/apartment/log_subscriber.rb
ros-apartment-2.8.1 lib/apartment/log_subscriber.rb
ros-apartment-2.8.1.rc2 lib/apartment/log_subscriber.rb