Class: ColdStorage::Restorer
- Inherits:
-
Object
- Object
- ColdStorage::Restorer
- 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
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
-
#associations ⇒ Hash{Symbol => Object}
The associations
with:resolves to, as association name => nested with. -
#call(ids) ⇒ Integer
Number of restored rows, children included.
-
#initialize(model, with: nil, delete_from_archive: true, batch_size: nil, dry_run: ColdStorage.config.dry_run, visited: []) ⇒ Restorer
constructor
A new instance of Restorer.
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.
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
#model ⇒ Object (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
#associations ⇒ Hash{Symbol => Object}
The associations with: resolves to, as association name => nested with.
60 61 62 |
# File 'lib/cold_storage/restorer.rb', line 60 def associations @associations ||= (@with) end |
#call(ids) ⇒ Integer
Returns 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 |