Class: Backup::MoveLogs

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/move_logs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, db_helper, dry_run_reporter = nil) ⇒ MoveLogs

Returns a new instance of MoveLogs.



7
8
9
10
11
# File 'lib/backup/move_logs.rb', line 7

def initialize(config, db_helper, dry_run_reporter=nil)
  @config = config
  @dry_run_reporter = dry_run_reporter
  @db_helper = db_helper
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/backup/move_logs.rb', line 5

def config
  @config
end

Instance Method Details

#process_logs_batch(logs_batch) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/backup/move_logs.rb', line 22

def process_logs_batch(logs_batch)
  log_hashes = logs_batch.as_json
  @db_helper.connect_db(@config.destination_db_url)

  log_hashes.each do |log_hash|
    new_log = Log.new(log_hash)
    new_log.save!
  end

  @db_helper.connect_db(@config.database_url)

  logs_batch.each(&:destroy)

  GC.start
end

#runObject



13
14
15
16
17
18
19
20
# File 'lib/backup/move_logs.rb', line 13

def run
  return run_dry if @config.dry_run

  @db_helper.connect_db(@config.database_url)
  Log.order(:id).in_batches(of: @config.limit.to_i).map do |logs_batch|
    process_logs_batch(logs_batch)
  end
end

#run_dryObject



38
39
40
41
# File 'lib/backup/move_logs.rb', line 38

def run_dry
  ids = Log.order(:id).map(&:id)
  @dry_run_reporter.add_to_report(:logs, *ids)
end