Class: Lockbox::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/lockbox/migrator.rb

Instance Method Summary collapse

Constructor Details

#initialize(relation, batch_size:) ⇒ Migrator

Returns a new instance of Migrator.



3
4
5
6
7
# File 'lib/lockbox/migrator.rb', line 3

def initialize(relation, batch_size:)
  @relation = relation
  @transaction = @relation.respond_to?(:transaction)
  @batch_size = batch_size
end

Instance Method Details

#migrate(restart:) ⇒ Object

TODO add attributes option



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lockbox/migrator.rb', line 26

def migrate(restart:)
  fields = model.respond_to?(:lockbox_attributes) ? model.lockbox_attributes.select { |k, v| v[:migrating] } : {}

  # need blind indexes for building relation
  blind_indexes = model.respond_to?(:blind_indexes) ? model.blind_indexes.select { |k, v| v[:migrating] } : {}

  attachments = model.respond_to?(:lockbox_attachments) ? model.lockbox_attachments.select { |k, v| v[:migrating] } : {}

  perform(fields: fields, blind_indexes: blind_indexes, restart: restart) if fields.any? || blind_indexes.any?
  perform_attachments(attachments: attachments, restart: restart) if attachments.any?
end

#modelObject



9
10
11
# File 'lib/lockbox/migrator.rb', line 9

def model
  @model ||= @relation
end

#rotate(attributes:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lockbox/migrator.rb', line 13

def rotate(attributes:)
  fields = {}
  attributes.each do |a|
    # use key instead of v[:attribute] to make it more intuitive when migrating: true
    field = model.lockbox_attributes[a]
    raise ArgumentError, "Bad attribute: #{a}" unless field
    fields[a] = field
  end

  perform(fields: fields, rotate: true)
end