pg_audit_log 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,34 @@
1
1
  module PgAuditLog
2
2
  class Function < PgAuditLog::ActiveRecord
3
3
  class << self
4
+ def name
5
+ "audit_changes"
6
+ end
7
+
8
+ def custom_variable
9
+ "audit"
10
+ end
11
+
12
+ def users_table_name
13
+ "users"
14
+ end
15
+
16
+ def user_id_field
17
+ "user_id"
18
+ end
19
+
20
+ def user_name_field
21
+ "user_unique_name"
22
+ end
23
+
24
+ def users_access_column
25
+ "last_accessed_at"
26
+ end
27
+
4
28
  def install
5
29
  execute <<-SQL
6
30
  CREATE OR REPLACE PROCEDURAL LANGUAGE plpgsql;
7
- CREATE OR REPLACE FUNCTION audit_changes() RETURNS trigger
31
+ CREATE OR REPLACE FUNCTION #{name}() RETURNS trigger
8
32
  LANGUAGE plpgsql
9
33
  AS $_$
10
34
  DECLARE
@@ -22,8 +46,8 @@ module PgAuditLog
22
46
  old_value := NULL;
23
47
  primary_key_column := NULL;
24
48
  primary_key_value := NULL;
25
- user_identifier := current_setting('audit.user_id');
26
- unique_name := current_setting('audit.user_unique_name');
49
+ user_identifier := current_setting('#{custom_variable}.#{user_id_field}');
50
+ unique_name := current_setting('#{custom_variable}.#{user_name_field}');
27
51
  column_name := col.column_name;
28
52
 
29
53
  EXECUTE 'SELECT pg_attribute.attname
@@ -48,7 +72,7 @@ module PgAuditLog
48
72
  END IF;
49
73
  END IF;
50
74
 
51
- IF TG_RELNAME = 'users' AND column_name = 'last_accessed_at' THEN
75
+ IF TG_RELNAME = '#{users_table_name}' AND column_name = '#{users_access_column}' THEN
52
76
  NULL;
53
77
  ELSE
54
78
  IF TG_OP != 'UPDATE' OR new_value != old_value OR (TG_OP = 'UPDATE' AND ( (new_value IS NULL AND old_value IS NOT NULL) OR (new_value IS NOT NULL AND old_value IS NULL))) THEN
@@ -81,7 +105,7 @@ module PgAuditLog
81
105
  end
82
106
 
83
107
  def uninstall
84
- execute "DROP FUNCTION audit_changes()"
108
+ execute "DROP FUNCTION #{name}()"
85
109
  end
86
110
  end
87
111
  end
@@ -40,11 +40,11 @@ module PgAuditLog
40
40
 
41
41
  def create_for_table(table_name)
42
42
  execute <<-SQL
43
- CREATE TRIGGER audit_#{table_name}
43
+ CREATE TRIGGER #{trigger_name_for_table(table_name)}
44
44
  AFTER INSERT OR UPDATE OR DELETE
45
45
  ON #{table_name}
46
46
  FOR EACH ROW
47
- EXECUTE PROCEDURE audit_changes()
47
+ EXECUTE PROCEDURE #{PgAuditLog::Function.name}()
48
48
  SQL
49
49
  end
50
50
 
@@ -53,11 +53,11 @@ module PgAuditLog
53
53
  end
54
54
 
55
55
  def enable_for_table(table_name)
56
- execute "ALTER TABLE #{table_name} DISABLE TRIGGER #{trigger_name_for_table(table_name)}"
56
+ execute "ALTER TABLE #{table_name} ENABLE TRIGGER #{trigger_name_for_table(table_name)}"
57
57
  end
58
58
 
59
59
  def disable_for_table(table_name)
60
- execute "ALTER TABLE #{table_name} ENABLE TRIGGER #{trigger_name_for_table(table_name)}"
60
+ execute "ALTER TABLE #{table_name} DISABLE TRIGGER #{trigger_name_for_table(table_name)}"
61
61
  end
62
62
 
63
63
  def trigger_name_for_table(table_name)
@@ -1,3 +1,3 @@
1
1
  module PgAuditLog
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -31,19 +31,33 @@ describe PgAuditLog::Triggers do
31
31
  end
32
32
 
33
33
  describe ".enable" do
34
- it "should work" do
34
+ it "should not blow up" do
35
35
  ->{
36
36
  PgAuditLog::Triggers.enable
37
37
  }.should_not raise_error
38
38
  end
39
+
40
+ it "should fire the audit" do
41
+ PgAuditLog::Triggers.enable
42
+ expect {
43
+ TableWithTriggers.create!
44
+ }.to change(PgAuditLog::Entry, :count)
45
+ end
39
46
  end
40
47
 
41
48
  describe ".disable" do
42
- it "should work" do
49
+ it "should not blow up" do
43
50
  ->{
44
51
  PgAuditLog::Triggers.disable
45
52
  }.should_not raise_error
46
53
  end
54
+
55
+ it "should not fire the audit" do
56
+ PgAuditLog::Triggers.disable
57
+ expect {
58
+ TableWithTriggers.create!
59
+ }.to_not change(PgAuditLog::Entry, :count)
60
+ end
47
61
  end
48
62
 
49
63
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: pg_audit_log
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Case Commons, LLC