Class: HasHelpers::DataMigration::Migrator
- Inherits:
-
Object
- Object
- HasHelpers::DataMigration::Migrator
- Defined in:
- lib/has_helpers/data_migration/migrator.rb
Class Method Summary collapse
- .migration_classes ⇒ Object
- .non_queued_classes ⇒ Object
-
.run(migration_name = nil) ⇒ Object
- .run_async ⇒ Object
- .run_one(migration) ⇒ Object
Class Method Details
.migration_classes ⇒ Object
74 75 76 |
# File 'lib/has_helpers/data_migration/migrator.rb', line 74 def self.migration_classes ::HasHelpers::DataMigration::Base.descendants end |
.non_queued_classes ⇒ Object
78 79 80 |
# File 'lib/has_helpers/data_migration/migrator.rb', line 78 def self.non_queued_classes migration_classes.reject(&:enqueued?) end |
.run(migration_name = nil) ⇒ Object
- By default, the rake task will try to run all pending (i.e. not completed) data migrations synchronously, one at a time. If you set MIGRATION, it will try just that one data migration. It will skip any data migrations that have already been enqueued. A data migration is enqueued when good job picks it up, or if you ran it with this rake task and it did not complete successfully.
- To rerun a data migration that was previously enqueued, first go into a rails console, and execute .rerun (for example, FixMetadataCreatedByIds.rerun) - and then run this rake task.
- If a data migration raises an uncaught error, the rake task will stop. It will not run subsequent data migrations, BUT... since the one that errored out is now marked as enqueued, running the rake task again will skip that one.
- If you have dependent data migrations, you may need to run this multiple times. For example, if A depends on B, it’s possible the first time you run the rake task, it will try to run A first and skip it because its dependency has not run yet. (You could also get lucky and the rake task might try to run B before A, but don’t count on it.) The rake task will output info to the console on which data migrations were run versus skipped.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/has_helpers/data_migration/migrator.rb', line 34 def self.run(migration_name = nil) puts "*** Starting..." if migration_name data_migration = migration_name.safe_constantize unless data_migration && data_migration < ::HasHelpers::DataMigration::Base puts "*** #{migration_name} is not a valid DataMigration." return end if data_migration.complete? puts "*** #{migration_name} is already complete" return end run_one(data_migration) else non_queued_classes.each do |migration| run_one(migration) end end puts "*******************" puts "DONE running data migrations. Check log/data_migrations.log and log/data_migrations/ for details." puts "*******************" end |
.run_async ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/has_helpers/data_migration/migrator.rb', line 6 def self.run_async non_queued_classes.each do |migration| if migration.ready? migration.enqueue end end end |
.run_one(migration) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/has_helpers/data_migration/migrator.rb', line 58 def self.run_one(migration) if migration.enqueued? puts "WARNING: Skipping #{migration.name} because it is already enqueued." return end if migration.ready? puts "Running #{migration.name}..." migration.send(:model).update(enqueued_at: Time.zone.now) migration.new.send(:internal_run) puts "Done #{migration.name}" else puts "WARNING: #{migration.name} not ready to run (may have dependencies)." end end |