table_sync 4.2.1 → 6.0
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/.github/workflows/ci.yml +52 -0
- data/CHANGELOG.md +75 -9
- data/Gemfile.lock +159 -152
- data/README.md +4 -1
- data/docs/message_protocol.md +3 -3
- data/docs/publishing/configuration.md +143 -0
- data/docs/publishing/manual.md +155 -0
- data/docs/publishing/publishers.md +162 -0
- data/docs/publishing.md +35 -119
- data/docs/receiving.md +11 -6
- data/lib/table_sync/errors.rb +31 -22
- data/lib/table_sync/event.rb +35 -0
- data/lib/table_sync/orm_adapter/active_record.rb +25 -0
- data/lib/table_sync/orm_adapter/base.rb +92 -0
- data/lib/table_sync/orm_adapter/sequel.rb +29 -0
- data/lib/table_sync/publishing/batch.rb +53 -0
- data/lib/table_sync/publishing/data/objects.rb +50 -0
- data/lib/table_sync/publishing/data/raw.rb +27 -0
- data/lib/table_sync/publishing/helpers/attributes.rb +63 -0
- data/lib/table_sync/publishing/helpers/debounce.rb +134 -0
- data/lib/table_sync/publishing/helpers/objects.rb +39 -0
- data/lib/table_sync/publishing/message/base.rb +73 -0
- data/lib/table_sync/publishing/message/batch.rb +14 -0
- data/lib/table_sync/publishing/message/raw.rb +54 -0
- data/lib/table_sync/publishing/message/single.rb +13 -0
- data/lib/table_sync/publishing/params/base.rb +66 -0
- data/lib/table_sync/publishing/params/batch.rb +23 -0
- data/lib/table_sync/publishing/params/raw.rb +7 -0
- data/lib/table_sync/publishing/params/single.rb +31 -0
- data/lib/table_sync/publishing/raw.rb +21 -0
- data/lib/table_sync/publishing/single.rb +65 -0
- data/lib/table_sync/publishing.rb +20 -5
- data/lib/table_sync/receiving/config.rb +6 -4
- data/lib/table_sync/receiving/handler.rb +25 -9
- data/lib/table_sync/receiving/model/active_record.rb +1 -1
- data/lib/table_sync/receiving.rb +0 -2
- data/lib/table_sync/setup/active_record.rb +22 -0
- data/lib/table_sync/setup/base.rb +67 -0
- data/lib/table_sync/setup/sequel.rb +26 -0
- data/lib/table_sync/utils/interface_checker.rb +2 -2
- data/lib/table_sync/version.rb +1 -1
- data/lib/table_sync.rb +31 -8
- data/table_sync.gemspec +7 -7
- metadata +58 -37
- data/.travis.yml +0 -34
- data/lib/table_sync/publishing/base_publisher.rb +0 -114
- data/lib/table_sync/publishing/batch_publisher.rb +0 -109
- data/lib/table_sync/publishing/orm_adapter/active_record.rb +0 -32
- data/lib/table_sync/publishing/orm_adapter/sequel.rb +0 -57
- data/lib/table_sync/publishing/publisher.rb +0 -129
data/lib/table_sync.rb
CHANGED
@@ -8,39 +8,62 @@ require "active_support/core_ext/object/blank"
|
|
8
8
|
require "active_support/core_ext/numeric/time"
|
9
9
|
|
10
10
|
module TableSync
|
11
|
+
require_relative "table_sync/event"
|
11
12
|
require_relative "table_sync/utils"
|
12
13
|
require_relative "table_sync/version"
|
13
14
|
require_relative "table_sync/errors"
|
15
|
+
|
14
16
|
require_relative "table_sync/instrument"
|
15
17
|
require_relative "table_sync/instrument_adapter/active_support"
|
18
|
+
|
16
19
|
require_relative "table_sync/naming_resolver/active_record"
|
17
20
|
require_relative "table_sync/naming_resolver/sequel"
|
21
|
+
|
22
|
+
require_relative "table_sync/orm_adapter/base"
|
23
|
+
require_relative "table_sync/orm_adapter/active_record"
|
24
|
+
require_relative "table_sync/orm_adapter/sequel"
|
25
|
+
|
18
26
|
require_relative "table_sync/receiving"
|
19
27
|
require_relative "table_sync/publishing"
|
20
28
|
|
29
|
+
require_relative "table_sync/setup/base"
|
30
|
+
require_relative "table_sync/setup/active_record"
|
31
|
+
require_relative "table_sync/setup/sequel"
|
32
|
+
|
21
33
|
class << self
|
22
|
-
attr_accessor :
|
34
|
+
attr_accessor :raise_on_empty_message
|
35
|
+
attr_accessor :single_publishing_job_class_callable
|
23
36
|
attr_accessor :batch_publishing_job_class_callable
|
24
37
|
attr_accessor :routing_key_callable
|
25
38
|
attr_accessor :exchange_name
|
26
|
-
attr_accessor :
|
39
|
+
attr_accessor :headers_callable
|
27
40
|
attr_accessor :notifier
|
41
|
+
|
28
42
|
attr_reader :orm
|
29
43
|
attr_reader :publishing_adapter
|
30
44
|
attr_reader :receiving_model
|
45
|
+
attr_reader :setup
|
31
46
|
|
32
|
-
def sync(
|
33
|
-
|
47
|
+
def sync(object_class, **options)
|
48
|
+
setup.new(
|
49
|
+
object_class: object_class,
|
50
|
+
on: options[:on],
|
51
|
+
if_condition: options[:if],
|
52
|
+
unless_condition: options[:unless],
|
53
|
+
debounce_time: options[:debounce_time],
|
54
|
+
).register_callbacks
|
34
55
|
end
|
35
56
|
|
36
57
|
def orm=(val)
|
37
58
|
case val
|
38
59
|
when :active_record
|
39
|
-
@publishing_adapter =
|
40
|
-
@receiving_model
|
60
|
+
@publishing_adapter = TableSync::ORMAdapter::ActiveRecord
|
61
|
+
@receiving_model = Receiving::Model::ActiveRecord
|
62
|
+
@setup = TableSync::Setup::ActiveRecord
|
41
63
|
when :sequel
|
42
|
-
@publishing_adapter =
|
43
|
-
@receiving_model
|
64
|
+
@publishing_adapter = TableSync::ORMAdapter::Sequel
|
65
|
+
@receiving_model = Receiving::Model::Sequel
|
66
|
+
@setup = TableSync::Setup::Sequel
|
44
67
|
else
|
45
68
|
raise ORMNotSupported.new(val.inspect)
|
46
69
|
end
|
data/table_sync.gemspec
CHANGED
@@ -27,18 +27,18 @@ Gem::Specification.new do |spec|
|
|
27
27
|
end
|
28
28
|
|
29
29
|
spec.add_runtime_dependency "memery"
|
30
|
-
spec.add_runtime_dependency "rabbit_messaging"
|
30
|
+
spec.add_runtime_dependency "rabbit_messaging"
|
31
31
|
spec.add_runtime_dependency "rails"
|
32
32
|
spec.add_runtime_dependency "self_data"
|
33
33
|
|
34
|
-
spec.add_development_dependency "
|
35
|
-
spec.add_development_dependency "rspec", "~> 3.8"
|
34
|
+
spec.add_development_dependency "rspec"
|
36
35
|
spec.add_development_dependency "rubocop-config-umbrellio"
|
37
|
-
spec.add_development_dependency "simplecov"
|
36
|
+
spec.add_development_dependency "simplecov"
|
37
|
+
spec.add_development_dependency "simplecov-lcov"
|
38
38
|
|
39
|
-
spec.add_development_dependency "activejob"
|
40
|
-
spec.add_development_dependency "activerecord"
|
41
|
-
spec.add_development_dependency "pg"
|
39
|
+
spec.add_development_dependency "activejob"
|
40
|
+
spec.add_development_dependency "activerecord"
|
41
|
+
spec.add_development_dependency "pg"
|
42
42
|
spec.add_development_dependency "sequel"
|
43
43
|
spec.add_development_dependency "timecop"
|
44
44
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: table_sync
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: '6.0'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Umbrellio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: memery
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: rabbit_messaging
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0
|
33
|
+
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,35 +67,35 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0
|
75
|
+
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0
|
82
|
+
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: rubocop-config-umbrellio
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - "
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - "
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
98
|
+
name: simplecov
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
@@ -109,61 +109,61 @@ dependencies:
|
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
|
-
name: simplecov
|
112
|
+
name: simplecov-lcov
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- - "
|
115
|
+
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: '0
|
117
|
+
version: '0'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- - "
|
122
|
+
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version: '0
|
124
|
+
version: '0'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: activejob
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
128
128
|
requirements:
|
129
129
|
- - ">="
|
130
130
|
- !ruby/object:Gem::Version
|
131
|
-
version: '
|
131
|
+
version: '0'
|
132
132
|
type: :development
|
133
133
|
prerelease: false
|
134
134
|
version_requirements: !ruby/object:Gem::Requirement
|
135
135
|
requirements:
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
|
-
version: '
|
138
|
+
version: '0'
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
140
|
name: activerecord
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
143
|
- - ">="
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version: '
|
145
|
+
version: '0'
|
146
146
|
type: :development
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
150
|
- - ">="
|
151
151
|
- !ruby/object:Gem::Version
|
152
|
-
version: '
|
152
|
+
version: '0'
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: pg
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|
156
156
|
requirements:
|
157
|
-
- - "
|
157
|
+
- - ">="
|
158
158
|
- !ruby/object:Gem::Version
|
159
|
-
version: '0
|
159
|
+
version: '0'
|
160
160
|
type: :development
|
161
161
|
prerelease: false
|
162
162
|
version_requirements: !ruby/object:Gem::Requirement
|
163
163
|
requirements:
|
164
|
-
- - "
|
164
|
+
- - ">="
|
165
165
|
- !ruby/object:Gem::Version
|
166
|
-
version: '0
|
166
|
+
version: '0'
|
167
167
|
- !ruby/object:Gem::Dependency
|
168
168
|
name: sequel
|
169
169
|
requirement: !ruby/object:Gem::Requirement
|
@@ -256,10 +256,10 @@ executables: []
|
|
256
256
|
extensions: []
|
257
257
|
extra_rdoc_files: []
|
258
258
|
files:
|
259
|
+
- ".github/workflows/ci.yml"
|
259
260
|
- ".gitignore"
|
260
261
|
- ".rspec"
|
261
262
|
- ".rubocop.yml"
|
262
|
-
- ".travis.yml"
|
263
263
|
- CHANGELOG.md
|
264
264
|
- Gemfile
|
265
265
|
- Gemfile.lock
|
@@ -271,19 +271,37 @@ files:
|
|
271
271
|
- docs/message_protocol.md
|
272
272
|
- docs/notifications.md
|
273
273
|
- docs/publishing.md
|
274
|
+
- docs/publishing/configuration.md
|
275
|
+
- docs/publishing/manual.md
|
276
|
+
- docs/publishing/publishers.md
|
274
277
|
- docs/receiving.md
|
275
278
|
- lib/table_sync.rb
|
276
279
|
- lib/table_sync/errors.rb
|
280
|
+
- lib/table_sync/event.rb
|
277
281
|
- lib/table_sync/instrument.rb
|
278
282
|
- lib/table_sync/instrument_adapter/active_support.rb
|
279
283
|
- lib/table_sync/naming_resolver/active_record.rb
|
280
284
|
- lib/table_sync/naming_resolver/sequel.rb
|
285
|
+
- lib/table_sync/orm_adapter/active_record.rb
|
286
|
+
- lib/table_sync/orm_adapter/base.rb
|
287
|
+
- lib/table_sync/orm_adapter/sequel.rb
|
281
288
|
- lib/table_sync/publishing.rb
|
282
|
-
- lib/table_sync/publishing/
|
283
|
-
- lib/table_sync/publishing/
|
284
|
-
- lib/table_sync/publishing/
|
285
|
-
- lib/table_sync/publishing/
|
286
|
-
- lib/table_sync/publishing/
|
289
|
+
- lib/table_sync/publishing/batch.rb
|
290
|
+
- lib/table_sync/publishing/data/objects.rb
|
291
|
+
- lib/table_sync/publishing/data/raw.rb
|
292
|
+
- lib/table_sync/publishing/helpers/attributes.rb
|
293
|
+
- lib/table_sync/publishing/helpers/debounce.rb
|
294
|
+
- lib/table_sync/publishing/helpers/objects.rb
|
295
|
+
- lib/table_sync/publishing/message/base.rb
|
296
|
+
- lib/table_sync/publishing/message/batch.rb
|
297
|
+
- lib/table_sync/publishing/message/raw.rb
|
298
|
+
- lib/table_sync/publishing/message/single.rb
|
299
|
+
- lib/table_sync/publishing/params/base.rb
|
300
|
+
- lib/table_sync/publishing/params/batch.rb
|
301
|
+
- lib/table_sync/publishing/params/raw.rb
|
302
|
+
- lib/table_sync/publishing/params/single.rb
|
303
|
+
- lib/table_sync/publishing/raw.rb
|
304
|
+
- lib/table_sync/publishing/single.rb
|
287
305
|
- lib/table_sync/receiving.rb
|
288
306
|
- lib/table_sync/receiving/config.rb
|
289
307
|
- lib/table_sync/receiving/config_decorator.rb
|
@@ -291,6 +309,9 @@ files:
|
|
291
309
|
- lib/table_sync/receiving/handler.rb
|
292
310
|
- lib/table_sync/receiving/model/active_record.rb
|
293
311
|
- lib/table_sync/receiving/model/sequel.rb
|
312
|
+
- lib/table_sync/setup/active_record.rb
|
313
|
+
- lib/table_sync/setup/base.rb
|
314
|
+
- lib/table_sync/setup/sequel.rb
|
294
315
|
- lib/table_sync/utils.rb
|
295
316
|
- lib/table_sync/utils/interface_checker.rb
|
296
317
|
- lib/table_sync/utils/proc_array.rb
|
@@ -317,7 +338,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
317
338
|
- !ruby/object:Gem::Version
|
318
339
|
version: '0'
|
319
340
|
requirements: []
|
320
|
-
rubygems_version: 3.2.
|
341
|
+
rubygems_version: 3.2.3
|
321
342
|
signing_key:
|
322
343
|
specification_version: 4
|
323
344
|
summary: DB Table synchronization between microservices based on Model's event system
|
data/.travis.yml
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
|
3
|
-
rvm:
|
4
|
-
- 2.5
|
5
|
-
- 2.6
|
6
|
-
- 2.7
|
7
|
-
- ruby-head
|
8
|
-
|
9
|
-
matrix:
|
10
|
-
fast_finish: true
|
11
|
-
allow_failures:
|
12
|
-
- rvm: ruby-head
|
13
|
-
|
14
|
-
sudo: false
|
15
|
-
|
16
|
-
dist: xenial
|
17
|
-
|
18
|
-
cache: bundler
|
19
|
-
|
20
|
-
services:
|
21
|
-
- postgresql
|
22
|
-
|
23
|
-
addons:
|
24
|
-
postgresql: "10"
|
25
|
-
|
26
|
-
before_install: gem install bundler
|
27
|
-
|
28
|
-
before_script:
|
29
|
-
- psql -c 'create database table_sync_test;' -U postgres
|
30
|
-
|
31
|
-
script:
|
32
|
-
- bundle exec rake bundle:audit
|
33
|
-
- bundle exec rubocop
|
34
|
-
- bundle exec rspec
|
@@ -1,114 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class TableSync::Publishing::BasePublisher
|
4
|
-
include Memery
|
5
|
-
|
6
|
-
BASE_SAFE_JSON_TYPES = [NilClass, String, TrueClass, FalseClass, Numeric, Symbol].freeze
|
7
|
-
NOT_MAPPED = Object.new
|
8
|
-
|
9
|
-
private
|
10
|
-
|
11
|
-
attr_accessor :object_class
|
12
|
-
|
13
|
-
# @!method job_callable
|
14
|
-
# @!method job_callable_error_message
|
15
|
-
# @!method attrs_for_callables
|
16
|
-
# @!method attrs_for_routing_key
|
17
|
-
# @!method attrs_for_metadata
|
18
|
-
# @!method attributes_for_sync
|
19
|
-
|
20
|
-
memoize def current_time
|
21
|
-
Time.current
|
22
|
-
end
|
23
|
-
|
24
|
-
memoize def primary_keys
|
25
|
-
Array(object_class.primary_key).map(&:to_sym)
|
26
|
-
end
|
27
|
-
|
28
|
-
memoize def attributes_for_sync_defined?
|
29
|
-
object_class.method_defined?(:attributes_for_sync)
|
30
|
-
end
|
31
|
-
|
32
|
-
memoize def attrs_for_routing_key_defined?
|
33
|
-
object_class.method_defined?(:attrs_for_routing_key)
|
34
|
-
end
|
35
|
-
|
36
|
-
memoize def attrs_for_metadata_defined?
|
37
|
-
object_class.method_defined?(:attrs_for_metadata)
|
38
|
-
end
|
39
|
-
|
40
|
-
def resolve_routing_key
|
41
|
-
routing_key_callable.call(object_class.name, attrs_for_routing_key)
|
42
|
-
end
|
43
|
-
|
44
|
-
def metadata
|
45
|
-
TableSync.routing_metadata_callable&.call(object_class.name, attrs_for_metadata)
|
46
|
-
end
|
47
|
-
|
48
|
-
def confirm?
|
49
|
-
@confirm
|
50
|
-
end
|
51
|
-
|
52
|
-
def routing_key_callable
|
53
|
-
return TableSync.routing_key_callable if TableSync.routing_key_callable
|
54
|
-
raise "Can't publish, set TableSync.routing_key_callable"
|
55
|
-
end
|
56
|
-
|
57
|
-
def filter_safe_for_serialization(object)
|
58
|
-
case object
|
59
|
-
when Array
|
60
|
-
object.map(&method(:filter_safe_for_serialization)).select(&method(:object_mapped?))
|
61
|
-
when Hash
|
62
|
-
object
|
63
|
-
.transform_keys(&method(:filter_safe_for_serialization))
|
64
|
-
.transform_values(&method(:filter_safe_hash_values))
|
65
|
-
.select { |*objects| objects.all?(&method(:object_mapped?)) }
|
66
|
-
when Float::INFINITY
|
67
|
-
NOT_MAPPED
|
68
|
-
when *BASE_SAFE_JSON_TYPES
|
69
|
-
object
|
70
|
-
else
|
71
|
-
NOT_MAPPED
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def filter_safe_hash_values(value)
|
76
|
-
case value
|
77
|
-
when Symbol
|
78
|
-
value.to_s
|
79
|
-
else
|
80
|
-
filter_safe_for_serialization(value)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def object_mapped?(object)
|
85
|
-
object != NOT_MAPPED
|
86
|
-
end
|
87
|
-
|
88
|
-
def job_class
|
89
|
-
job_callable ? job_callable.call : raise(job_callable_error_message)
|
90
|
-
end
|
91
|
-
|
92
|
-
def publishing_data
|
93
|
-
{
|
94
|
-
model: object_class.try(:table_sync_model_name) || object_class.name,
|
95
|
-
attributes: attributes_for_sync,
|
96
|
-
version: current_time.to_f,
|
97
|
-
}
|
98
|
-
end
|
99
|
-
|
100
|
-
def params
|
101
|
-
params = {
|
102
|
-
event: :table_sync,
|
103
|
-
data: publishing_data,
|
104
|
-
confirm_select: confirm?,
|
105
|
-
routing_key: routing_key,
|
106
|
-
realtime: true,
|
107
|
-
headers: metadata,
|
108
|
-
}
|
109
|
-
|
110
|
-
params[:exchange_name] = TableSync.exchange_name if TableSync.exchange_name
|
111
|
-
|
112
|
-
params
|
113
|
-
end
|
114
|
-
end
|
@@ -1,109 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class TableSync::Publishing::BatchPublisher < TableSync::Publishing::BasePublisher
|
4
|
-
def initialize(object_class, original_attributes_array, **options)
|
5
|
-
@original_attributes_array = original_attributes_array.map do |hash|
|
6
|
-
filter_safe_for_serialization(hash.deep_symbolize_keys)
|
7
|
-
end
|
8
|
-
|
9
|
-
@object_class = object_class.constantize
|
10
|
-
@confirm = options[:confirm] || true
|
11
|
-
@routing_key = options[:routing_key] || resolve_routing_key
|
12
|
-
@push_original_attributes = options[:push_original_attributes] || false
|
13
|
-
@headers = options[:headers]
|
14
|
-
@event = options[:event] || :update
|
15
|
-
end
|
16
|
-
|
17
|
-
def publish
|
18
|
-
enqueue_job
|
19
|
-
end
|
20
|
-
|
21
|
-
def publish_now
|
22
|
-
return unless need_publish?
|
23
|
-
Rabbit.publish(params)
|
24
|
-
|
25
|
-
model_naming = TableSync.publishing_adapter.model_naming(object_class)
|
26
|
-
TableSync::Instrument.notify table: model_naming.table, schema: model_naming.schema,
|
27
|
-
event: event,
|
28
|
-
count: publishing_data[:attributes].size, direction: :publish
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
attr_reader :original_attributes_array, :routing_key, :headers, :event
|
34
|
-
|
35
|
-
def push_original_attributes?
|
36
|
-
@push_original_attributes
|
37
|
-
end
|
38
|
-
|
39
|
-
def need_publish?
|
40
|
-
(push_original_attributes? && original_attributes_array.present?) || objects.present?
|
41
|
-
end
|
42
|
-
|
43
|
-
memoize def objects
|
44
|
-
needles.map { |needle| TableSync.publishing_adapter.find(object_class, needle) }.compact
|
45
|
-
end
|
46
|
-
|
47
|
-
def job_callable
|
48
|
-
TableSync.batch_publishing_job_class_callable
|
49
|
-
end
|
50
|
-
|
51
|
-
def job_callable_error_message
|
52
|
-
"Can't publish, set TableSync.batch_publishing_job_class_callable"
|
53
|
-
end
|
54
|
-
|
55
|
-
def attrs_for_callables
|
56
|
-
{}
|
57
|
-
end
|
58
|
-
|
59
|
-
def attrs_for_routing_key
|
60
|
-
{}
|
61
|
-
end
|
62
|
-
|
63
|
-
def attrs_for_metadata
|
64
|
-
{}
|
65
|
-
end
|
66
|
-
|
67
|
-
def params
|
68
|
-
{
|
69
|
-
**super,
|
70
|
-
headers: headers,
|
71
|
-
}
|
72
|
-
end
|
73
|
-
|
74
|
-
def needles
|
75
|
-
original_attributes_array.map { |original_attributes| original_attributes.slice(*primary_keys) }
|
76
|
-
end
|
77
|
-
|
78
|
-
def publishing_data
|
79
|
-
{
|
80
|
-
**super,
|
81
|
-
event: event,
|
82
|
-
metadata: {},
|
83
|
-
}
|
84
|
-
end
|
85
|
-
|
86
|
-
def attributes_for_sync
|
87
|
-
return original_attributes_array if push_original_attributes?
|
88
|
-
|
89
|
-
objects.map do |object|
|
90
|
-
if attributes_for_sync_defined?
|
91
|
-
object.attributes_for_sync
|
92
|
-
else
|
93
|
-
TableSync.publishing_adapter.attributes(object)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def enqueue_job
|
99
|
-
job_class.perform_later(
|
100
|
-
object_class.name,
|
101
|
-
original_attributes_array,
|
102
|
-
enqueue_additional_options,
|
103
|
-
)
|
104
|
-
end
|
105
|
-
|
106
|
-
def enqueue_additional_options
|
107
|
-
{ confirm: confirm?, push_original_attributes: push_original_attributes? }
|
108
|
-
end
|
109
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module TableSync::Publishing::ORMAdapter
|
4
|
-
module ActiveRecord
|
5
|
-
module_function
|
6
|
-
|
7
|
-
def model_naming(object)
|
8
|
-
::TableSync::NamingResolver::ActiveRecord.new(table_name: object.table_name)
|
9
|
-
end
|
10
|
-
|
11
|
-
def find(dataset, conditions)
|
12
|
-
dataset.find_by(conditions)
|
13
|
-
end
|
14
|
-
|
15
|
-
def attributes(object)
|
16
|
-
object.attributes
|
17
|
-
end
|
18
|
-
|
19
|
-
def setup_sync(klass, opts)
|
20
|
-
debounce_time = opts.delete(:debounce_time)
|
21
|
-
|
22
|
-
klass.instance_exec do
|
23
|
-
{ create: :created, update: :updated, destroy: :destroyed }.each do |event, state|
|
24
|
-
after_commit(on: event, **opts) do
|
25
|
-
TableSync::Publishing::Publisher.new(self.class.name, attributes,
|
26
|
-
state: state, debounce_time: debounce_time).publish
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module TableSync::Publishing::ORMAdapter
|
4
|
-
module Sequel
|
5
|
-
module_function
|
6
|
-
|
7
|
-
def model_naming(object)
|
8
|
-
::TableSync::NamingResolver::Sequel.new(table_name: object.table_name, db: object.db)
|
9
|
-
end
|
10
|
-
|
11
|
-
def find(dataset, conditions)
|
12
|
-
dataset.find(conditions)
|
13
|
-
end
|
14
|
-
|
15
|
-
def attributes(object)
|
16
|
-
object.values
|
17
|
-
end
|
18
|
-
|
19
|
-
def setup_sync(klass, opts)
|
20
|
-
if_predicate = to_predicate(opts.delete(:if), true)
|
21
|
-
unless_predicate = to_predicate(opts.delete(:unless), false)
|
22
|
-
debounce_time = opts.delete(:debounce_time)
|
23
|
-
|
24
|
-
if opts.any?
|
25
|
-
raise "Only :if, :skip_debounce and :unless options are currently " \
|
26
|
-
"supported for Sequel hooks"
|
27
|
-
end
|
28
|
-
|
29
|
-
register_callbacks(klass, if_predicate, unless_predicate, debounce_time)
|
30
|
-
end
|
31
|
-
|
32
|
-
def to_predicate(val, default)
|
33
|
-
return val.to_proc if val.respond_to?(:to_proc)
|
34
|
-
|
35
|
-
-> (*) { default }
|
36
|
-
end
|
37
|
-
|
38
|
-
def register_callbacks(klass, if_predicate, unless_predicate, debounce_time)
|
39
|
-
{ create: :created, update: :updated, destroy: :destroyed }.each do |event, state|
|
40
|
-
klass.send(:define_method, :"after_#{event}") do
|
41
|
-
if instance_eval(&if_predicate) && !instance_eval(&unless_predicate)
|
42
|
-
db.after_commit do
|
43
|
-
TableSync::Publishing::Publisher.new(
|
44
|
-
self.class.name,
|
45
|
-
values,
|
46
|
-
state: state,
|
47
|
-
debounce_time: debounce_time,
|
48
|
-
).publish
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
super()
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|