waldit 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/waldit/migration.rb +64 -0
- data/lib/waldit/railtie.rb +10 -5
- data/lib/waldit/sidekiq.rb +7 -2
- data/lib/waldit/version.rb +1 -1
- data/lib/waldit/watcher.rb +12 -7
- data/lib/waldit.rb +3 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8dd9fb06d63894b50126cf5b577ef2c0ab62f58ffc095632ad26ee18f8f9d86e
|
4
|
+
data.tar.gz: 4d99542390d1a30ba64055515bcf9e30d5c83351fd402fe0e933f96a9a37425e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28290886fa42d5ef259186281f73dd3f6539f561adef547c458215127fd40253d374ffc67a3e48db9ec32e47317676cc173cea9a52bd1d569e715c7831d3b84d
|
7
|
+
data.tar.gz: ce9ef4fc3d0df78721537c55226f57049f28dedaa7b7d1f072b74c139a0fdb7c2ab2918c500a6666645517d2be0f9a27c9f4a86331b4900ad0a3509fd0050344
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Waldit
|
4
|
+
module Migration
|
5
|
+
def create_waldit_table(name = "waldit", &block)
|
6
|
+
reversible do |dir|
|
7
|
+
dir.up { execute "CREATE TYPE waldit_action AS ENUM ('insert', 'update', 'delete')" }
|
8
|
+
dir.down { execute "DROP TYPE waldit_action" }
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table :waldit do |t|
|
12
|
+
t.column :action, :waldit_action, null: false
|
13
|
+
t.string :table_name, null: false
|
14
|
+
t.string :primary_key
|
15
|
+
t.bigint :transaction_id, null: false
|
16
|
+
t.decimal :lsn, null: false, precision: 20, scale: 0
|
17
|
+
t.timestamptz :commited_at
|
18
|
+
t.jsonb :context, null: false, default: {}
|
19
|
+
t.jsonb :old, null: true
|
20
|
+
t.jsonb :new, null: true
|
21
|
+
t.jsonb :diff, null: true
|
22
|
+
block.call(t)
|
23
|
+
end
|
24
|
+
|
25
|
+
add_index :waldit, [:table_name, :primary_key, :transaction_id], unique: true
|
26
|
+
add_index :waldit, [:transaction_id, :lsn]
|
27
|
+
add_index :waldit, :commited_at
|
28
|
+
add_index :waldit, :context, using: :gin, opclass: :jsonb_path_ops
|
29
|
+
end
|
30
|
+
|
31
|
+
def create_waldit_publication
|
32
|
+
reversible do |dir|
|
33
|
+
dir.up { execute "CREATE PUBLICATION waldit" }
|
34
|
+
dir.down { execute "DROP PUBLICATION waldit" }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_table_to_waldit(table)
|
39
|
+
reversible do |dir|
|
40
|
+
dir.up do
|
41
|
+
execute "ALTER TABLE #{table} REPLICA IDENTITY FULL"
|
42
|
+
execute "ALTER PUBLICATION waldit ADD TABLE #{table}"
|
43
|
+
end
|
44
|
+
dir.down do
|
45
|
+
execute "ALTER PUBLICATION waldit DROP TABLE #{table}"
|
46
|
+
execute "ALTER TABLE #{table} REPLICA IDENTITY DEFAULT"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def remove_table_from_waldit(table)
|
52
|
+
reversible do |dir|
|
53
|
+
dir.up do
|
54
|
+
execute "ALTER PUBLICATION waldit DROP TABLE #{table}"
|
55
|
+
execute "ALTER TABLE #{table} REPLICA IDENTITY DEFAULT"
|
56
|
+
end
|
57
|
+
dir.down do
|
58
|
+
execute "ALTER TABLE #{table} REPLICA IDENTITY FULL"
|
59
|
+
execute "ALTER PUBLICATION waldit ADD TABLE #{table}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/waldit/railtie.rb
CHANGED
@@ -5,11 +5,16 @@ require "rails/railtie"
|
|
5
5
|
module Waldit
|
6
6
|
class Railtie < Rails::Railtie
|
7
7
|
config.before_configuration do
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
["waldit", "postgresqlwaldit"].each do |adapter_name|
|
9
|
+
ActiveRecord::ConnectionAdapters.register(
|
10
|
+
adapter_name,
|
11
|
+
"Waldit::PostgreSQLAdapter",
|
12
|
+
"waldit/postgresql_adapter",
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
require_relative "migration"
|
17
|
+
ActiveRecord::Migration.include Waldit::Migration
|
13
18
|
end
|
14
19
|
end
|
15
20
|
end
|
data/lib/waldit/sidekiq.rb
CHANGED
@@ -17,8 +17,13 @@ module Waldit
|
|
17
17
|
include ::Sidekiq::ServerMiddleware
|
18
18
|
|
19
19
|
def call(job_instance, job, queue, &block)
|
20
|
-
|
21
|
-
|
20
|
+
background_job = case job["class"]
|
21
|
+
in "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"
|
22
|
+
job["args"][0]["job_class"]
|
23
|
+
in klass
|
24
|
+
klass
|
25
|
+
end
|
26
|
+
Waldit.with_context((deserialize_context(job) || {}).merge(background_job:), &block)
|
22
27
|
end
|
23
28
|
|
24
29
|
private
|
data/lib/waldit/version.rb
CHANGED
data/lib/waldit/watcher.rb
CHANGED
@@ -44,14 +44,19 @@ module Waldit
|
|
44
44
|
when CommitTransactionEvent
|
45
45
|
record.where(transaction_id: event.transaction_id).update_all(commited_at: event.timestamp)
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
47
|
+
changes = [:old, :new, :diff]
|
48
|
+
.map { |diff| [diff, tables.filter { |table| Waldit.store_changes.call(table).include? diff }] }
|
49
|
+
.to_h
|
50
|
+
|
51
|
+
log_new = (changes[:new] || []).map { |table| "'#{table}'" }.join(",")
|
52
|
+
log_old = (changes[:old] || []).map { |table| "'#{table}'" }.join(",")
|
53
|
+
log_diff = (changes[:diff] || []).map { |table| "'#{table}'" }.join(",")
|
54
|
+
|
55
|
+
record.where(transaction_id: event.transaction_id, action: "update").update_all(<<~SQL)
|
56
|
+
new = CASE WHEN table_name = ANY (ARRAY[#{log_new}]::varchar[]) THEN new ELSE null END,
|
57
|
+
old = CASE WHEN table_name = ANY (ARRAY[#{log_old}]::varchar[]) THEN old ELSE null END,
|
53
58
|
diff =
|
54
|
-
CASE WHEN table_name = ANY (ARRAY[#{log_diff
|
59
|
+
CASE WHEN table_name = ANY (ARRAY[#{log_diff}]::varchar[]) THEN (
|
55
60
|
SELECT
|
56
61
|
jsonb_object_agg(
|
57
62
|
coalesce(old_kv.key, new_kv.key),
|
data/lib/waldit.rb
CHANGED
@@ -27,10 +27,10 @@ module Waldit
|
|
27
27
|
case changes
|
28
28
|
when Symbol
|
29
29
|
changes = [changes].to_set
|
30
|
-
@store_changes = -> { changes }
|
30
|
+
@store_changes = -> table { changes }
|
31
31
|
when Array
|
32
32
|
changes = changes.map(&:to_sym).to_set
|
33
|
-
@store_changes = -> { changes }
|
33
|
+
@store_changes = -> table { changes }
|
34
34
|
else
|
35
35
|
@store_changes = changes
|
36
36
|
end
|
@@ -51,7 +51,7 @@ module Waldit
|
|
51
51
|
|
52
52
|
config.watched_tables = -> table { table != "waldit" }
|
53
53
|
|
54
|
-
config.store_changes = -> table { %i[old new
|
54
|
+
config.store_changes = -> table { %i[old new] }
|
55
55
|
|
56
56
|
config.ignored_columns = -> table { %w[created_at updated_at] }
|
57
57
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: waldit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Navarro
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-08-
|
10
|
+
date: 2025-08-22 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: wal
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- Rakefile
|
65
65
|
- lib/waldit.rb
|
66
66
|
- lib/waldit/context.rb
|
67
|
+
- lib/waldit/migration.rb
|
67
68
|
- lib/waldit/postgresql_adapter.rb
|
68
69
|
- lib/waldit/railtie.rb
|
69
70
|
- lib/waldit/record.rb
|