Class: ActiveMigration::Base
- Inherits:
-
Object
- Object
- ActiveMigration::Base
- Defined in:
- lib/active_migration/base.rb
Overview
ActiveMigration::Base is subclassed by your migration. It defines a DSL similar to ActiveRecord, in which it feel more like a configuration.
Typical Usage:
class PostMigration < ActiveMigration::Base
set_active_model 'Post'
set_legacy_model 'Legacy::Post'
map [['name_tx', 'name' ],
['description_tx', 'description'],
['date', 'created_at' ]]
end
Class Attribute Summary collapse
-
.active_model ⇒ Object
Returns the value of attribute active_model.
-
.active_record_mode ⇒ Object
Returns the value of attribute active_record_mode.
-
.legacy_find_options ⇒ Object
Returns the value of attribute legacy_find_options.
-
.legacy_model ⇒ Object
Returns the value of attribute legacy_model.
-
.mappings ⇒ Object
Returns the value of attribute mappings.
Class Method Summary collapse
-
.map(mappings) ⇒ Object
(also: mappings=)
Sets the mappings for the migration.
-
.set_active_model(active_model, mode = :create) ⇒ Object
(also: active_model=)
Sets the active model to be migrated to.
-
.set_legacy_model(legacy_model, *args) ⇒ Object
(also: legacy_model=)
Sets the legacy model to be migrated from.
Instance Method Summary collapse
-
#run ⇒ Object
Runs the migration.
Class Attribute Details
.active_model ⇒ Object
Returns the value of attribute active_model.
30 31 32 |
# File 'lib/active_migration/base.rb', line 30 def active_model @active_model end |
.active_record_mode ⇒ Object
Returns the value of attribute active_record_mode.
30 31 32 |
# File 'lib/active_migration/base.rb', line 30 def active_record_mode @active_record_mode end |
.legacy_find_options ⇒ Object
Returns the value of attribute legacy_find_options.
30 31 32 |
# File 'lib/active_migration/base.rb', line 30 def @legacy_find_options end |
.legacy_model ⇒ Object
Returns the value of attribute legacy_model.
30 31 32 |
# File 'lib/active_migration/base.rb', line 30 def legacy_model @legacy_model end |
.mappings ⇒ Object
Returns the value of attribute mappings.
30 31 32 |
# File 'lib/active_migration/base.rb', line 30 def mappings @mappings end |
Class Method Details
.map(mappings) ⇒ Object Also known as: mappings=
Sets the mappings for the migration. Mappings are specified in a multidimensional array. Each array elment contains another array in which the legacy field is the first element and the active field is the second elment.
map [['some_old_field', 'new_spiffy_field']]
80 81 82 |
# File 'lib/active_migration/base.rb', line 80 def map(mappings) @mappings = mappings end |
.set_active_model(active_model, mode = :create) ⇒ Object Also known as: active_model=
Sets the active model to be migrated to.
Also, an additional parameter for the method of instantiation. Valid parameters are: :create or :update. Defaults to :create. Use this if records already exist in the active database. Lookup with :update will be done via the PK of the legacy record.
set_active_model 'Post'
set_active_model 'Post',
:update
68 69 70 71 |
# File 'lib/active_migration/base.rb', line 68 def set_active_model(active_model, mode=:create) @active_model = eval(active_model) @active_record_mode = mode end |
.set_legacy_model(legacy_model, *args) ⇒ Object Also known as: legacy_model=
Sets the legacy model to be migrated from. It’s wise to namespace your legacy models to prevent class duplicates.
Also, *args can be passed a Hash to hold finder options for legacy record lookup.
Note: If you set :limit, it will stagger your selects with an offset. This is intended to break up large datasets to conserve memory. Keep in mind, for this functionality to work :offset(because it is needed internally) can never be specified, it will be deleted.
set_legacy_model Legacy::Post
set_legacy_model Legacy::Post,
:conditions => 'some_field = value',
:order => 'this_field ASC',
:limit => 5
48 49 50 51 52 53 |
# File 'lib/active_migration/base.rb', line 48 def set_legacy_model(legacy_model, *args) @legacy_model = eval(legacy_model) args[0].delete(:offset) if args[0] @legacy_find_options = args[0] unless args.empty? @legacy_find_options ||= {} end |
Instance Method Details
#run ⇒ Object
Runs the migration.
MyMigration.new.run
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/active_migration/base.rb', line 91 def run logger.info("#{self.class.to_s} is starting.") = self.class..dup .delete(:order) .delete(:group) .delete(:limit) .delete(:offset) @num_of_records = self.class.legacy_model.count() if self.class.[:limit] && (@num_of_records > self.class.[:limit]) run_in_batches @num_of_records else run_normal end logger.info("#{self.class.to_s} migrated all #{@num_of_records} records successfully.") end |