Sha256: 226bcd52de8403c8ad70562ff0bcd63ebdd719bd5d1068e0e29491fa1be7e8d0

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

require "active_record/connection_adapters/postgresql_adapter"

# Did not want to reopen the class but sending an include seemingly is not working.
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
  def execute_with_auditing(sql, name = nil)
    current_user = Thread.current[:current_user]
    user_unique_name = current_user.try(:unique_name) || "UNKNOWN"

    log_user_id = %[SET audit.user_id = #{current_user.try(:id) || "-1"}]
    log_user_unique_name = %[SET audit.user_unique_name = "#{user_unique_name}"]

    log([log_user_id, log_user_unique_name, sql].join("; "), name) do
      if @async
        @connection.async_exec(log_user_id)
        @connection.async_exec(log_user_unique_name)
        @connection.async_exec(sql)
      else
        @connection.exec(log_user_id)
        @connection.exec(log_user_unique_name)
        @connection.exec(sql)
      end
    end
  end

  alias_method_chain :execute, :auditing

  def begin_db_transaction
    execute_without_auditing "BEGIN"
  end

  # Commits a transaction.
  def commit_db_transaction
    execute_without_auditing "COMMIT"
  end

  # Aborts a transaction.
  def rollback_db_transaction
    execute_without_auditing "ROLLBACK"
  end

  def create_savepoint
    execute_without_auditing("SAVEPOINT #{current_savepoint_name}")
  end

  def rollback_to_savepoint
    execute_without_auditing("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
  end

  def release_savepoint
    execute_without_auditing("RELEASE SAVEPOINT #{current_savepoint_name}")
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pg_audit_log-0.2.0 lib/pg_audit_log/extensions/3.0/postgresql_adapter.rb
pg_audit_log-0.1.4 lib/pg_audit_log/extensions/postgresql_adapter.rb
pg_audit_log-0.1.3 lib/pg_audit_log/extensions/postgresql_adapter.rb
pg_audit_log-0.1.2 lib/pg_audit_log/extensions/postgresql_adapter.rb
pg_audit_log-0.1.1 lib/pg_audit_log/extensions/postgresql_adapter.rb
pg_audit_log-0.1.0 lib/pg_audit_log/extensions/postgresql_adapter.rb
pg_audit_log-0.0.2 lib/pg_audit_log/extensions/postgresql_adapter.rb
pg_audit_log-0.0.1 lib/pg_audit_log/extensions/postgresql_adapter.rb