class PgAuditLog::Triggers

Public Class Methods

all_tables_without_triggers() click to toggle source
# File lib/pg_audit_log/triggers.rb, line 35
def all_tables_without_triggers
  connection.tables - tables_with_triggers
end
create_for_table(table_name) click to toggle source
# File lib/pg_audit_log/triggers.rb, line 69
      def create_for_table(table_name)
        PgAuditLog::Entry.install unless PgAuditLog::Entry.installed?
        PgAuditLog::Function.install unless PgAuditLog::Function.installed?
        return if tables_with_triggers.include?(table_name)
        execute <<-SQL
          CREATE TRIGGER #{trigger_name_for_table(table_name)}
          AFTER INSERT OR UPDATE OR DELETE
          ON #{table_name}
          FOR EACH ROW
          EXECUTE PROCEDURE #{PgAuditLog::Function.name}()
        SQL
      end
disable() click to toggle source
# File lib/pg_audit_log/triggers.rb, line 56
def disable
  @disabled = true
  connection.set_user_id(PgAuditLog::Function::DISABLED_USER)
end
disable_for_table(table_name) click to toggle source
# File lib/pg_audit_log/triggers.rb, line 91
def disable_for_table(table_name)
  execute "ALTER TABLE #{table_name} DISABLE TRIGGER #{trigger_name_for_table(table_name)}"
end
drop_for_table(table_name) click to toggle source
# File lib/pg_audit_log/triggers.rb, line 82
def drop_for_table(table_name)
  return unless tables_with_triggers.include?(table_name)
  execute "DROP TRIGGER #{trigger_name_for_table(table_name)} ON #{table_name}"
end
enable() click to toggle source
# File lib/pg_audit_log/triggers.rb, line 51
def enable
  @disabled = false
  connection.set_user_id(nil)
end
enable_for_table(table_name) click to toggle source
# File lib/pg_audit_log/triggers.rb, line 87
def enable_for_table(table_name)
  execute "ALTER TABLE #{table_name} ENABLE TRIGGER #{trigger_name_for_table(table_name)}"
end
install() click to toggle source
# File lib/pg_audit_log/triggers.rb, line 39
def install
  tables.each do |table|
    create_for_table(table) unless tables_with_triggers.include?(table)
  end
end
tables() click to toggle source
# File lib/pg_audit_log/triggers.rb, line 13
def tables
  skip_tables = (PgAuditLog::IGNORED_TABLES + [PgAuditLog::Entry.table_name, /#{PgAuditLog::Entry.table_name}_[0-9]{6}/])
  connection.tables.reject do |table|
    skip_tables.include?(table) ||
      skip_tables.any? { |skip_table| skip_table =~ table if skip_table.is_a? Regexp }
  end
end
tables_with_triggers() click to toggle source
# File lib/pg_audit_log/triggers.rb, line 21
      def tables_with_triggers
        connection.select_values <<-SQL
          SELECT tables.relname as table_name
          FROM pg_trigger triggers, pg_class tables
          WHERE triggers.tgrelid = tables.oid
          AND tables.relname !~ '^pg_'
          AND triggers.tgname LIKE '#{trigger_prefix}%'
        SQL
      end
tables_without_triggers() click to toggle source
# File lib/pg_audit_log/triggers.rb, line 31
def tables_without_triggers
  tables - tables_with_triggers
end
trigger_name_for_table(table_name) click to toggle source
# File lib/pg_audit_log/triggers.rb, line 99
def trigger_name_for_table(table_name)
  "#{trigger_prefix}#{table_name}"
end
trigger_prefix() click to toggle source
# File lib/pg_audit_log/triggers.rb, line 95
def trigger_prefix
  'audit_'
end
uninstall() click to toggle source
# File lib/pg_audit_log/triggers.rb, line 45
def uninstall
  tables_with_triggers.each do |table|
    drop_for_table(table) if tables_with_triggers.include?(table)
  end
end
without_triggers() { || ... } click to toggle source
# File lib/pg_audit_log/triggers.rb, line 61
def without_triggers
  was_disabled = @disabled
  disable unless was_disabled
  yield
ensure
  enable unless was_disabled
end