Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database and schema to active record log #55

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# require_relative 'apartment/arel/visitors/postgresql'

require_relative 'apartment/active_record/log_subscriber'
require_relative 'apartment/active_record/connection_handling' if ActiveRecord.version.release >= Gem::Version.new('6.0')

if ActiveRecord.version.release >= Gem::Version.new('6.1')
Expand All @@ -20,7 +21,7 @@ class << self
extend Forwardable

ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment
append_environment with_multi_server_setup tenant_presence_check].freeze
append_environment with_multi_server_setup tenant_presence_check active_record_log].freeze

WRITER_METHODS = %i[tenant_names database_schema_file excluded_models
default_schema persistent_schemas connection_class
Expand Down
41 changes: 41 additions & 0 deletions lib/apartment/active_record/log_subscriber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module ActiveRecord
class LogSubscriber
def apartment_log
return unless Apartment.active_record_log

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

def payload_binds(binds, type_casted_binds)
return unless (binds || []).empty?

casted_params = type_casted_binds(type_casted_binds)
binds = ' ' + binds.zip(casted_params).map { |attr, value| render_bind(attr, value) }.inspect
binds
end

def sql(event)
self.class.runtime += event.duration
return unless logger.debug?

payload = event.payload

return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])

name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
name = "CACHE #{name}" if payload[:cached]
sql = payload[:sql]
binds = payload_binds(payload[:binds], payload[:type_casted_binds])

name = colorize_payload_name(name, payload[:name])
sql = color(sql, sql_color(sql), true) if colorize_logging

debug " #{apartment_log}#{name} #{sql}#{binds}"
end
end
end
1 change: 1 addition & 0 deletions lib/apartment/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Railtie < Rails::Railtie
config.prepend_environment = false
config.append_environment = false
config.tenant_presence_check = true
config.active_record_log = false
end

ActiveRecord::Migrator.migrations_paths = Rails.application.paths['db/migrate'].to_a
Expand Down
5 changes: 5 additions & 0 deletions lib/generators/apartment/install/templates/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
# the new tenant
#
# config.pg_excluded_names = ["uuid_generate_v4"]

# Specifies whether the database and schema (when using PostgreSQL schemas) will prepend in ActiveRecord log.
# Uncomment the line below if you want to enable this behavior.
#
# config.active_record_log = true
end

# Setup a custom Tenant switching middleware. The Proc should return the name of the Tenant that
Expand Down