Class: ColdStorage::Restorer

Inherits:
Object
  • Object
show all
Includes:
Logging, RowReader
Defined in:
lib/cold_storage/restorer.rb

Overview

Moves rows back from the archive database into the primary one, optionally taking their archived children along.

ColdStorage.restore(Invoice, [1, 2])
ColdStorage.restore(Invoice, [1, 2], with: :all)
ColdStorage.restore(Invoice, [1, 2], with: [:invoice_lines])
ColdStorage.restore(Invoice, [1, 2], with: { invoice_lines: [:taxes] })

Parents are written before their children, so foreign keys in the primary database hold at every step.

Constant Summary

Constants included from Logging

Logging::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, with: nil, delete_from_archive: true, batch_size: nil, dry_run: ColdStorage.config.dry_run, visited: []) ⇒ Restorer

Returns a new instance of Restorer.

Parameters:

  • with (Symbol, Array, Hash, nil) (defaults to: nil)

    associations to restore too; :all walks every has_many/has_one that has an archive table

  • delete_from_archive (Boolean) (defaults to: true)

    remove the rows from the archive once they are back in the primary database



24
25
26
27
28
29
30
31
32
# File 'lib/cold_storage/restorer.rb', line 24

def initialize(model, with: nil, delete_from_archive: true, batch_size: nil,
               dry_run: ColdStorage.config.dry_run, visited: [])
  @model               = model
  @with                = with
  @delete_from_archive = delete_from_archive
  @batch_size          = batch_size || ColdStorage.config.batch_size
  @dry_run             = dry_run
  @visited             = visited
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



18
19
20
# File 'lib/cold_storage/restorer.rb', line 18

def model
  @model
end

Instance Method Details

#associationsHash{Symbol => Object}

The associations with: resolves to, as association name => nested with.

Returns:

  • (Hash{Symbol => Object})


60
61
62
# File 'lib/cold_storage/restorer.rb', line 60

def associations
  @associations ||= expand(@with)
end

#call(ids) ⇒ Integer

Returns number of restored rows, children included.

Parameters:

  • ids (Array, ActiveRecord::Relation)

    ids to restore, or a relation on the archive model (Invoice.archived.where(...))

Returns:

  • (Integer)

    number of restored rows, children included



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cold_storage/restorer.rb', line 37

def call(ids)
  validate_associations!

  relation = normalize(ids)
  restored = 0
  own = 0

  relation.in_batches(of: @batch_size) do |batch|
    rows = read(batch)
    next if rows.empty?

    own += write(rows)
    restored_ids = rows.map { |row| row[primary_key] }
    restored += restore_associations(restored_ids)
    delete(restored_ids)
  end

  log("#{model}: restored #{own} row(s) from the archive#{' [dry run]' if @dry_run}")
  own + restored
end