dekiru 0.1.8 → 0.1.9

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: 5ae4d2f88bf4f0db1e61e84464ea24df21523043c039f5b06fabf1a81d6a5f7d
4
- data.tar.gz: a329f3991c66e86f0f76d859770291ce9d0a4b32391e7554a48183fde335445c
3
+ metadata.gz: 1a97b022b934401c9a7bb4ada03bda713299e5ee8a59acc93e8e8397a6c2b529
4
+ data.tar.gz: 2adfa51b5b92c008f2a7099d863f385093a41a18f64c4123c15130c4dd29ad7f
5
5
  SHA512:
6
- metadata.gz: a5c8ff8add10ef86b6d165bb8ac9805cfd83bff5e3feca41b1d0237fcfefdcd81b9922b893d5f9f86f1408edb45c14903efafd3178b2fa8b66057d3bc8f2c057
7
- data.tar.gz: 1f3724e621a879d91f43787c8b0a08cfd33a3b97b00069574753074337aab4abc1e18fd72a687017c54f3659fe5e88461335e381230342177c66a373de9907b9
6
+ metadata.gz: 2329153068a4faa166d61d7c08ee43cec421bf93b9205c7ceed4104bd0b8cd5f1a81998a62623106e0f093c689adfa6ecce7a935b4e456d0d438080993dcf3ed
7
+ data.tar.gz: 63012279c77d3896c1e9b5a9e35587c95eeb5c0ae07a72645786bf223cd028093c938e67d75bf253ee6c1de64679e1e175bb924305adaa2dc5da272d75f70f00
data/README.md CHANGED
@@ -154,6 +154,34 @@ Finished successfully: Demo migration
154
154
  Total time: 6.35 sec
155
155
  ```
156
156
 
157
+ また`warning_side_effects: true`オプションを付けて実行することで、データ移行作業で発生した副作用が表示されるようになります。
158
+
159
+ ```ruby
160
+ Dekiru::DataMigrationOperator.execute('Demo migration', warning_side_effects: true) do
161
+ # ...
162
+ end
163
+ ```
164
+
165
+ ```
166
+ $ bin/rails r scripts/demo.rb
167
+ Start: Demo migration at 2019-05-24 18:29:57 +0900
168
+
169
+ all targets count: 30
170
+ Time: 00:00:00 |=================>>| 100% Progress
171
+ updated user count: 30
172
+
173
+ Write Queries!!
174
+ 30 call: Update "users" SET ...
175
+
176
+ Enqueued Jobs!!
177
+ 10 call: NotifyJob
178
+
179
+ Deliverd Mailers!!
180
+ 10 call: UserMailer
181
+
182
+ Are you sure to commit? (yes/no) > yes
183
+ ```
184
+
157
185
  ## Refinements
158
186
 
159
187
  ### Dekiru::CamelizeHash
@@ -10,18 +10,22 @@ module Dekiru
10
10
 
11
11
  def initialize(title, options = {})
12
12
  @title = title
13
- @stream = options[:output] || $stdout
14
- end
15
-
16
- def log(message)
17
- stream.puts(message)
13
+ @options = options
14
+ @stream = @options[:output] || $stdout
15
+ @side_effects = Hash.new do |hash, key|
16
+ hash[key] = Hash.new(0)
17
+ end
18
18
  end
19
19
 
20
20
  def execute(&block)
21
21
  @started_at = Time.current
22
22
  log "Start: #{title} at #{started_at}\n\n"
23
23
  @result = ActiveRecord::Base.transaction(requires_new: true, joinable: false) do
24
- instance_eval(&block)
24
+ if @options[:warning_side_effects]
25
+ warning_side_effects(&block)
26
+ else
27
+ instance_eval(&block)
28
+ end
25
29
  confirm?("\nAre you sure to commit?")
26
30
  end
27
31
  log "Finished successfully: #{title}" if @result == true
@@ -56,6 +60,12 @@ module Dekiru
56
60
  pb.finish
57
61
  end
58
62
 
63
+ private
64
+
65
+ def log(message)
66
+ stream.puts(message)
67
+ end
68
+
59
69
  def confirm?(message = 'Are you sure?')
60
70
  loop do
61
71
  stream.print "#{message} (yes/no) > "
@@ -78,5 +88,34 @@ module Dekiru
78
88
  log "Canceled: #{title}"
79
89
  raise ActiveRecord::Rollback
80
90
  end
91
+
92
+ def handle_notification(*args)
93
+ event = ActiveSupport::Notifications::Event.new(*args)
94
+
95
+ increment_side_effects(:enqueued_jobs, event.payload[:job].class.name) if event.payload[:job]
96
+ increment_side_effects(:deliverd_mailers, event.payload[:mailer]) if event.payload[:mailer]
97
+
98
+ if event.payload[:sql] && /\A\s*(insert|update|delete)/i.match?(event.payload[:sql])
99
+ increment_side_effects(:write_queries, event.payload[:sql])
100
+ end
101
+ end
102
+
103
+ def increment_side_effects(type, value)
104
+ @side_effects[type][value] += 1
105
+ end
106
+
107
+ def warning_side_effects(&block)
108
+ ActiveSupport::Notifications.subscribed(method(:handle_notification), /^(sql|enqueue|deliver)/) do
109
+ instance_eval(&block)
110
+ end
111
+
112
+ @side_effects.each do |name, items|
113
+ newline
114
+ log "#{name.to_s.titlecase}!!"
115
+ items.sort_by { |v, c| c }.reverse.slice(0, 20).each do |value, count|
116
+ log "#{count} call: #{value}"
117
+ end
118
+ end
119
+ end
81
120
  end
82
121
  end
@@ -1,3 +1,3 @@
1
1
  module Dekiru
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dekiru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akihiro Matsumura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-05 00:00:00.000000000 Z
11
+ date: 2020-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http_accept_language