Class: PgAuditLog::Triggers
- Inherits:
-
ActiveRecord
- Object
- ActiveRecord
- PgAuditLog::Triggers
- Defined in:
- lib/pg_audit_log/triggers.rb
Defined Under Namespace
Classes: MissingTriggers
Class Method Summary collapse
- .all_tables_without_triggers ⇒ Object
- .create_for_table(table_name) ⇒ Object
- .disable ⇒ Object
- .disable_for_table(table_name) ⇒ Object
- .drop_for_table(table_name) ⇒ Object
- .enable ⇒ Object
- .enable_for_table(table_name) ⇒ Object
- .install ⇒ Object
- .tables ⇒ Object
- .tables_with_triggers ⇒ Object
- .tables_without_triggers ⇒ Object
- .trigger_name_for_table(table_name) ⇒ Object
- .trigger_prefix ⇒ Object
- .uninstall ⇒ Object
- .without_triggers ⇒ Object
Class Method Details
.all_tables_without_triggers ⇒ Object
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 " CREATE TRIGGER \#{trigger_name_for_table(table_name)}\n AFTER INSERT OR UPDATE OR DELETE\n ON \#{table_name}\n FOR EACH ROW\n EXECUTE PROCEDURE \#{PgAuditLog::Function.name}()\n SQL\nend\n" |
.disable ⇒ Object
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 |
.enable ⇒ Object
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 |
.install ⇒ Object
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 |
.tables ⇒ Object
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_triggers ⇒ Object
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 " SELECT tables.relname as table_name\n FROM pg_trigger triggers, pg_class tables\n WHERE triggers.tgrelid = tables.oid\n AND tables.relname !~ '^pg_'\n AND triggers.tgname LIKE '\#{trigger_prefix}%'\n SQL\nend\n" |
.tables_without_triggers ⇒ Object
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_prefix ⇒ Object
92 93 94 |
# File 'lib/pg_audit_log/triggers.rb', line 92 def trigger_prefix 'audit_' end |
.uninstall ⇒ Object
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_triggers ⇒ Object
59 60 61 62 63 64 |
# File 'lib/pg_audit_log/triggers.rb', line 59 def without_triggers disable yield ensure enable end |