Class: OnlineMigrations::DataMigration

Inherits:
Object
  • Object
show all
Defined in:
lib/online_migrations/data_migration.rb

Overview

Base class that is inherited by the host application’s data migration classes.

Defined Under Namespace

Classes: NotFoundError

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.active_record_enumerator_batch_sizeObject



36
37
38
# File 'lib/online_migrations/data_migration.rb', line 36

def active_record_enumerator_batch_size
  @active_record_enumerator_batch_size
end

Class Method Details

.collection_batch_size(size) ⇒ Object

Limit the number of records that will be fetched in a single query when iterating over an Active Record collection migration.

Parameters:

  • size (Integer)

    the number of records to fetch in a single query.



43
44
45
# File 'lib/online_migrations/data_migration.rb', line 43

def collection_batch_size(size)
  self.active_record_enumerator_batch_size = size
end

.named(name) ⇒ DataMigration

Finds a Data Migration with the given name.

Parameters:

  • name (String)

    the name of the Data Migration to be found.

Returns:

Raises:

  • (NotFoundError)

    if a Data Migration with the given name does not exist.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/online_migrations/data_migration.rb', line 20

def named(name)
  namespace = OnlineMigrations.config.background_data_migrations.migrations_module.constantize
  internal_namespace = ::OnlineMigrations::BackgroundDataMigrations

  migration = "#{namespace}::#{name}".safe_constantize ||
              "#{internal_namespace}::#{name}".safe_constantize

  raise NotFoundError.new("Data Migration #{name} not found", name) if migration.nil?
  if !(migration.is_a?(Class) && migration < self)
    raise NotFoundError.new("#{name} is not a Data Migration", name)
  end

  migration
end

Instance Method Details

#after_cancelObject

A hook to override that will be called when the migration is cancelled.



85
86
# File 'lib/online_migrations/data_migration.rb', line 85

def after_cancel
end

#after_completeObject

A hook to override that will be called when the migration finished its work.



75
76
# File 'lib/online_migrations/data_migration.rb', line 75

def after_complete
end

#after_pauseObject

A hook to override that will be called when the migration is paused.



80
81
# File 'lib/online_migrations/data_migration.rb', line 80

def after_pause
end

#after_resumeObject

A hook to override that will be called when the migration resumes its work.



63
64
# File 'lib/online_migrations/data_migration.rb', line 63

def after_resume
end

#after_startObject

A hook to override that will be called when the migration starts running.



50
51
# File 'lib/online_migrations/data_migration.rb', line 50

def after_start
end

#after_stopObject

A hook to override that will be called each time the migration is interrupted.

This can be due to interruption or sidekiq stopping.



70
71
# File 'lib/online_migrations/data_migration.rb', line 70

def after_stop
end

#around_processObject

A hook to override that will be called around ‘process’ each time.

Can be useful for some metrics collection, performance tracking etc.



57
58
59
# File 'lib/online_migrations/data_migration.rb', line 57

def around_process
  yield
end

#build_enumerator(cursor:) ⇒ Enumerator

Enumerator builder. You may override this method to return any Enumerator yielding pairs of ‘[item, item_cursor]`, instead of using `collection`.

It is useful when it is not practical or impossible to define an explicit collection in the ‘collection` method.

Parameters:

  • cursor (Object, nil)

    cursor position to resume from, or nil on initial call.

Returns:

  • (Enumerator)


124
125
# File 'lib/online_migrations/data_migration.rb', line 124

def build_enumerator(cursor:)
end

#collectionActiveRecord::Relation, ...

The collection to be processed.

Returns:

  • (ActiveRecord::Relation, ActiveRecord::Batches::BatchEnumerator, Array, Enumerator)

Raises:

  • (NotImplementedError)

    with a message advising subclasses to override this method.



94
95
96
# File 'lib/online_migrations/data_migration.rb', line 94

def collection
  raise NotImplementedError, "#{self.class.name} must implement a 'collection' method"
end

#countInteger?

Total count of iterations to be performed (optional, to be able to show progress).

Returns:

  • (Integer, nil)


111
112
# File 'lib/online_migrations/data_migration.rb', line 111

def count
end

#process(_item) ⇒ Object

The action to be performed on each item from the collection.

Parameters:

  • _item

    the current item from the collection being iterated

Raises:

  • (NotImplementedError)

    with a message advising subclasses to override this method.



103
104
105
# File 'lib/online_migrations/data_migration.rb', line 103

def process(_item)
  raise NotImplementedError, "#{self.class.name} must implement a 'process' method"
end