active_pg_log 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/active_pg_log.gemspec +1 -1
- data/init.rb +3 -0
- data/lib/active_pg_log.rb +6 -114
- data/lib/active_pg_log/active_log.rb +29 -0
- data/lib/active_pg_log/active_pg_log_table.rb +2 -0
- data/lib/active_pg_log/prepare.rb +53 -0
- data/lib/active_pg_log/trigger.rb +29 -0
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54365b3367760d3de57a0bbd6c222902a740d4704ea254281388968689796fec
|
4
|
+
data.tar.gz: 83abbc93332027947c30ecc858c4cbc138d568ae2dd437a837862c1547a3a010
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e711ccce1486e792f693753be1865421592ef94f2ce441f3eebfd04ed0b55364cbe7c77d2e027794db5da6d79d6c04806d08c12689e46f41b56fe8419de43ea3
|
7
|
+
data.tar.gz: bc543af36a6d776cb0b2806327197d150febb68afe463cd8002a390478eae224fe811e6ebd9adc24689277a2a910897fbe1e7b6c3f4cccc455744f7998f40529
|
data/active_pg_log.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
|
11
11
|
spec.summary = 'Gem para log de tabelas via banco de dados PostgreSQL sincronizado com ActiveRecord attributes.'
|
12
12
|
spec.description = 'Gem para log de tabelas.'
|
13
|
-
spec.homepage = 'https://
|
13
|
+
spec.homepage = 'https://github.com/waejl/active_pg_log'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
16
16
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
data/init.rb
ADDED
data/lib/active_pg_log.rb
CHANGED
@@ -2,118 +2,10 @@
|
|
2
2
|
|
3
3
|
require 'active_record'
|
4
4
|
module ActivePgLog
|
5
|
-
VERSION = '0.1.
|
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
|
5
|
+
VERSION = '0.1.1'
|
119
6
|
end
|
7
|
+
|
8
|
+
require 'active_pg_log/active_pg_log_table'
|
9
|
+
require 'active_pg_log/prepare'
|
10
|
+
require 'active_pg_log/trigger'
|
11
|
+
require 'active_pg_log/active_log'
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActivePgLog::ActiveLog
|
4
|
+
def self.included(clazz)
|
5
|
+
@active_trigger = ActivePgLog::ActiveTrigger.new
|
6
|
+
|
7
|
+
return if @active_trigger.include?("trigger_log_#{clazz.table_name}_insert_update", clazz.table_name)
|
8
|
+
|
9
|
+
ActiveRecord::Base.connection.execute(create_log_ddl_trigger(clazz))
|
10
|
+
end
|
11
|
+
|
12
|
+
def disable_log_ddl_trigger
|
13
|
+
ActiveRecord::Base.connection.execute("drop trigger if exists trigger_log_#{self.class.table_name}_insert_update on #{self.class.table_name} cascade;")
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def pg_logs
|
18
|
+
ActivePgLogTable.where(table_name: self.class.table_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
class << self
|
22
|
+
def create_log_ddl_trigger(clazz)
|
23
|
+
%(
|
24
|
+
drop trigger if exists trigger_log_#{clazz.table_name}_insert_update on #{clazz.table_name} cascade;
|
25
|
+
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();
|
26
|
+
)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Prepare
|
4
|
+
class << self
|
5
|
+
def destroy
|
6
|
+
return unless ActiveRecord::Base.connection.tables.include?('active_pg_log_tables')
|
7
|
+
|
8
|
+
ActiveRecord::Base.transaction do
|
9
|
+
ActiveRecord::Base.connection.execute('drop table if exists active_pg_log_tables cascade;')
|
10
|
+
ActiveRecord::Base.connection.execute('drop function if exists active_pg_loging() cascade;')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def setup
|
15
|
+
return if ActiveRecord::Base.connection.tables.include?('active_pg_log_tables')
|
16
|
+
|
17
|
+
ActiveRecord::Base.connection.transaction do
|
18
|
+
ActiveRecord::Base.connection.execute(log_ddl_table)
|
19
|
+
ActiveRecord::Base.connection.execute(log_ddl_function)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def log_ddl_table
|
24
|
+
%(
|
25
|
+
CREATE TABLE if not exists active_pg_log_tables(
|
26
|
+
id serial not null,
|
27
|
+
id_reference integer not null,
|
28
|
+
table_name character varying not null,
|
29
|
+
user_name character varying not null,
|
30
|
+
data text,
|
31
|
+
created_at timestamp without time zone default now()
|
32
|
+
);
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def log_ddl_function
|
37
|
+
%(
|
38
|
+
drop function if exists public.active_pg_loging();
|
39
|
+
CREATE OR REPLACE function public.active_pg_loging() returns trigger as $$
|
40
|
+
DECLARE
|
41
|
+
newRecord JSON;
|
42
|
+
BEGIN
|
43
|
+
if TG_OP = 'INSERT' or TG_OP = 'UPDATE' then
|
44
|
+
select row_to_json(NEW.*) into newRecord;
|
45
|
+
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());
|
46
|
+
end if;
|
47
|
+
return null;
|
48
|
+
END;
|
49
|
+
$$ language 'plpgsql';
|
50
|
+
)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class ActivePgLog::ActiveTrigger
|
2
|
+
attr_accessor :dados
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@dados = ActiveRecord::Base.connection.select_all(query)
|
6
|
+
end
|
7
|
+
|
8
|
+
def include?(trigger_name, table_name)
|
9
|
+
return false if @dados.rows.size.zero?
|
10
|
+
|
11
|
+
@dados.select { |e| e['table_name'] == table_name && e['trigger_name'] == trigger_name }.size.positive?
|
12
|
+
end
|
13
|
+
|
14
|
+
def query
|
15
|
+
%(select event_object_schema as table_schema,
|
16
|
+
event_object_table as table_name,
|
17
|
+
trigger_schema,
|
18
|
+
trigger_name,
|
19
|
+
string_agg(event_manipulation, ',') as event,
|
20
|
+
action_timing as activation,
|
21
|
+
action_condition as condition,
|
22
|
+
action_statement as definition
|
23
|
+
from information_schema.triggers
|
24
|
+
group by 1,2,3,4,6,7,8
|
25
|
+
order by table_schema,
|
26
|
+
table_name;
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_pg_log
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wesley Oliveira
|
@@ -70,17 +70,22 @@ files:
|
|
70
70
|
- active_pg_log.gemspec
|
71
71
|
- bin/console
|
72
72
|
- bin/setup
|
73
|
+
- init.rb
|
73
74
|
- lib/active_pg_log.rb
|
75
|
+
- lib/active_pg_log/active_log.rb
|
76
|
+
- lib/active_pg_log/active_pg_log_table.rb
|
77
|
+
- lib/active_pg_log/prepare.rb
|
78
|
+
- lib/active_pg_log/trigger.rb
|
74
79
|
- lib/generators/active_pg_log/install_generator.rb
|
75
80
|
- lib/generators/active_pg_log/templates/lib/tasks/active_pg_log.rake
|
76
81
|
- spec/active_pg_log_spec.rb
|
77
82
|
- spec/spec_helper.rb
|
78
|
-
homepage: https://
|
83
|
+
homepage: https://github.com/waejl/active_pg_log
|
79
84
|
licenses:
|
80
85
|
- MIT
|
81
86
|
metadata:
|
82
87
|
allowed_push_host: https://rubygems.org
|
83
|
-
homepage_uri: https://
|
88
|
+
homepage_uri: https://github.com/waejl/active_pg_log
|
84
89
|
source_code_uri: https://github.com/waejl/active_pg_log
|
85
90
|
changelog_uri: https://github.com/waejl/active_pg_log
|
86
91
|
post_install_message:
|