Class: Dekiru::DataMigration::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/dekiru/data_migration/migration.rb,
sig/dekiru/data_migration/migration.rbs

Overview

Base class for data migration with testable method separation

Constant Summary collapse

DEFAULT_BATCH_SIZE =

Default batch size used by ActiveRecord's in_batches when of: is not given

1000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



10
11
12
# File 'lib/dekiru/data_migration/migration.rb', line 10

def batch_size
  @batch_size
end

Class Method Details

.run(options = {}) ⇒ Boolean

Parameters:

  • options (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dekiru/data_migration/migration.rb', line 12

def self.run(options = {})
  migration = new
  title = migration.title

  options = options.dup
  migration.batch_size = options.delete(:batch_size)

  Operator.execute(title, options) do
    migration.instance_variable_set(:@operator_context, self)
    migration.migrate
  end
end

Instance Method Details

#each_with_progress(enum, options = {}, &block) ⇒ void

This method returns an undefined value.



106
107
108
109
110
111
112
113
# File 'lib/dekiru/data_migration/migration.rb', line 106

def each_with_progress(enum, options = {}, &block)
  if @operator_context
    @operator_context.send(:each_with_progress, enum, options, &block)
  else
    # Default behavior during test (no progress bar)
    enum.each(&block)
  end
end

#find_each_with_progress(scope, options = {}) {|arg0| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • scope (Object)
  • options (Hash[Symbol, untyped]) (defaults to: {})

Yields:

Yield Parameters:

  • arg0 (Object)

Yield Returns:

  • (void)


97
98
99
100
101
102
103
104
# File 'lib/dekiru/data_migration/migration.rb', line 97

def find_each_with_progress(scope, options = {}, &block)
  if @operator_context
    @operator_context.send(:find_each_with_progress, scope, options, &block)
  else
    # Default behavior during test (no progress bar)
    scope.find_each(&block)
  end
end

#log(message) ⇒ void

This method returns an undefined value.

Parameters:

  • message (String)


88
89
90
91
92
93
94
95
# File 'lib/dekiru/data_migration/migration.rb', line 88

def log(message)
  if @operator_context
    @operator_context.send(:log, message)
  else
    # Default behavior during test
    puts message
  end
end

#migratevoid

This method returns an undefined value.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dekiru/data_migration/migration.rb', line 29

def migrate
  targets = migration_targets

  target_count = targets.count
  log "Target count: #{target_count}"
  confirm?

  if migrate_batch_overridden?
    migrate_in_batches(targets, target_count)
  else
    migrate_each_record(targets)
  end

  log "Migration completed"
end

#migrate_batch(relation) ⇒ Object

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/dekiru/data_migration/migration.rb', line 53

def migrate_batch(relation)
  raise NotImplementedError, "#{self.class}#migrate_batch must be implemented"
end

#migrate_record(record) ⇒ void

This method returns an undefined value.

Parameters:

  • record (Object)

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/dekiru/data_migration/migration.rb', line 49

def migrate_record(record)
  raise NotImplementedError, "#{self.class}#migrate_record must be implemented"
end

#migration_targetsObject

Returns:

  • (Object)

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/dekiru/data_migration/migration.rb', line 45

def migration_targets
  raise NotImplementedError, "#{self.class}#migration_targets must be implemented"
end

#titleString

Returns:

  • (String)


25
26
27
# File 'lib/dekiru/data_migration/migration.rb', line 25

def title
  self.class.name.demodulize.underscore.humanize
end