Class: Dekiru::DataMigration::Migration
- Inherits:
-
Object
- Object
- Dekiru::DataMigration::Migration
- 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_batcheswhenof:is not given 1000
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
Returns the value of attribute batch_size.
Class Method Summary collapse
Instance Method Summary collapse
- #each_with_progress(enum, options = {}, &block) ⇒ void
- #find_each_with_progress(scope, options = {}) {|arg0| ... } ⇒ void
- #log(message) ⇒ void
- #migrate ⇒ void
- #migrate_batch(relation) ⇒ Object
- #migrate_record(record) ⇒ void
- #migration_targets ⇒ Object
- #title ⇒ String
Instance Attribute Details
#batch_size ⇒ Object
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
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/dekiru/data_migration/migration.rb', line 12 def self.run( = {}) migration = new title = migration.title = .dup migration.batch_size = .delete(:batch_size) Operator.execute(title, ) 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, = {}, &block) if @operator_context @operator_context.send(:each_with_progress, enum, , &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.
97 98 99 100 101 102 103 104 |
# File 'lib/dekiru/data_migration/migration.rb', line 97 def find_each_with_progress(scope, = {}, &block) if @operator_context @operator_context.send(:find_each_with_progress, scope, , &block) else # Default behavior during test (no progress bar) scope.find_each(&block) end end |
#log(message) ⇒ void
This method returns an undefined value.
88 89 90 91 92 93 94 95 |
# File 'lib/dekiru/data_migration/migration.rb', line 88 def log() if @operator_context @operator_context.send(:log, ) else # Default behavior during test puts end end |
#migrate ⇒ void
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
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.
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_targets ⇒ Object
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 |
#title ⇒ String
25 26 27 |
# File 'lib/dekiru/data_migration/migration.rb', line 25 def title self.class.name.demodulize.underscore.humanize end |