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



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

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.



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

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.

  • (ArgumentError)

    if a Data Migration with the given name is not a subclass of DataMigration.



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

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 ArgumentError, "#{name} is not a Data Migration"
  end

  migration
end

Instance Method Details

#after_cancelObject

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



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

def after_cancel
end

#after_completeObject

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



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

def after_complete
end

#after_pauseObject

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



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

def after_pause
end

#after_resumeObject

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



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

def after_resume
end

#after_startObject

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



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

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.



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

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.



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

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)


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

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.



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

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)


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

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.



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

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