Class: Dekiru::DataMigrationOperator

Inherits:
Object
  • Object
show all
Defined in:
lib/dekiru/data_migration_operator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @title = title
  @options = options
  @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

#canceledObject (readonly)

Returns the value of attribute canceled.



5
6
7
# File 'lib/dekiru/data_migration_operator.rb', line 5

def canceled
  @canceled
end

#ended_atObject (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

#errorObject (readonly)

Returns the value of attribute error.



5
6
7
# File 'lib/dekiru/data_migration_operator.rb', line 5

def error
  @error
end

#resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'lib/dekiru/data_migration_operator.rb', line 5

def result
  @result
end

#started_atObject (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

#streamObject (readonly)

Returns the value of attribute stream.



5
6
7
# File 'lib/dekiru/data_migration_operator.rb', line 5

def stream
  @stream
end

#titleObject (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, options = {}, &block)
  self.new(title, options).execute(&block)
end

Instance Method Details

#durationObject



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, options = {})
  total = options.delete(:total)
  opt = {
    format: '%a |%b>>%i| %p%% %t',
  }.merge(options).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