Class: Dekiru::DataMigrationOperator
- Inherits:
-
Object
- Object
- Dekiru::DataMigrationOperator
- Defined in:
- lib/dekiru/data_migration_operator.rb
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.
-
#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
- #execute(&block) ⇒ Object
- #find_each_with_progress(target_scope, options = {}) ⇒ Object
-
#initialize(title, options = {}) ⇒ DataMigrationOperator
constructor
A new instance of DataMigrationOperator.
Constructor Details
#initialize(title, options = {}) ⇒ DataMigrationOperator
Returns a new instance of DataMigrationOperator.
11 12 13 14 15 16 17 18 19 |
# File 'lib/dekiru/data_migration_operator.rb', line 11 def initialize(title, = {}) @title = title = @stream = .fetch(:output, $stdout) @without_transaction = .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.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def canceled @canceled end |
#ended_at ⇒ Object (readonly)
Returns the value of attribute ended_at.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def ended_at @ended_at end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def error @error end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def result @result end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def started_at @started_at end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def stream @stream end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def title @title end |
Class Method Details
.execute(title, options = {}, &block) ⇒ Object
7 8 9 |
# File 'lib/dekiru/data_migration_operator.rb', line 7 def self.execute(title, = {}, &block) self.new(title, ).execute(&block) end |
Instance Method Details
#duration ⇒ Object
46 47 48 |
# File 'lib/dekiru/data_migration_operator.rb', line 46 def duration ((self.ended_at || Time.current) - self.started_at) end |
#execute(&block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dekiru/data_migration_operator.rb', line 21 def execute(&block) @started_at = Time.current log "Start: #{title} at #{started_at}\n\n" if @without_transaction run(&block) @result = true else @result = ActiveRecord::Base.transaction(requires_new: true, joinable: false) do run(&block) 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 = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dekiru/data_migration_operator.rb', line 50 def find_each_with_progress(target_scope, = {}) total = .delete(:total) opt = { format: '%a |%b>>%i| %p%% %t', }.merge().merge( total: total || target_scope.count, output: stream ) pb = ::ProgressBar.create(opt) target_scope.find_each do |target| yield target pb.increment end pb.finish end |