Class: Dekiru::DataMigrationOperator
- Inherits:
-
Object
- Object
- Dekiru::DataMigrationOperator
- Defined in:
- lib/dekiru/data_migration_operator.rb
Defined Under Namespace
Classes: NestedTransactionError
Instance Attribute Summary collapse
-
#canceled ⇒ Object
readonly
Returns the value of attribute canceled.
-
#ended_at ⇒ Object
readonly
Returns the value of attribute ended_at.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
Instance Method Summary collapse
- #duration ⇒ Object
- #each_with_progress(enum, options = {}) ⇒ Object
- #execute(&block) ⇒ Object
- #find_each_with_progress(target_scope, options = {}, &block) ⇒ Object
-
#initialize(title, options = {}) ⇒ DataMigrationOperator
constructor
A new instance of DataMigrationOperator.
Constructor Details
#initialize(title, options = {}) ⇒ DataMigrationOperator
Returns a new instance of DataMigrationOperator.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/dekiru/data_migration_operator.rb', line 13 def initialize(title, = {}) @title = title @options = @logger = @options.fetch(:logger) { Logger.new(Rails.root.join("log/data_migration_#{Time.current.strftime("%Y%m%d%H%M")}.log")) } @stream = @options.fetch(:output, $stdout) @without_transaction = @options.fetch(:without_transaction, false) @side_effects = Hash.new do |hash, key| hash[key] = Hash.new(0) end end |
Instance Attribute Details
#canceled ⇒ Object (readonly)
Returns the value of attribute canceled.
7 8 9 |
# File 'lib/dekiru/data_migration_operator.rb', line 7 def canceled @canceled end |
#ended_at ⇒ Object (readonly)
Returns the value of attribute ended_at.
7 8 9 |
# File 'lib/dekiru/data_migration_operator.rb', line 7 def ended_at @ended_at end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
7 8 9 |
# File 'lib/dekiru/data_migration_operator.rb', line 7 def error @error end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'lib/dekiru/data_migration_operator.rb', line 7 def logger @logger end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
7 8 9 |
# File 'lib/dekiru/data_migration_operator.rb', line 7 def result @result end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
7 8 9 |
# File 'lib/dekiru/data_migration_operator.rb', line 7 def started_at @started_at end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
7 8 9 |
# File 'lib/dekiru/data_migration_operator.rb', line 7 def stream @stream end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
7 8 9 |
# File 'lib/dekiru/data_migration_operator.rb', line 7 def title @title end |
Class Method Details
.execute(title, options = {}, &block) ⇒ Object
9 10 11 |
# File 'lib/dekiru/data_migration_operator.rb', line 9 def self.execute(title, = {}, &block) self.new(title, ).execute(&block) end |
Instance Method Details
#duration ⇒ Object
52 53 54 |
# File 'lib/dekiru/data_migration_operator.rb', line 52 def duration ((self.ended_at || Time.current) - self.started_at) end |
#each_with_progress(enum, options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dekiru/data_migration_operator.rb', line 56 def each_with_progress(enum, = {}) = .dup [:total] ||= ((enum.size == Float::INFINITY ? nil : enum.size) rescue nil) [:format] ||= [:total] ? '%a |%b>>%i| %p%% %t' : '%a |%b>>%i| ??%% %t' [:output] = stream @pb = ::ProgressBar.create() enum.each do |item| yield item @pb.increment end @pb.finish end |
#execute(&block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dekiru/data_migration_operator.rb', line 24 def execute(&block) @started_at = Time.current log "Start: #{title} at #{started_at}\n\n" if @without_transaction run(&block) @result = true else raise NestedTransactionError if current_transaction_open? @result = ActiveRecord::Base.transaction do run(&block) log "Finished execution: #{title}" confirm?("\nAre you sure to commit?") end end log "Finished successfully: #{title}" if @result == true rescue => e @error = e @result = false ensure @ended_at = Time.current log "Total time: #{self.duration.round(2)} sec" raise error if error return @result end |
#find_each_with_progress(target_scope, options = {}, &block) ⇒ Object
70 71 72 73 74 |
# File 'lib/dekiru/data_migration_operator.rb', line 70 def find_each_with_progress(target_scope, = {}, &block) # `LocalJumpError: no block given (yield)` が出る場合、 find_each メソッドが enumerator を返していない可能性があります # 直接 each_with_progress を使うか、 find_each が enumerator を返すように修正してください each_with_progress(target_scope.find_each, , &block) end |