pg_audit_log 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pg_audit_log/function.rb +29 -5
- data/lib/pg_audit_log/triggers.rb +4 -4
- data/lib/pg_audit_log/version.rb +1 -1
- data/spec/triggers_spec.rb +16 -2
- metadata +1 -1
@@ -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
|
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('
|
26
|
-
unique_name := current_setting('
|
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 = '
|
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
|
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
|
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
|
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}
|
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}
|
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)
|
data/lib/pg_audit_log/version.rb
CHANGED
data/spec/triggers_spec.rb
CHANGED
@@ -31,19 +31,33 @@ describe PgAuditLog::Triggers do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
describe ".enable" do
|
34
|
-
it "should
|
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
|
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
|