Class: MaglevRecord::Migrator
- Defined in:
- lib/maglev_record/migration/migrator.rb
Overview
Given a migration list (the desired applied migrations) this class migrates to these state. Therefore, it decides which migrations must be undone, and which must be done and leaves the stone in the desired state.
Constant Summary collapse
- MIGRATION_KEY =
:__migrations__
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(migration_list) ⇒ Migrator
constructor
A new instance of Migrator.
-
#migration_store ⇒ Object
Returns all migrations currently in the stone in the correct order.
- #migrations_todo ⇒ Object
-
#up(logger = @non_displaying_logger) ⇒ Object
Applies the desired state of migrations.
- #up?(logger = @non_displaying_logger) ⇒ Boolean
Constructor Details
#initialize(migration_list) ⇒ Migrator
Returns a new instance of Migrator.
11 12 13 14 15 |
# File 'lib/maglev_record/migration/migrator.rb', line 11 def initialize(migration_list) @migration_list = migration_list @non_displaying_logger = Logger.new(STDOUT) @non_displaying_logger.level = Logger::FATAL end |
Class Method Details
.for_directory(directory) ⇒ Object
52 53 54 55 56 |
# File 'lib/maglev_record/migration/migrator.rb', line 52 def self.for_directory(directory) loader = MigrationLoader.new loader.load_directory(directory) new(loader.migration_list) end |
Instance Method Details
#migration_store ⇒ Object
Returns all migrations currently in the stone in the correct order.
19 20 21 |
# File 'lib/maglev_record/migration/migrator.rb', line 19 def migration_store Maglev::PERSISTENT_ROOT[MIGRATION_KEY] ||= [] end |
#migrations_todo ⇒ Object
23 24 25 26 27 |
# File 'lib/maglev_record/migration/migrator.rb', line 23 def migrations_todo @migration_list.reject do |mig| migration_store.include?(mig.id) end end |
#up(logger = @non_displaying_logger) ⇒ Object
Applies the desired state of migrations.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/maglev_record/migration/migrator.rb', line 39 def up(logger = @non_displaying_logger) Maglev.abort_transaction to_do = migrations_todo logger.info("Already applied all migrations.") if to_do.empty? to_do.sort.each do |mig| mig.logger = logger logger.info("Doing '" + mig.name + "' from " + mig..to_s) mig.do migration_store << mig.id end Maglev.commit_transaction end |
#up?(logger = @non_displaying_logger) ⇒ Boolean
29 30 31 32 33 34 35 |
# File 'lib/maglev_record/migration/migrator.rb', line 29 def up?(logger = @non_displaying_logger) # todo: test migrations_todo.sort.each do |mig| logger.info("to do: '" + mig.name + "' from " + mig..to_s) end logger.info("Already applied all migrations.") if migrations_todo.empty? end |