table_sync 1.11.0 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86c13b07f0c63f70ecd2acc80fca50cdd57e0c3682ad5d7bafa56ef69b94185d
4
- data.tar.gz: 8e01d572ffa14629317b248e0090bab8bde89e3dee513434b51b0bc897ceb02f
3
+ metadata.gz: 703130e1a092c3fdff5a5ada5c83d10371a33ee7f4f45c770e9c4dd48fe20c1d
4
+ data.tar.gz: 1bc7d081106b6aeb2544f504485ec62a6e5eb7a6c6eb6e2d06aa48413d43cdfd
5
5
  SHA512:
6
- metadata.gz: 741c3d1904400d34d1e3f0fb03eac24d9b954544744dd0736fa672a3a94751b09e0b221867d4e625749a8301f674e527f82cf62d297ccb1e4f4d1c09b6ad45f9
7
- data.tar.gz: 62ee1dc7912c8c301105f0134e0a8dbef9f45ba3ca9d82ff321127f9daedd03129f261ee7af8b738da029d3f5d012627c2cbbd0f9e759826218f1b2c70f5c0b2
6
+ metadata.gz: efb76a18e1c8d417a73114953a08901add1c282876747dd550d4ee3c7705881fa77f9394cb152e3efe9de404090ebf716ddd69f9f73a013615498b0e7bcdef0c
7
+ data.tar.gz: 98f5481ee75be205f2e27938c46ebf82b54ede70248fd052a4ef5bce3a14bf574171364485d1e1d465b42801b0a820e55b3d1f3ae5f920497f92d4488351c911
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [1.12.0] - 2019-09-20
5
+ ### Changed
6
+ - Payload for existing ActiveSupport adapter contains both `table` and `schema` fields now.
7
+
4
8
  ## [1.11.0] - 2019-09-11
5
9
  ### Added
6
10
  - Receiving: inconsistent events raises `TableSync::UnprovidedEventTargetKeysError`
data/docs/synopsis.md CHANGED
@@ -276,13 +276,14 @@ Types of events available:
276
276
  `"tablesync.receive.update"`, `"tablesync.receive.destroy"`, `"tablesync.publish.update"`
277
277
  and `"tablesync.publish.destroy"`.
278
278
 
279
- You have access to the payload, which contains `event`, `direction`, `table` and `count`.
279
+ You have access to the payload, which contains `event`, `direction`, `table`, `schema` and `count`.
280
280
 
281
281
  ```
282
282
  {
283
283
  :event => :update, # one of update / destroy
284
284
  :direction => :publish, # one of publish / receive
285
285
  :table => "users",
286
+ :schema => "public",
286
287
  :count => 1
287
288
  }
288
289
  ```
@@ -19,7 +19,9 @@ class TableSync::BatchPublisher < TableSync::BasePublisher
19
19
  return unless need_publish?
20
20
  Rabbit.publish(params)
21
21
 
22
- TableSync::Instrument.notify table: TableSync.orm.table_name(object_class), event: event,
22
+ model_naming = TableSync.orm.model_naming(object_class)
23
+ TableSync::Instrument.notify table: model_naming.table, schema: model_naming.schema,
24
+ event: event,
23
25
  count: publishing_data[:attributes].size, direction: :publish
24
26
  end
25
27
 
@@ -10,6 +10,7 @@ module TableSync
10
10
 
11
11
  @callback_registry = CallbackRegistry.new
12
12
 
13
+ # initialize default options
13
14
  only(model.columns)
14
15
  mapping_overrides({})
15
16
  additional_data({})
@@ -21,23 +22,33 @@ module TableSync
21
22
  target_keys(model.primary_keys)
22
23
  end
23
24
 
24
- # add_option implements next logic
25
+ # add_option implements the following logic
25
26
  # config.option - get value
26
27
  # config.option(args) - set static value
27
28
  # config.option { ... } - set proc as value
28
29
  def self.add_option(name, &option_block)
29
30
  ivar = "@#{name}".to_sym
30
31
 
32
+ # default option handler (empty handler)
33
+ # handles values when setting options
31
34
  option_block ||= proc { |value| value }
32
35
 
33
36
  define_method(name) do |*args, &block|
34
- if args.empty? && block.nil?
37
+ # logic for each option
38
+ if args.empty? && block.nil? # case for config.option, just return ivar
35
39
  instance_variable_get(ivar)
36
- elsif block
40
+ elsif block # case for config.option { ... }
41
+ # get named parameters (parameters with :keyreq) from proc-value
37
42
  params = block.parameters.map { |param| param[0] == :keyreq ? param[1] : nil }.compact
43
+
44
+ # wrapper for proc-value, this wrapper receives all params (model, version, project_id...)
45
+ # and filters them for proc-value
38
46
  unified_block = proc { |hash = {}| block.call(hash.slice(*params)) }
47
+
48
+ # set wrapped proc-value as ivar value
39
49
  instance_variable_set(ivar, unified_block)
40
- else
50
+ else # case for config.option(args)
51
+ # call option_block with args as params and set ivar to result
41
52
  instance_variable_set(ivar, instance_exec(*args, &option_block))
42
53
  end
43
54
  end
@@ -4,10 +4,11 @@ module TableSync::InstrumentAdapter
4
4
  module ActiveSupport
5
5
  module_function
6
6
 
7
- def notify(table:, event:, direction:, count: 1)
7
+ def notify(table:, schema:, event:, direction:, count: 1)
8
8
  ::ActiveSupport::Notifications.instrument "tablesync.#{direction}.#{event}",
9
9
  count: count,
10
10
  table: table.to_s,
11
+ schema: schema.to_s,
11
12
  event: event,
12
13
  direction: direction
13
14
  end
@@ -45,8 +45,8 @@ module TableSync::Model
45
45
  AND kcu.constraint_name = tc.constraint_name
46
46
  WHERE
47
47
  t.table_schema NOT IN ('pg_catalog', 'information_schema')
48
- AND t.table_schema = '#{table_info[:schema]}'
49
- AND t.table_name = '#{table_info[:name]}'
48
+ AND t.table_schema = '#{model_naming.schema}'
49
+ AND t.table_name = '#{model_naming.table}'
50
50
  ORDER BY
51
51
  kcu.ordinal_position
52
52
  SQL
@@ -74,9 +74,8 @@ module TableSync::Model
74
74
  end.compact
75
75
  end
76
76
 
77
- TableSync::Instrument.notify(
78
- table: table_name, event: :update, count: result.count, direction: :receive,
79
- )
77
+ TableSync::Instrument.notify(table: model_naming.table, schema: model_naming.schema,
78
+ event: :update, count: result.count, direction: :receive)
80
79
 
81
80
  result
82
81
  end
@@ -88,7 +87,8 @@ module TableSync::Model
88
87
  end
89
88
 
90
89
  TableSync::Instrument.notify(
91
- table: table_name, event: :destroy, count: result.count, direction: :receive,
90
+ table: model_naming.table, schema: model_naming.schema,
91
+ event: :destroy, count: result.count, direction: :receive
92
92
  )
93
93
 
94
94
  result
@@ -106,21 +106,14 @@ module TableSync::Model
106
106
 
107
107
  attr_reader :raw_model
108
108
 
109
- def table_info
110
- keys = raw_model.table_name.split(".")
111
- name = keys[-1]
112
- schema = keys[-2] || "public"
113
- { schema: schema, name: name }
114
- end
115
-
116
- def table_name
117
- table_info[:name]
118
- end
119
-
120
109
  def db
121
110
  @raw_model.connection
122
111
  end
123
112
 
113
+ def model_naming
114
+ ::TableSync::NamingResolver::ActiveRecord.new(table_name: raw_model.table_name)
115
+ end
116
+
124
117
  def row_to_hash(row)
125
118
  row.attributes.each_with_object({}) { |(k, v), o| o[k.to_sym] = v }
126
119
  end
@@ -36,14 +36,15 @@ module TableSync::Model
36
36
  )
37
37
  .multi_insert(insert_data)
38
38
 
39
- TableSync::Instrument.notify table: table_name, count: result.count,
40
- event: :update, direction: :receive
39
+ TableSync::Instrument.notify table: model_naming.table, schema: model_naming.schema,
40
+ count: result.count, event: :update, direction: :receive
41
41
  result
42
42
  end
43
43
 
44
44
  def destroy(data)
45
45
  result = dataset.returning.where(data).delete
46
- TableSync::Instrument.notify table: table_name, count: result.count,
46
+ TableSync::Instrument.notify table: model_naming.table, schema: model_naming.schema,
47
+ count: result.count,
47
48
  event: :destroy, direction: :receive
48
49
  result
49
50
  end
@@ -64,6 +65,10 @@ module TableSync::Model
64
65
  raw_model.table_name
65
66
  end
66
67
 
68
+ def model_naming
69
+ ::TableSync::NamingResolver::Sequel.new(table_name: table_name, db: db)
70
+ end
71
+
67
72
  def dataset
68
73
  raw_model.dataset
69
74
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableSync::NamingResolver
4
+ class ActiveRecord
5
+ def initialize(table_name:)
6
+ @table_name = table_name
7
+ end
8
+
9
+ def table
10
+ meta_data.last
11
+ end
12
+
13
+ def schema
14
+ meta_data.size > 1 ? meta_data[-2] : "public"
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :table_name
20
+
21
+ def meta_data
22
+ table_name.to_s.split "."
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableSync::NamingResolver
4
+ class Sequel
5
+ def initialize(table_name:, db:)
6
+ @table_name = table_name
7
+ @db = db
8
+ end
9
+
10
+ def table
11
+ table_name.is_a?(::Sequel::SQL::QualifiedIdentifier) ? table_name.column : table_name
12
+ end
13
+
14
+ def schema
15
+ return table_name.table if table_name.is_a?(::Sequel::SQL::QualifiedIdentifier)
16
+ db.get(Sequel.function("current_schema")) rescue "public"
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :table_name, :db
22
+ end
23
+ end
@@ -8,6 +8,10 @@ module TableSync::ORMAdapter
8
8
  ::TableSync::Model::ActiveRecord
9
9
  end
10
10
 
11
+ def model_naming(object)
12
+ ::TableSync::NamingResolver::ActiveRecord.new(table_name: object.table_name)
13
+ end
14
+
11
15
  def find(dataset, conditions)
12
16
  dataset.find_by(conditions)
13
17
  end
@@ -16,10 +20,6 @@ module TableSync::ORMAdapter
16
20
  object.attributes
17
21
  end
18
22
 
19
- def table_name(object)
20
- object.table_name
21
- end
22
-
23
23
  def setup_sync(klass, **opts)
24
24
  debounce_time = opts.delete(:debounce_time)
25
25
 
@@ -8,6 +8,10 @@ module TableSync::ORMAdapter
8
8
  ::TableSync::Model::Sequel
9
9
  end
10
10
 
11
+ def model_naming(object)
12
+ ::TableSync::NamingResolver::Sequel.new(table_name: object.table_name, db: object.db)
13
+ end
14
+
11
15
  def find(dataset, conditions)
12
16
  dataset.find(conditions)
13
17
  end
@@ -16,10 +20,6 @@ module TableSync::ORMAdapter
16
20
  object.values
17
21
  end
18
22
 
19
- def table_name(object)
20
- object.table_name
21
- end
22
-
23
23
  def setup_sync(klass, **opts)
24
24
  if_predicate = to_predicate(opts.delete(:if), true)
25
25
  unless_predicate = to_predicate(opts.delete(:unless), false)
@@ -34,7 +34,8 @@ class TableSync::Publisher < TableSync::BasePublisher
34
34
  return if !object && !destroyed?
35
35
 
36
36
  Rabbit.publish(params)
37
- TableSync::Instrument.notify table: TableSync.orm.table_name(object_class),
37
+ model_naming = TableSync.orm.model_naming(object_class)
38
+ TableSync::Instrument.notify table: model_naming.table, schema: model_naming.schema,
38
39
  event: event, direction: :publish
39
40
  end
40
41
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableSync
4
- VERSION = "1.11.0"
4
+ VERSION = "1.12.0"
5
5
  end
data/lib/table_sync.rb CHANGED
@@ -24,6 +24,8 @@ module TableSync
24
24
  require_relative "./table_sync/model/sequel"
25
25
  require_relative "./table_sync/instrument"
26
26
  require_relative "./table_sync/instrument_adapter/active_support"
27
+ require_relative "./table_sync/naming_resolver/active_record"
28
+ require_relative "./table_sync/naming_resolver/sequel"
27
29
 
28
30
  class << self
29
31
  include Memery
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: 1.11.0
4
+ version: 1.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Umbrellio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-11 00:00:00.000000000 Z
11
+ date: 2019-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: memery
@@ -267,6 +267,8 @@ files:
267
267
  - lib/table_sync/instrument_adapter/active_support.rb
268
268
  - lib/table_sync/model/active_record.rb
269
269
  - lib/table_sync/model/sequel.rb
270
+ - lib/table_sync/naming_resolver/active_record.rb
271
+ - lib/table_sync/naming_resolver/sequel.rb
270
272
  - lib/table_sync/orm_adapter/active_record.rb
271
273
  - lib/table_sync/orm_adapter/sequel.rb
272
274
  - lib/table_sync/publisher.rb