saseo 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/db/saseo/migrate/20151108210618_add_indexes.rb +9 -0
- data/db/saseo/schema.rb +6 -1
- data/db/saseo_source/migrate/20151028181502_initial_schema.rb +9 -3
- data/db/saseo_source/schema.rb +0 -10
- data/lib/generators/saseo/templates/add_saseo_trigger.rb.erb +1 -1
- data/lib/generators/saseo/templates/add_saseo_trigger_function.rb +19 -18
- data/lib/saseo/config/defaults.rb +1 -0
- data/lib/saseo/models/source/version.rb +1 -1
- data/lib/saseo/publishing/publisher.rb +11 -9
- data/lib/saseo/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ace1b2a4acc3bc9335423feaa6ac2683d316c05
|
4
|
+
data.tar.gz: 539126fd909b07cb23d26775b9925892d7b0d078
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2477b4227cf676ae414d8d42fbc2a306b46aa48ed53d2b14b2d9542db00d6b08093d2f597b5ee71b6fbd080d579060483d4caa4148a06038c007ad25570395bf
|
7
|
+
data.tar.gz: 28a75ce72cffcb90be7b39307b73f8230c4223b53265f64f8a7e84283b968ca02c11b9fe8ce3246c600b5932f1e9faacae1cead6caf1016c919ae2a2a80faae5
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class AddIndexes < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
add_index :saseo_versions, [:transaction_id, :table_name, :item_id], name: :saseo_transaction_item_id_idx
|
4
|
+
add_index :saseo_versions, [:table_name, :action_timestamp, :item_id], name: :saseo_item_id_idx
|
5
|
+
|
6
|
+
add_index :saseo_versions, [:transaction_id, :table_name, :item_uuid], name: :saseo_transaction_item_uuid_idx
|
7
|
+
add_index :saseo_versions, [:table_name, :action_timestamp, :item_uuid], name: :saseo_item_uuid_idx
|
8
|
+
end
|
9
|
+
end
|
data/db/saseo/schema.rb
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended that you check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
14
|
+
ActiveRecord::Schema.define(version: 20151108210618) do
|
15
15
|
|
16
16
|
# These are extensions that must be enabled in order to support this database
|
17
17
|
enable_extension "plpgsql"
|
@@ -29,4 +29,9 @@ ActiveRecord::Schema.define(version: 20151028181502) do
|
|
29
29
|
t.jsonb "new_data"
|
30
30
|
end
|
31
31
|
|
32
|
+
add_index "saseo_versions", ["table_name", "action_timestamp", "item_id"], name: "saseo_item_id_idx", using: :btree
|
33
|
+
add_index "saseo_versions", ["table_name", "action_timestamp", "item_uuid"], name: "saseo_item_uuid_idx", using: :btree
|
34
|
+
add_index "saseo_versions", ["transaction_id", "table_name", "item_id"], name: "saseo_transaction_item_id_idx", using: :btree
|
35
|
+
add_index "saseo_versions", ["transaction_id", "table_name", "item_uuid"], name: "saseo_transaction_item_uuid_idx", using: :btree
|
36
|
+
|
32
37
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
class InitialSchema < ActiveRecord::Migration
|
2
|
-
def
|
3
|
-
|
2
|
+
def up
|
4
3
|
enable_extension 'uuid-ossp'
|
4
|
+
execute 'CREATE SCHEMA saseo'
|
5
5
|
|
6
|
-
create_table 'saseo_source_versions', id: :uuid, force: true do |t|
|
6
|
+
create_table 'saseo.saseo_source_versions', id: :uuid, force: true do |t|
|
7
7
|
t.integer 'transaction_id', null: false, limit: 8 # bigint
|
8
8
|
t.string 'table_name', null: false
|
9
9
|
t.string 'action', null: false
|
@@ -13,4 +13,10 @@ class InitialSchema < ActiveRecord::Migration
|
|
13
13
|
t.jsonb 'new_data'
|
14
14
|
end
|
15
15
|
end
|
16
|
+
|
17
|
+
def down
|
18
|
+
drop_table 'saseo.saseo_source_versions'
|
19
|
+
disable_extension 'uuid-ossp'
|
20
|
+
execute 'DROP SCHEMA IF EXISTS saseo'
|
21
|
+
end
|
16
22
|
end
|
data/db/saseo_source/schema.rb
CHANGED
@@ -17,14 +17,4 @@ ActiveRecord::Schema.define(version: 20151028181502) do
|
|
17
17
|
enable_extension "plpgsql"
|
18
18
|
enable_extension "uuid-ossp"
|
19
19
|
|
20
|
-
create_table "saseo_source_versions", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t|
|
21
|
-
t.integer "transaction_id", limit: 8, null: false
|
22
|
-
t.string "table_name", null: false
|
23
|
-
t.string "action", null: false
|
24
|
-
t.datetime "action_timestamp", null: false
|
25
|
-
t.string "whodunnit", null: false
|
26
|
-
t.jsonb "old_data"
|
27
|
-
t.jsonb "new_data"
|
28
|
-
end
|
29
|
-
|
30
20
|
end
|
@@ -3,7 +3,7 @@ class AddSaseoTriggerTo<%= @table_name.camelcase %> < ActiveRecord::Migration
|
|
3
3
|
# originally copied from:
|
4
4
|
# https://wiki.postgresql.org/wiki/Audit_trigger
|
5
5
|
trigger_definition = <<SQL
|
6
|
-
CREATE TRIGGER <%= @table_name %>_saseo_trigger AFTER INSERT OR UPDATE OR DELETE ON <%= @table_name %> FOR EACH ROW EXECUTE PROCEDURE
|
6
|
+
CREATE TRIGGER <%= @table_name %>_saseo_trigger AFTER INSERT OR UPDATE OR DELETE ON <%= @table_name %> FOR EACH ROW EXECUTE PROCEDURE saseo.audit_func();
|
7
7
|
SQL
|
8
8
|
execute(trigger_definition)
|
9
9
|
end
|
@@ -4,7 +4,9 @@ class AddSaseoTriggerFunction < ActiveRecord::Migration
|
|
4
4
|
uuid_extension_sql = 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'
|
5
5
|
|
6
6
|
audit_table_sql = <<SQL
|
7
|
-
CREATE
|
7
|
+
CREATE schema saseo;
|
8
|
+
|
9
|
+
CREATE TABLE saseo.saseo_source_versions (
|
8
10
|
id uuid PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4(),
|
9
11
|
transaction_id bigint NOT NULL,
|
10
12
|
table_name text NOT NULL,
|
@@ -13,19 +15,18 @@ CREATE TABLE saseo_source_versions (
|
|
13
15
|
whodunnit text NOT NULL,
|
14
16
|
old_data jsonb,
|
15
17
|
new_data jsonb
|
16
|
-
)
|
17
|
-
SQL
|
18
|
-
|
18
|
+
);
|
19
19
|
|
20
|
+
SQL
|
20
21
|
# originally copied from:
|
21
22
|
# https://wiki.postgresql.org/wiki/Audit_trigger
|
22
23
|
#
|
23
24
|
# modified to use NOTIFY
|
24
25
|
#
|
25
26
|
# a trigger must be added to every table
|
26
|
-
# CREATE TRIGGER identities_saseo_trigger AFTER INSERT OR UPDATE OR DELETE ON identities FOR EACH ROW EXECUTE PROCEDURE
|
27
|
+
# CREATE TRIGGER identities_saseo_trigger AFTER INSERT OR UPDATE OR DELETE ON identities FOR EACH ROW EXECUTE PROCEDURE saseo.audit_func();
|
27
28
|
whodunnit_sql = <<SQL
|
28
|
-
CREATE OR REPLACE FUNCTION
|
29
|
+
CREATE OR REPLACE FUNCTION saseo.whodunnit() RETURNS TEXT AS $body$
|
29
30
|
BEGIN
|
30
31
|
return current_setting('saseo.whodunnit');
|
31
32
|
EXCEPTION WHEN undefined_object THEN
|
@@ -35,7 +36,7 @@ $body$
|
|
35
36
|
LANGUAGE plpgsql
|
36
37
|
SQL
|
37
38
|
audit_sql = <<SQL
|
38
|
-
CREATE OR REPLACE FUNCTION
|
39
|
+
CREATE OR REPLACE FUNCTION saseo.audit_func() RETURNS TRIGGER AS $body$
|
39
40
|
DECLARE
|
40
41
|
v_old_data json;
|
41
42
|
v_new_data json;
|
@@ -43,7 +44,7 @@ DECLARE
|
|
43
44
|
v_channel text;
|
44
45
|
v_audit_uuid uuid;
|
45
46
|
BEGIN
|
46
|
-
v_whodunnit :=
|
47
|
+
v_whodunnit := saseo.whodunnit();
|
47
48
|
v_audit_uuid := uuid_generate_v4();
|
48
49
|
|
49
50
|
v_channel := upper('SASEO' || '_' || TG_TABLE_NAME::TEXT || '_' || TG_OP || '_AUDIT');
|
@@ -52,38 +53,38 @@ BEGIN
|
|
52
53
|
v_old_data := row_to_json(OLD);
|
53
54
|
v_new_data := row_to_json(NEW);
|
54
55
|
|
55
|
-
INSERT INTO saseo_source_versions VALUES(v_audit_uuid, txid_current(), TG_TABLE_NAME, DEFAULT, TG_OP, v_whodunnit, v_old_data::JSONB, v_new_data::JSONB);
|
56
|
+
INSERT INTO saseo.saseo_source_versions VALUES(v_audit_uuid, txid_current(), TG_TABLE_NAME, DEFAULT, TG_OP, v_whodunnit, v_old_data::JSONB, v_new_data::JSONB);
|
56
57
|
PERFORM pg_notify(v_channel, v_audit_uuid::TEXT);
|
57
58
|
|
58
59
|
RETURN NEW;
|
59
60
|
ELSIF (TG_OP = 'DELETE') THEN
|
60
61
|
v_old_data := row_to_json(OLD);
|
61
62
|
|
62
|
-
INSERT INTO saseo_source_versions VALUES(v_audit_uuid, txid_current(), TG_TABLE_NAME, DEFAULT, TG_OP, v_whodunnit, v_old_data::JSONB, NULL);
|
63
|
+
INSERT INTO saseo.saseo_source_versions VALUES(v_audit_uuid, txid_current(), TG_TABLE_NAME, DEFAULT, TG_OP, v_whodunnit, v_old_data::JSONB, NULL);
|
63
64
|
PERFORM pg_notify(v_channel, v_audit_uuid::TEXT);
|
64
65
|
|
65
66
|
RETURN OLD;
|
66
67
|
ELSIF (TG_OP = 'INSERT') THEN
|
67
68
|
v_new_data := row_to_json(NEW);
|
68
69
|
|
69
|
-
INSERT INTO saseo_source_versions VALUES(v_audit_uuid, txid_current(), TG_TABLE_NAME, DEFAULT, TG_OP, v_whodunnit, NULL, v_new_data::JSONB);
|
70
|
+
INSERT INTO saseo.saseo_source_versions VALUES(v_audit_uuid, txid_current(), TG_TABLE_NAME, DEFAULT, TG_OP, v_whodunnit, NULL, v_new_data::JSONB);
|
70
71
|
PERFORM pg_notify(v_channel, v_audit_uuid::TEXT);
|
71
72
|
|
72
73
|
RETURN NEW;
|
73
74
|
ELSE
|
74
|
-
RAISE WARNING '[
|
75
|
+
RAISE WARNING '[SASEO.AUDIT_FUNC] - Other action occurred: %, at %',TG_OP,now();
|
75
76
|
RETURN NULL;
|
76
77
|
END IF;
|
77
78
|
|
78
79
|
EXCEPTION
|
79
80
|
WHEN data_exception THEN
|
80
|
-
RAISE WARNING '[
|
81
|
+
RAISE WARNING '[SASEO.AUDIT_FUNC] - UDF ERROR [DATA EXCEPTION] - SQLSTATE: %, SQLERRM: %',SQLSTATE,SQLERRM;
|
81
82
|
RETURN NULL;
|
82
83
|
WHEN unique_violation THEN
|
83
|
-
RAISE WARNING '[
|
84
|
+
RAISE WARNING '[SASEO.AUDIT_FUNC] - UDF ERROR [UNIQUE] - SQLSTATE: %, SQLERRM: %',SQLSTATE,SQLERRM;
|
84
85
|
RETURN NULL;
|
85
86
|
WHEN OTHERS THEN
|
86
|
-
RAISE WARNING '[
|
87
|
+
RAISE WARNING '[SASEO.AUDIT_FUNC] - UDF ERROR [OTHER] - SQLSTATE: %, SQLERRM: %',SQLSTATE,SQLERRM;
|
87
88
|
RETURN NULL;
|
88
89
|
END;
|
89
90
|
$body$
|
@@ -96,9 +97,9 @@ SQL
|
|
96
97
|
end
|
97
98
|
|
98
99
|
def down
|
99
|
-
execute('DROP FUNCTION IF EXISTS
|
100
|
-
execute('DROP FUNCTION IF EXISTS
|
101
|
-
execute('DROP TABLE IF EXISTS saseo_source_versions CASCADE')
|
100
|
+
execute('DROP FUNCTION IF EXISTS saseo.audit_func() CASCADE')
|
101
|
+
execute('DROP FUNCTION IF EXISTS saseo.whodunnit() CASCADE')
|
102
|
+
execute('DROP TABLE IF EXISTS saseo.saseo_source_versions CASCADE')
|
102
103
|
execute('DROP EXTENSION IF EXISTS "uuid-ossp" CASCADE')
|
103
104
|
|
104
105
|
end
|
@@ -5,7 +5,7 @@ module Saseo
|
|
5
5
|
module Models
|
6
6
|
module Source
|
7
7
|
class Version < Saseo::Models::Source::Base
|
8
|
-
self.table_name = Saseo.config.source_table_name
|
8
|
+
self.table_name = "#{Saseo.config.source_table_schema}.#{Saseo.config.source_table_name}"
|
9
9
|
|
10
10
|
validates :transaction_id, presence: true
|
11
11
|
validates :table_name, presence: true
|
@@ -21,7 +21,7 @@ module Saseo
|
|
21
21
|
|
22
22
|
def channels=(val)
|
23
23
|
if val == :all
|
24
|
-
tables =
|
24
|
+
tables = db_connection.tables
|
25
25
|
|
26
26
|
@channels = tables.reduce([]) do |channels, table|
|
27
27
|
%w[insert update delete].each do |operation|
|
@@ -38,19 +38,21 @@ module Saseo
|
|
38
38
|
self.channels = channels
|
39
39
|
listen channels: self.channels, loop: loop, timeout: timeout do |channel, pid, payload|
|
40
40
|
uuid = payload
|
41
|
-
logger.debug { "received NOTIFY for source version: #{uuid}"}
|
42
|
-
Saseo::Models::Source::Base.transaction do
|
41
|
+
logger.debug { "received NOTIFY for source version: #{uuid}" }
|
43
42
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
43
|
+
record = Saseo::Models::Source::Version.find(uuid)
|
44
|
+
Saseo::Publishing::DataChangeMessage.publish(record)
|
45
|
+
record.delete
|
46
|
+
logger.debug { "deleted source version: #{uuid}" }
|
49
47
|
end
|
50
48
|
end
|
51
49
|
|
50
|
+
def db_connection
|
51
|
+
Saseo::Models::Source::Base.connection
|
52
|
+
end
|
53
|
+
|
52
54
|
def notify(channel, payload)
|
53
|
-
conn =
|
55
|
+
conn = db_connection.instance_variable_get(:@connection)
|
54
56
|
conn.async_exec "SELECT pg_notify('#{channel}', '#{payload.to_s}')"
|
55
57
|
end
|
56
58
|
|
data/lib/saseo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: saseo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Keyes
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: awesome_print
|
@@ -201,6 +201,7 @@ files:
|
|
201
201
|
- bin/console
|
202
202
|
- bin/setup
|
203
203
|
- db/saseo/migrate/20151028181502_initial_schema.rb
|
204
|
+
- db/saseo/migrate/20151108210618_add_indexes.rb
|
204
205
|
- db/saseo/schema.rb
|
205
206
|
- db/saseo_source/migrate/20151028181502_initial_schema.rb
|
206
207
|
- db/saseo_source/schema.rb
|