pg_audit_log 0.4.5 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -41,12 +41,45 @@ class PgAuditLog::Entry < ActiveRecord::Base
41
41
  );
42
42
 
43
43
  ALTER SEQUENCE #{self.table_name}_id_seq OWNED BY #{self.table_name}.id;
44
+
45
+ CREATE OR REPLACE FUNCTION audit_log_insert_trigger()
46
+ RETURNS TRIGGER AS $$
47
+ DECLARE
48
+ tablename TEXT;
49
+ insert_sql TEXT;
50
+ create_table_sql TEXT;
51
+ month_start DATE;
52
+ month_end DATE;
53
+ BEGIN
54
+ tablename := '#{self.table_name}_' || to_char(NEW.occurred_at, 'YYYYMM');
55
+ insert_sql := 'INSERT INTO ' || tablename || ' VALUES($1.*)';
56
+ EXECUTE insert_sql USING NEW;
57
+ RETURN NULL;
58
+ EXCEPTION
59
+ WHEN null_value_not_allowed THEN
60
+ RETURN NULL;
61
+ WHEN undefined_table THEN
62
+ EXECUTE 'SELECT to_char($1, ''YYYY-MM-01'')::DATE' INTO month_start USING NEW.occurred_at;
63
+ EXECUTE 'SELECT ($1 + INTERVAL ''1 MONTH'')' INTO month_end USING month_start;
64
+ create_table_sql := 'CREATE TABLE ' || tablename || ' ( CHECK ( date(occurred_at) >= DATE ''' || month_start || ''' AND date(occurred_at) < DATE ''' ||
65
+ month_end || ''' ) ) INHERITS (#{self.table_name})';
66
+ EXECUTE create_table_sql;
67
+ EXECUTE 'CREATE INDEX ' || tablename || '_occurred_at ON ' || tablename || ' (date(occurred_at))';
68
+ EXECUTE insert_sql USING NEW;
69
+ RETURN NULL;
70
+ END;
71
+ $$
72
+ LANGUAGE plpgsql;
73
+
74
+ CREATE TRIGGER insert_audit_log_trigger
75
+ BEFORE INSERT ON audit_log
76
+ FOR EACH ROW EXECUTE PROCEDURE audit_log_insert_trigger();
44
77
  SQL
45
78
  connection.execute_without_auditing(sql)
46
79
  end
47
80
 
48
81
  def uninstall
49
- connection.execute("DROP TABLE IF EXISTS #{self.table_name}")
82
+ connection.execute("DROP TABLE IF EXISTS #{self.table_name} CASCADE")
50
83
  end
51
84
 
52
85
  def delete(id)
@@ -11,7 +11,7 @@ module PgAuditLog
11
11
 
12
12
  class << self
13
13
  def tables
14
- skip_tables = (PgAuditLog::IGNORED_TABLES + [PgAuditLog::Entry.table_name])
14
+ skip_tables = (PgAuditLog::IGNORED_TABLES + [PgAuditLog::Entry.table_name, /#{PgAuditLog::Entry.table_name}_[0-9]{6}/])
15
15
  connection.tables.reject do |table|
16
16
  skip_tables.include?(table) ||
17
17
  skip_tables.any? { |skip_table| skip_table =~ table if skip_table.is_a? Regexp }
@@ -1,3 +1,3 @@
1
1
  module PgAuditLog
2
- VERSION = "0.4.5"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -18,12 +18,12 @@ rescue PGError => e
18
18
  raise e
19
19
  end
20
20
 
21
-
22
21
  RSpec.configure do |config|
23
22
  config.mock_with :rspec
24
23
  config.extend WithModel
25
24
 
26
25
  config.before(:each) do
26
+ PgAuditLog::Entry.uninstall rescue nil
27
27
  connection.tables.each do |table|
28
28
  connection.drop_table_without_auditing(table)
29
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_audit_log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70252020501060 !ruby/object:Gem::Requirement
16
+ requirement: &70242168359480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70252020501060
24
+ version_requirements: *70242168359480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: pg
27
- requirement: &70252020500560 !ruby/object:Gem::Requirement
27
+ requirement: &70242168358980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.9.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70252020500560
35
+ version_requirements: *70242168358980
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec-rails
38
- requirement: &70252020500100 !ruby/object:Gem::Requirement
38
+ requirement: &70242168358520 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - =
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '2.7'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70252020500100
46
+ version_requirements: *70242168358520
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: with_model
49
- requirement: &70252020499640 !ruby/object:Gem::Requirement
49
+ requirement: &70242168358060 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.1.3
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70252020499640
57
+ version_requirements: *70242168358060
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: autotest
60
- requirement: &70252020499260 !ruby/object:Gem::Requirement
60
+ requirement: &70242168357680 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70252020499260
68
+ version_requirements: *70242168357680
69
69
  description: A completely transparent audit logging component for your application
70
70
  using a stored procedure and triggers. Comes with specs for your project and a rake
71
71
  task to generate the reverse SQL to undo changes logged
@@ -120,7 +120,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
120
120
  version: '0'
121
121
  segments:
122
122
  - 0
123
- hash: 3581204670683200493
123
+ hash: 1658347057273507033
124
124
  required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  none: false
126
126
  requirements:
@@ -129,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  version: '0'
130
130
  segments:
131
131
  - 0
132
- hash: 3581204670683200493
132
+ hash: 1658347057273507033
133
133
  requirements: []
134
134
  rubyforge_project:
135
135
  rubygems_version: 1.8.15