table_sync 4.0.0 → 4.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63d56abd7c8729d7301e24bf4da9b6e1b8f4a44c387f3041a746398ed1bb381e
4
- data.tar.gz: 10344d248c85c9effc5f25ef1442b176a2c90f2135efea3dfaf653df2c53d408
3
+ metadata.gz: d6b452885641883758a131ec14f3a9aa2f8508bb1c3a540b8e78c727a8d86a88
4
+ data.tar.gz: efd055d98480febde524af5af5db5cfefde1b534a5fa40d7b8cb696ac11432ba
5
5
  SHA512:
6
- metadata.gz: 1c2dc57d1e62c294ffab4e2ec32361b1bcecdc06f3037f6944fb044e19f3fe6f9efe1409b086ab6b8bbe0f5ac801cc609896a5750733bc4536a0f4c11806a008
7
- data.tar.gz: edb939c55898245a92ac762adbbf933cfd4fb7fc6ebf8e4c0bc1a18398ecd2d354f7604f851206c0c1c7a6b1f2eab4af987a0505a204a6cbdff2966e37257800
6
+ metadata.gz: bd98bc390f5d04625d98c9c5fac179a9b588830210502727288b74c46f4badf4a177afbcbe02b67bd33cdd7ba8ed7d3fea593e3893e5519f5d2c40c01f3a454e
7
+ data.tar.gz: 230f6a412f0d2d55750a4d1edb75b5c9605730de1c6352d5e5d1e8b4375ed16081fe296f70ace3dff65b54d5b85bc1a18a153119d8a6f7c1994289d5170ddc80
@@ -1,6 +1,11 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [4.1.0] - 2020-11-02
5
+ ### Changed
6
+ - move `TableSync::Instrument.notify` from models to the handler
7
+ - fire `TableSync::Instrument.notify` after commit insted of in transaction
8
+
4
9
  ## [4.0.0] - 2020-10-23
5
10
  ### Returned
6
11
  - config inheritance
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_sync (4.0.0)
4
+ table_sync (4.1.0)
5
5
  memery
6
6
  rabbit_messaging (~> 0.3)
7
7
  rails
@@ -22,7 +22,7 @@ class Rabbit::Handler::MainProject::TableSync < TableSync::ReceivingHandler
22
22
  receive "User", to_table: :clients, events: %i[update destroy] do
23
23
  mapping_overrides email: :project_user_email, id: :project_user_id
24
24
 
25
- only :project_user_email, :project_user_id
25
+ only :project_user_email, :project_user_id, :project_id
26
26
  target_keys :project_id, :project_user_id
27
27
  rest_key :project_user_rest
28
28
  version_key :project_user_version
@@ -37,7 +37,7 @@ class Rabbit::Handler::MainProject::TableSync < TableSync::ReceivingHandler
37
37
  end
38
38
 
39
39
  receive "User", to_model: CustomModel.new(:users) do
40
- rest_key nil
40
+ rest_key false
41
41
  end
42
42
  end
43
43
  ```
@@ -109,22 +109,23 @@ class TableSync::Receiving::Handler < Rabbit::EventHandler
109
109
  model = config.model
110
110
 
111
111
  model.transaction do
112
- if event == :update
113
- config.before_update(**params)
114
-
115
- results = model.upsert(**params)
112
+ results = if event == :update
113
+ config.before_update(**params)
114
+ model.upsert(**params)
115
+ else
116
+ config.before_destroy(**params)
117
+ model.destroy(**params)
118
+ end
119
+
120
+ model.after_commit do
121
+ TableSync::Instrument.notify table: model.table, schema: model.schema,
122
+ count: results.count, event: event, direction: :receive
123
+ end
116
124
 
117
- model.after_commit do
118
- config.after_commit_on_update(**params.merge(results: results))
119
- end
125
+ if event == :update
126
+ model.after_commit { config.after_commit_on_update(**params.merge(results: results)) }
120
127
  else
121
- config.before_destroy(**params)
122
-
123
- results = model.destroy(**params)
124
-
125
- model.after_commit do
126
- config.after_commit_on_destroy(**params.merge(results: results))
127
- end
128
+ model.after_commit { config.after_commit_on_destroy(**params.merge(results: results)) }
128
129
  end
129
130
  end
130
131
  end
@@ -18,11 +18,18 @@ module TableSync::Receiving::Model
18
18
  def trigger_transactional_callbacks?(*); end
19
19
  end
20
20
 
21
+ attr_reader :table, :schema
22
+
21
23
  def initialize(table_name)
22
24
  @raw_model = Class.new(::ActiveRecord::Base) do
23
25
  self.table_name = table_name
24
26
  self.inheritance_column = nil
25
27
  end
28
+
29
+ model_naming = ::TableSync::NamingResolver::ActiveRecord.new(table_name: table_name)
30
+
31
+ @table = model_naming.table.to_sym
32
+ @schema = model_naming.schema.to_sym
26
33
  end
27
34
 
28
35
  def columns
@@ -45,15 +52,15 @@ module TableSync::Receiving::Model
45
52
  AND kcu.constraint_name = tc.constraint_name
46
53
  WHERE
47
54
  t.table_schema NOT IN ('pg_catalog', 'information_schema')
48
- AND t.table_schema = '#{model_naming.schema}'
49
- AND t.table_name = '#{model_naming.table}'
55
+ AND t.table_schema = '#{schema}'
56
+ AND t.table_name = '#{table}'
50
57
  ORDER BY
51
58
  kcu.ordinal_position
52
59
  SQL
53
60
  end
54
61
 
55
62
  def upsert(data:, target_keys:, version_key:, default_values:)
56
- result = data.map do |datum|
63
+ data.map do |datum|
57
64
  conditions = datum.select { |k| target_keys.include?(k) }
58
65
 
59
66
  row = raw_model.lock("FOR NO KEY UPDATE").where(conditions)
@@ -75,11 +82,6 @@ module TableSync::Receiving::Model
75
82
 
76
83
  row_to_hash(row)
77
84
  end.compact
78
-
79
- TableSync::Instrument.notify(table: model_naming.table, schema: model_naming.schema,
80
- event: :update, count: result.count, direction: :receive)
81
-
82
- result
83
85
  end
84
86
 
85
87
  def destroy(data:, target_keys:, version_key:)
@@ -100,11 +102,6 @@ module TableSync::Receiving::Model
100
102
  raise TableSync::DestroyError.new(data: data, target_keys: target_keys, result: result)
101
103
  end
102
104
 
103
- TableSync::Instrument.notify(
104
- table: model_naming.table, schema: model_naming.schema,
105
- event: :destroy, count: result.count, direction: :receive
106
- )
107
-
108
105
  result
109
106
  end
110
107
 
@@ -121,11 +118,7 @@ module TableSync::Receiving::Model
121
118
  attr_reader :raw_model
122
119
 
123
120
  def db
124
- @raw_model.connection
125
- end
126
-
127
- def model_naming
128
- ::TableSync::NamingResolver::ActiveRecord.new(table_name: raw_model.table_name)
121
+ raw_model.connection
129
122
  end
130
123
 
131
124
  def row_to_hash(row)
@@ -2,8 +2,18 @@
2
2
 
3
3
  module TableSync::Receiving::Model
4
4
  class Sequel
5
+ attr_reader :table, :schema
6
+
5
7
  def initialize(table_name)
6
8
  @raw_model = Class.new(::Sequel::Model(table_name)).tap(&:unrestrict_primary_key)
9
+
10
+ model_naming = ::TableSync::NamingResolver::Sequel.new(
11
+ table_name: table_name,
12
+ db: @raw_model.db,
13
+ )
14
+
15
+ @table = model_naming.table.to_sym
16
+ @schema = model_naming.schema.to_sym
7
17
  end
8
18
 
9
19
  def columns
@@ -15,7 +25,7 @@ module TableSync::Receiving::Model
15
25
  end
16
26
 
17
27
  def upsert(data:, target_keys:, version_key:, default_values:)
18
- qualified_version = ::Sequel.qualify(table_name, version_key)
28
+ qualified_version = ::Sequel.qualify(raw_model.table_name, version_key)
19
29
  version_condition = ::Sequel.function(:coalesce, qualified_version, 0) <
20
30
  ::Sequel.qualify(:excluded, version_key)
21
31
 
@@ -24,18 +34,10 @@ module TableSync::Receiving::Model
24
34
 
25
35
  insert_data = type_cast(data)
26
36
 
27
- result = dataset.returning
28
- .insert_conflict(
29
- target: target_keys,
30
- update: upd_spec,
31
- update_where: version_condition,
32
- )
33
- .multi_insert(insert_data)
34
-
35
- TableSync::Instrument.notify table: model_naming.table, schema: model_naming.schema,
36
- count: result.count, event: :update, direction: :receive
37
-
38
- result
37
+ dataset
38
+ .returning
39
+ .insert_conflict(target: target_keys, update: upd_spec, update_where: version_condition)
40
+ .multi_insert(insert_data)
39
41
  end
40
42
 
41
43
  def destroy(data:, target_keys:, version_key:)
@@ -47,10 +49,6 @@ module TableSync::Receiving::Model
47
49
  raise TableSync::DestroyError.new(data: data, target_keys: target_keys, result: result)
48
50
  end
49
51
 
50
- TableSync::Instrument.notify table: model_naming.table, schema: model_naming.schema,
51
- count: result.count,
52
- event: :destroy, direction: :receive
53
-
54
52
  result
55
53
  end
56
54
 
@@ -66,14 +64,6 @@ module TableSync::Receiving::Model
66
64
 
67
65
  attr_reader :raw_model
68
66
 
69
- def table_name
70
- raw_model.table_name
71
- end
72
-
73
- def model_naming
74
- ::TableSync::NamingResolver::Sequel.new(table_name: table_name, db: db)
75
- end
76
-
77
67
  def dataset
78
68
  raw_model.dataset
79
69
  end
@@ -95,3 +95,9 @@ __END__
95
95
  :primary_keys:
96
96
  :parameters: []
97
97
  :description: "returns an array with the primary_keys"
98
+ :table:
99
+ :parameters: []
100
+ :description: "returns an instance of Symbol"
101
+ :schema:
102
+ :parameters: []
103
+ :description: "returns an instance of Symbol"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableSync
4
- VERSION = "4.0.0"
4
+ VERSION = "4.1.0"
5
5
  end
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.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Umbrellio
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-26 00:00:00.000000000 Z
11
+ date: 2020-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: memery
@@ -302,7 +302,7 @@ homepage: https://github.com/umbrellio/table_sync
302
302
  licenses:
303
303
  - MIT
304
304
  metadata: {}
305
- post_install_message:
305
+ post_install_message:
306
306
  rdoc_options: []
307
307
  require_paths:
308
308
  - lib
@@ -318,7 +318,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
318
318
  version: '0'
319
319
  requirements: []
320
320
  rubygems_version: 3.2.0.rc.1
321
- signing_key:
321
+ signing_key:
322
322
  specification_version: 4
323
323
  summary: DB Table synchronization between microservices based on Model's event system
324
324
  and RabbitMQ messaging