active_pg_log 0.1.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
+ SHA256:
3
+ metadata.gz: 1ec1432a7d5f0433655ec7cdaad67f349c315f0fe7feb029384ee30917bb2262
4
+ data.tar.gz: eabfdfbcd3db9fe50cd739c8d0d57d5b96216e83c1c65c814b73d576d17b437b
5
+ SHA512:
6
+ metadata.gz: 75f1ddfa00c0fd4dff5934db8adda8693667635fe6e29da7a8f90e323da13965aa7921b3caeb85c5cd86e27694830a39a94cd3f19ca550c2714295c2690355de
7
+ data.tar.gz: c6c9a65eede438bccb928ad11a3436e4a7e1b3ff25426fa17460d34f546e4e56a404e2fb4fa53f48183a630036aea381a0593e7694b1990cdfc76559860b0670
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.6
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
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 active_pg_log.gemspec
6
+ gemspec
7
+
8
+ gem 'activerecord'
9
+ gem 'rubocop'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # ActivePgLog
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/active_pg_log`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'active_pg_log'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install active_pg_log
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_pg_log. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the ActivePgLog project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/active_pg_log/blob/master/CODE_OF_CONDUCT.md).
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
@@ -0,0 +1,38 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'active_pg_log'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'active_pg_log'
7
+ spec.version = ActivePgLog::VERSION
8
+ spec.authors = ['Wesley Oliveira']
9
+ spec.email = ['waejloliveira@gmail.com']
10
+
11
+ spec.summary = 'Gem para log de tabelas via banco de dados PostgreSQL sincronizado com ActiveRecord attributes.'
12
+ spec.description = 'Gem para log de tabelas.'
13
+ spec.homepage = 'https://rubygems.org/gems/active_pg_log'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = 'https://github.com/waejl/active_pg_log'
22
+ spec.metadata['changelog_uri'] = 'https://github.com/waejl/active_pg_log'
23
+ else
24
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
25
+ 'public gem pushes.'
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = `git ls-files`.split("\n")
31
+ spec.bindir = 'bin'
32
+ # spec.executables = %w[setup console]
33
+ spec.require_paths = ['lib']
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.17'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'rspec', '~> 3.0'
38
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_pg_log"
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,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ module ActivePgLog
5
+ VERSION = '0.1.0'
6
+
7
+ class Configuration
8
+ class << self
9
+ def destroy
10
+ return unless ActiveRecord::Base.connection.tables.include?('active_pg_log_tables')
11
+
12
+ ActiveRecord::Base.transaction do
13
+ ActiveRecord::Base.connection.execute('drop table if exists active_pg_log_tables cascade;')
14
+ ActiveRecord::Base.connection.execute('drop function if exists active_pg_loging() cascade;')
15
+ end
16
+ end
17
+
18
+ def setup
19
+ return if ActiveRecord::Base.connection.tables.include?('active_pg_log_tables')
20
+
21
+ ActiveRecord::Base.connection.transaction do
22
+ ActiveRecord::Base.connection.execute(log_ddl_table)
23
+ ActiveRecord::Base.connection.execute(log_ddl_function)
24
+ end
25
+ end
26
+
27
+ def log_ddl_table
28
+ %(
29
+ CREATE TABLE if not exists active_pg_log_tables(
30
+ id serial not null,
31
+ id_reference integer not null,
32
+ table_name character varying not null,
33
+ user_name character varying not null,
34
+ data text,
35
+ created_at timestamp without time zone default now()
36
+ );
37
+ )
38
+ end
39
+
40
+ def log_ddl_function
41
+ %(
42
+ drop function if exists public.active_pg_loging();
43
+ CREATE OR REPLACE function public.active_pg_loging() returns trigger as $$
44
+ DECLARE
45
+ newRecord JSON;
46
+ BEGIN
47
+ if TG_OP = 'INSERT' or TG_OP = 'UPDATE' then
48
+ select row_to_json(NEW.*) into newRecord;
49
+ insert into active_pg_log_tables (id_reference,table_name,user_name,data,created_at) values (NEW.id,TG_TABLE_NAME,current_user,newRecord,now());
50
+ end if;
51
+ return null;
52
+ END;
53
+ $$ language 'plpgsql';
54
+ )
55
+ end
56
+ end
57
+ end
58
+
59
+ class ActiveTrigger
60
+ attr_accessor :dados
61
+
62
+ def initialize
63
+ @dados = ActiveRecord::Base.connection.select_all(query)
64
+ end
65
+
66
+ def include?(trigger_name, table_name)
67
+ return false if @dados.rows.size.zero?
68
+
69
+ @dados.select { |e| e['table_name'] == table_name && e['trigger_name'] == trigger_name }.size.positive?
70
+ end
71
+
72
+ def query
73
+ %(select event_object_schema as table_schema,
74
+ event_object_table as table_name,
75
+ trigger_schema,
76
+ trigger_name,
77
+ string_agg(event_manipulation, ',') as event,
78
+ action_timing as activation,
79
+ action_condition as condition,
80
+ action_statement as definition
81
+ from information_schema.triggers
82
+ group by 1,2,3,4,6,7,8
83
+ order by table_schema,
84
+ table_name;
85
+ )
86
+ end
87
+ end
88
+
89
+ class ActivePgLogTable < ActiveRecord::Base
90
+ end
91
+
92
+ module ActiveLog
93
+ def self.included(clazz)
94
+ @active_trigger = ActivePgLog::ActiveTrigger.new
95
+
96
+ return if @active_trigger.include?("trigger_log_#{clazz.table_name}_insert_update", clazz.table_name)
97
+
98
+ ActiveRecord::Base.connection.execute(create_log_ddl_trigger(clazz))
99
+ end
100
+
101
+ def disable_log_ddl_trigger
102
+ ActiveRecord::Base.connection.execute("drop trigger if exists trigger_log_#{self.class.table_name}_insert_update on #{self.class.table_name} cascade;")
103
+ nil
104
+ end
105
+
106
+ def pg_logs
107
+ ActivePgLogTable.where(table_name: self.class.table_name)
108
+ end
109
+
110
+ class << self
111
+ def create_log_ddl_trigger(clazz)
112
+ %(
113
+ drop trigger if exists trigger_log_#{clazz.table_name}_insert_update on #{clazz.table_name} cascade;
114
+ CREATE TRIGGER trigger_log_#{clazz.table_name}_insert_update after insert or update on #{clazz.table_name} for each row execute procedure public.active_pg_loging();
115
+ )
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ActivePgLog
4
+ module Generators
5
+ class InstallGenerator < ::ActiveRecord::Generators::Base
6
+ argument :name, type: :string, default: 'random_name'
7
+
8
+ source_root File.expand_path('templates', __dir__)
9
+
10
+ def install
11
+ directory 'lib/tasks'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :active_pg_log do
4
+ desc 'Install table on database'
5
+ task install: :environment do
6
+ ActivePgLog::Configuration.setup
7
+ end
8
+
9
+ desc 'Remove table and function of the database'
10
+ task uninstall: :environment do
11
+ ActivePgLog::Configuration.destroy
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ RSpec.describe ActivePgLog do
2
+ it 'has a version number' do
3
+ expect(ActivePgLog::VERSION).not_to be nil
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require "bundler/setup"
2
+ require "active_pg_log"
3
+
4
+ RSpec.configure do |config|
5
+ # Enable flags like --only-failures and --next-failure
6
+ config.example_status_persistence_file_path = ".rspec_status"
7
+
8
+ # Disable RSpec exposing methods globally on `Module` and `main`
9
+ config.disable_monkey_patching!
10
+
11
+ config.expect_with :rspec do |c|
12
+ c.syntax = :expect
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_pg_log
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wesley Oliveira
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Gem para log de tabelas.
56
+ email:
57
+ - waejloliveira@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - active_pg_log.gemspec
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/active_pg_log.rb
74
+ - lib/generators/active_pg_log/install_generator.rb
75
+ - lib/generators/active_pg_log/templates/lib/tasks/active_pg_log.rake
76
+ - spec/active_pg_log_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: https://rubygems.org/gems/active_pg_log
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ allowed_push_host: https://rubygems.org
83
+ homepage_uri: https://rubygems.org/gems/active_pg_log
84
+ source_code_uri: https://github.com/waejl/active_pg_log
85
+ changelog_uri: https://github.com/waejl/active_pg_log
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.0.6
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Gem para log de tabelas via banco de dados PostgreSQL sincronizado com ActiveRecord
105
+ attributes.
106
+ test_files: []