sequel-bulk-audit 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 969f874c37e5b8cdd0bc74f1e5fb8eff41ddc680
4
+ data.tar.gz: 899ee7a547d11be1ba2b6bd03c008aa26a307722
5
+ SHA512:
6
+ metadata.gz: 4d1df05162de7060a17f396e0951c1c584c573cf3cae55baa0cea365242cb2b18409551b498bb48e1dcd35c47323cd3bb44299edea93dfb8b2352164a04d691a
7
+ data.tar.gz: f755c70544b356c0143dbc0adf7e7a54dc5ca2144575587f8ef610119405cc358d72ce0a45e73f3b99d4298c05fce1d7558c0e8dd25474586cda1309d8d2cb13
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
6
+ addons:
7
+ postgresql: "9.6"
8
+ services:
9
+ - postgresql
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sequel-bulk-audit.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # sequel-bulk-audit [![Build Status](https://travis-ci.org/fiscal-cliff/sequel-bulk-audit.svg?branch=master)](https://travis-ci.org/fiscal-cliff/sequel-bulk-audit)
2
+
3
+ This gem allows you to track any changes in your tables. This approach not only is suitable for model updates but also enables you to track dataset updates.
4
+
5
+ You should wrap your updating code as follows:
6
+
7
+ ```ruby
8
+ Model.with_current_user(current_user) do
9
+ Model.where(...).update(...)
10
+ end
11
+ ```
12
+
13
+ Method #with_current_user expects current_user to be an object (or record) having attributes id and login
14
+
15
+ You are able setup polymorphic associations between audit records and corresponding records.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'sequel-bulk-audit'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install sequel-bulk-audit
32
+
33
+ After Installation you should run ```rails g audit_migration``` generator.
34
+
35
+ You can exdend this migration by attaching the trigger to audited tables.
36
+
37
+ ## Usage
38
+
39
+ Models, changes in which you plan to audit should contain
40
+ ```ruby
41
+ plugin :bulk_audit
42
+ ```
43
+
44
+ Method #with_current_user should wrap all the operations on the table.
45
+
46
+ ```ruby
47
+ Model.with_current_user(current_user) do
48
+ Model.where(...).update(...)
49
+ end
50
+ ```
51
+
52
+ ## Development
53
+
54
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
55
+
56
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fiscal-cliff/sequel-bulk-audit.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sequel/bulk/audit"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ Description:
2
+ The generator inserts sequel migration
3
+ This migration contains trigger.
4
+ You can attach the trigger to tables here.
5
+
6
+ Example:
7
+ rails generate audit_migration
8
+
9
+ This will create:
10
+ what/will/it/create
@@ -0,0 +1,8 @@
1
+ class AuditMigrationGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def copy_audit_migration_files
5
+ version = Time.now.utc.strftime('%Y%m%d%H%M%S')
6
+ copy_file "01_migration.rb", "db/migrate/#{version}_CreateAuditTableAndTrigger.rb"
7
+ end
8
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ Sequel.migration do
4
+ up do
5
+ create_table(:audit_logs) do
6
+ primary_key :id
7
+ column :model_type, String
8
+ column :model_id, String
9
+ column :event, String
10
+ column :changed, :jsonb
11
+ column :user_id, :int
12
+ column :username, String
13
+ column :user_type, String, default: "User"
14
+ column :created_at, DateTime
15
+ column :query, String
16
+ column :data, :jsonb, default: Sequel.pg_jsonb({})
17
+
18
+ index :created_at
19
+ index %i[model_type model_id]
20
+ index :user_id
21
+ end
22
+
23
+ create_function(:audit_changes, <<~SQL, returns: :trigger, language: :plpgsql, replace: true)
24
+ DECLARE
25
+ changes jsonb := '{}'::jsonb;
26
+ ri RECORD;
27
+ n jsonb;
28
+ o jsonb;
29
+ __audit_info RECORD;
30
+ model_id text;
31
+ return_record RECORD;
32
+ trid bigint;
33
+ BEGIN
34
+ SELECT txid_current() INTO trid;
35
+ EXECUTE 'SELECT * FROM __audit_info_' || trid::text INTO __audit_info;
36
+ FOR ri IN
37
+ SELECT column_name
38
+ FROM information_schema.columns
39
+ WHERE
40
+ table_schema = quote_ident(TG_TABLE_SCHEMA)
41
+ AND table_name = quote_ident(TG_TABLE_NAME)
42
+ ORDER BY ordinal_position
43
+ LOOP
44
+ IF (TG_OP = 'UPDATE') THEN
45
+ EXECUTE 'SELECT to_jsonb(($1).' || ri.column_name || ')' INTO n USING NEW;
46
+ EXECUTE 'SELECT to_jsonb(($1).' || ri.column_name || ')' INTO o USING OLD;
47
+ IF (o != n) THEN
48
+ SELECT changes || jsonb_build_object(ri.column_name, ARRAY[o, n]) INTO changes;
49
+ END IF;
50
+ ELSE
51
+ IF (TG_OP = 'DELETE') THEN
52
+ EXECUTE 'SELECT to_jsonb(($1).' || ri.column_name || ')' INTO n USING OLD;
53
+ ELSIF (TG_OP = 'INSERT') THEN
54
+ EXECUTE 'SELECT to_jsonb(($1).' || ri.column_name || ')' INTO n USING NEW;
55
+ END IF;
56
+ SELECT changes || jsonb_build_object(ri.column_name, n) INTO changes;
57
+ END IF;
58
+ END LOOP;
59
+
60
+ CASE TG_OP
61
+ WHEN 'UPDATE' THEN
62
+ model_id := OLD.id;
63
+ return_record := NEW;
64
+ WHEN 'DELETE' THEN
65
+ model_id := OLD.id;
66
+ return_record := OLD;
67
+ WHEN 'INSERT' THEN
68
+ model_id := NEW.id;
69
+ return_record := NEW;
70
+ ELSE
71
+ RAISE WARNING '[AUDIT.IF_MODIFIED_FUNC] - Other action occurred: %, at %',TG_OP,now();
72
+ RETURN NULL;
73
+ END CASE;
74
+ INSERT INTO audit_logs ("model_type", "model_id", "event", "changed",
75
+ "created_at", "user_id", "username", "query", "data")
76
+ VALUES (coalesce((__audit_info.model_map ->> TG_TABLE_NAME::TEXT), TG_TABLE_NAME::TEXT), model_id, TG_OP, changes, NOW(), __audit_info.user_id,
77
+ __audit_info.username, current_query(), __audit_info.data);
78
+ RETURN return_record;
79
+ END;
80
+ SQL
81
+
82
+ ### @TODO: Setup the trigger for all tables changes of you want to audit
83
+ # execute <<~SQL
84
+ # CREATE TRIGGER audit_changes_on_table BEFORE INSERT OR UPDATE OR DELETE ON table
85
+ # FOR EACH ROW EXECUTE PROCEDURE audit_changes();
86
+ # SQL
87
+ end
88
+
89
+ down do
90
+ drop_function(:audit_changes, cascade: true)
91
+ drop_table(:audit_logs)
92
+ end
93
+ end
@@ -0,0 +1,2 @@
1
+ require "pry"
2
+ require "sequel/plugins/bulk_audit"
@@ -0,0 +1,45 @@
1
+ require "sequel/plugins/bulk_audit/version"
2
+ require 'sequel/model'
3
+
4
+ module Sequel
5
+ module Plugins
6
+ module BulkAudit
7
+ def self.apply(model, opts={})
8
+ model.instance_eval do
9
+ @excluded_columns = [*opts[:excluded_columns]]
10
+ end
11
+ end
12
+
13
+ module SharedMethods
14
+ def model_to_table_map
15
+ @@model_to_table_map ||= ObjectSpace.each_object(Class).select do |klazz|
16
+ next if klazz.name.nil?
17
+ klazz < Sequel::Model && klazz&.plugins&.include?(Sequel::Plugins::BulkAudit)
18
+ end.map { |c| [c.to_s, c.table_name] }.to_h.invert
19
+ end
20
+
21
+ def with_current_user(current_user, attributes = nil)
22
+ self.db.transaction do
23
+ trid = self.db.select(Sequel.function(:txid_current)).single_value
24
+ data = self.db.select(Sequel.expr(current_user&.id || 0).as(:user_id),
25
+ Sequel.cast(current_user&.login || "unspecified", :text).as(:username),
26
+ Sequel.pg_jsonb(model_to_table_map).as(:model_map),
27
+ Sequel.pg_jsonb(attributes || {}).as(:data))
28
+ self.db.create_table!(:"__audit_info_#{trid}", temp: true, as: data)
29
+ result = yield if block_given?
30
+ self.db.drop_table?(:"__audit_info_#{trid}")
31
+ result
32
+ end
33
+ end
34
+ end
35
+
36
+ module ClassMethods
37
+ include SharedMethods
38
+ end
39
+
40
+ module InstanceMethods
41
+ include SharedMethods
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ module Sequel
2
+ module Plugins
3
+ module BulkAudit
4
+ VERSION = "0.2.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,45 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "sequel/plugins/bulk_audit/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sequel-bulk-audit"
8
+ spec.version = Sequel::Plugins::BulkAudit::VERSION
9
+ spec.authors = ["Fox"]
10
+ spec.email = ["strong.drug@gmail.com"]
11
+
12
+ spec.summary = %q{This gem provides a trigger based solution for auditing table changes}
13
+ spec.description = %q{Every update on audited table will be logged. You can update the table in bulk}
14
+ spec.homepage = "https://github.com/fiscal-cliff/sequel-bulk-audit/"
15
+ spec.post_install_message = %q{ Next steps:
16
+ 1. Run rails g audit_migration
17
+ 2. Edit generated migration
18
+ 3. Apply the migration"
19
+ }
20
+
21
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
22
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
23
+ if spec.respond_to?(:metadata)
24
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
31
+ f.match(%r{^(test|spec|features)/})
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_dependency "sequel", ">= 4.0.0"
38
+ spec.add_dependency "pg", ">= 0.17.0"
39
+
40
+ spec.add_development_dependency "bundler", "~> 1.14"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "rspec", "~> 3.0"
43
+ spec.add_development_dependency "pry", "~> 0.10"
44
+ spec.add_development_dependency "sequel_polymorphic"
45
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel-bulk-audit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Fox
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.17.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.17.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sequel_polymorphic
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Every update on audited table will be logged. You can update the table
112
+ in bulk
113
+ email:
114
+ - strong.drug@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - lib/generators/audit_migration/USAGE
128
+ - lib/generators/audit_migration/audit_migration_generator.rb
129
+ - lib/generators/audit_migration/templates/01_migration.rb
130
+ - lib/sequel-bulk-audit.rb
131
+ - lib/sequel/plugins/bulk_audit.rb
132
+ - lib/sequel/plugins/bulk_audit/version.rb
133
+ - sequel-bulk-audit.gemspec
134
+ homepage: https://github.com/fiscal-cliff/sequel-bulk-audit/
135
+ licenses: []
136
+ metadata:
137
+ allowed_push_host: https://rubygems.org
138
+ post_install_message: " Next steps:\n 1. Run rails g audit_migration\n 2. Edit
139
+ generated migration\n 3. Apply the migration\"\n "
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.6.13
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: This gem provides a trigger based solution for auditing table changes
159
+ test_files: []