dekiru 0.1.6 → 0.1.10

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: 645b46b3968939b35e0d81f7af7cb370c9a1120b489081df02cac5de31d9eb22
4
- data.tar.gz: 87e792def5e10912c14cdd25f002eeb6a4c6db2de08c9d95a0663c2944656a19
3
+ metadata.gz: c6561c9b0821c11571527dc126a5014304352c8b58a5915e03dcaffdcfe9987b
4
+ data.tar.gz: d599fef1eb98d58389da140f638ef317c2866583c77d29c403e2d72ad4a1517b
5
5
  SHA512:
6
- metadata.gz: acd32ab352485b4be6c5fd8ea0277bf06338cfe6cdaade9930fdd81b344ba9a9177b691c1cc55f7b08b443d84530267b805a20347679ec0cc47d367324f3ce2a
7
- data.tar.gz: b342ebd2a82fafdf44b5776407f502412c8fb541cee51d7c889f7dede805bb5ea4c70f90ea6f0c4826a5710c613b432f6fdb7df2f8925e0cfef7a57139580a2d
6
+ metadata.gz: 8bcbe875ecbbab54ca72d7dbed1d63988d7a5dfa403c5aeb75d657a606d64e1669e3470daaacbf459a6b72a321b32bea70efed6a0b058b4a88938708d03a0b5a
7
+ data.tar.gz: b53319d34f655be66216a88c71c31d4119289a1beea0929bcf20bee6eed6edd4f8350c540ca5d53cfd785ff0ac2422ab0c03154ebf4bb3774f61146cba88e412
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
@@ -2,14 +2,14 @@ module Dekiru
2
2
  module Capybara
3
3
  module Matchers
4
4
  class JsNoErrorMatcher
5
- def matches?(page)
6
- errors = page.driver.browser.manage.logs.get(:browser)
7
- errors.find_all { |error| error.level == 'WARNING' }.each do |error|
5
+ def matches?(page_or_logs)
6
+ logs = page_or_logs.respond_to?(:driver) ? page_or_logs.driver.browser.manage.logs.get(:browser) : page_or_logs
7
+ logs.find_all { |log| log.level == 'WARNING' }.each do |log|
8
8
  STDERR.puts 'WARN: javascript warning'
9
- STDERR.puts error.message
9
+ STDERR.puts log.message
10
10
  end
11
11
 
12
- @severe_errors = errors.find_all { |error| error.level == 'SEVERE' }
12
+ @severe_errors = logs.find_all { |log| log.level == 'SEVERE' }
13
13
  @severe_errors.empty?
14
14
  end
15
15
 
@@ -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
@@ -4,16 +4,17 @@ module TaskWithLogger
4
4
 
5
5
  def task(*args, &block)
6
6
  new_block = proc do |_task, _args|
7
- __echo__ "[START] #{_task.name} #{_args.to_h} (#{Time.current})"
7
+ TaskWithLogger.echo("[START] #{_task.name} #{_args.to_h} (#{Time.current})")
8
8
  yield(_task, _args)
9
- __echo__ "[END] #{_task.name} #{_args.to_h} (#{Time.current})"
9
+ TaskWithLogger.echo("[END] #{_task.name} #{_args.to_h} (#{Time.current})")
10
10
  end
11
11
  super(*args, &new_block)
12
12
  end
13
+ end
13
14
 
14
- def __echo__(str)
15
- Rails.logger.info(str)
16
- puts(str)
17
- end
15
+ def echo(str)
16
+ Rails.logger.info(str)
17
+ puts(str)
18
18
  end
19
+ module_function :echo
19
20
  end
@@ -1,3 +1,3 @@
1
1
  module Dekiru
2
- VERSION = '0.1.6'
2
+ VERSION = '0.1.10'
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.6
4
+ version: 0.1.10
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-09-16 00:00:00.000000000 Z
11
+ date: 2021-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http_accept_language