Class: PgAuditLog::Triggers

Inherits:
ActiveRecord show all
Defined in:
lib/pg_audit_log/triggers.rb

Defined Under Namespace

Classes: MissingTriggers

Class Method Summary collapse

Class Method Details

.all_tables_without_triggersObject



35
36
37
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pg_audit_log/triggers.rb', line 66

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

.disableObject



55
56
57
# File 'lib/pg_audit_log/triggers.rb', line 55

def disable
  connection.set_user_id(PgAuditLog::Function::DISABLED_USER)
end

.disable_for_table(table_name) ⇒ Object



88
89
90
# File 'lib/pg_audit_log/triggers.rb', line 88

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) ⇒ Object



79
80
81
82
# File 'lib/pg_audit_log/triggers.rb', line 79

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

.enableObject



51
52
53
# File 'lib/pg_audit_log/triggers.rb', line 51

def enable
  connection.set_user_id(nil)
end

.enable_for_table(table_name) ⇒ Object



84
85
86
# File 'lib/pg_audit_log/triggers.rb', line 84

def enable_for_table(table_name)
  execute "ALTER TABLE #{table_name} ENABLE TRIGGER #{trigger_name_for_table(table_name)}"
end

.installObject



39
40
41
42
43
# 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

.tablesObject



13
14
15
16
17
18
19
# 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_triggersObject



21
22
23
24
25
26
27
28
29
# 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_triggersObject



31
32
33
# File 'lib/pg_audit_log/triggers.rb', line 31

def tables_without_triggers
  tables - tables_with_triggers
end

.trigger_name_for_table(table_name) ⇒ Object



96
97
98
# File 'lib/pg_audit_log/triggers.rb', line 96

def trigger_name_for_table(table_name)
  "#{trigger_prefix}#{table_name}"
end

.trigger_prefixObject



92
93
94
# File 'lib/pg_audit_log/triggers.rb', line 92

def trigger_prefix
  'audit_'
end

.uninstallObject



45
46
47
48
49
# 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_triggersObject



59
60
61
62
63
64
# File 'lib/pg_audit_log/triggers.rb', line 59

def without_triggers
  disable
  yield
ensure
  enable
end